C# .NET - show image from image list - Asked By Priyanka on 11-Feb-12 12:49 AM

my question is related to visual C#. i am retreiveing data in list view using following code:


try


{

listView1.Items.Clear();

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\db1.mdb;Persist Security Info=True;Jet OLEDB:Database Password=123456789");

con.Open();


string sql = "select name from record where surname= 'sharma'";

OleDbCommand com = new OleDbCommand(sql, con);

OleDbDataReader dr = com.ExecuteReader();

while (dr.Read())

{

ListViewItem b = new ListViewItem();

b.Text = dr1.GetString(0);

 

listView1.Items.Add(b,0);

listView1.Refresh();

}


con.Close();

}


catch (Exception ex)

{

MessageBox.Show("" + ex);

}


but i also want to show image with each record from image list from index 0.. i dont know how to do that...... i am giving imge index at where i background with yellow color.but it is giving error...Error 1 The best overloaded method match for 'System.Windows.Forms.ListView.ListViewItemCollection.Add(string, int)' has some invalid arguments

Error 2 Argument '1': cannot convert from 'System.Windows.Forms.ListViewItem' to 'string' 

Web Star replied to Priyanka on 11-Feb-12 01:31 AM
As you got error itself describe correctly what is the cause of that error , that are saying Add(string, int)' has some invalid arguments where needed two paramter one is sting and 2nd one is int
where as you are passing 'System.Windows.Forms.ListViewItem' as first paramter b so you should pass that is string
change your code as follows

listView1.Items.Add(b.Text.ToString(),0);


Priyanka replied to Web Star on 11-Feb-12 04:05 AM
thanks.. it is working fine but the problem is that it also adds some texts with name.. it adds like : ListViewItem : {Priyanka}.
whats the reason for that ??? and whats the solution ??
Web Star replied to Priyanka on 12-Feb-12 10:59 AM
if the binding text is "Priyanka" than that only ge tfrom .text property so first check in your code actually.
If you in your code there are fixed extra text in that than you will be remove from that string.