Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to select 2 columns from 2comboboxes from table1 and put the data in these 2 columns in in listview11 in button1 click event

C++
if (cn.State == ConnectionState.Closed) cn.Open();
            cm.Connection = cn;
            if (comboBox3.Enabled == true)
            {
                string searchFor2 = comboBox1.Text;
                string searchFor3 = comboBox2.Text;
                string selectSql = "SELECT " + searchFor2 + ", " + searchFor3 + " FROM itmsparts";
                SqlCommand com = new SqlCommand(selectSql, cn);
                try
                {
                    using (SqlDataReader read1 = com.ExecuteReader())
                    {
                        while (read1.Read())
                        {
                            foreach (ListViewItem item in listView1.Items)
                            {
                                item.Text = (read1["searchFor2"].ToString());
                                item.SubItems[1].Text = (read1["searchFor3"].ToString());

                            }


                        }
                    }
                }
                finally
                {

                }
            }


but no error and no nothing is added to listview1
Posted
Updated 10-Nov-15 8:03am
v3

1 solution

Well, I'd start with the debugger.
Put a breakpoint on the line
csb
while (read1.Read())

and look at exactly what is in the selectSql string.
Then, single step you code and see exactly what is happening.

My guess is that either the foreach loop doesn't execute because your listview is empty, or you are getting exceptions because there are no results returned that have the column names "searchFor2" and "searchFor3". It's possible that this would fix that:
C#
{
    item.Text = (read1[searchFor2].ToString());
    item.SubItems[1].Text = (read1[searchFor3].ToString());

But you need to start by finding out exactly what is happening first.
 
Share this answer
 
Comments
Ahmed Zoeil 10-Nov-15 14:24pm    
what i'm trying to do is that the form contain 2 comboboxes contain columns names in a table .. so when the user click button1 it should find the data by column names user selected in comboboxes and add it to listview1
OriginalGriff 10-Nov-15 14:48pm    
And?
What did the debugger show you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900