Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a listbox populated dynamically, i want to loop through it and retrieve its items one after the other. this is what i have tried

C#
foreach (object p in ListBox1.Items)
                    {
                        label1.Text = p.ToString();
                        MySqlCommand cmd2 = new MySqlCommand("select Code, Title, Unit, semester from pla20132014.courses where Code = '"+ p.ToString() +"' ", conn);
                        MySqlDataAdapter ad = new MySqlDataAdapter();               
                        
                        ad.SelectCommand = cmd2;
                        DataTable dt = new DataTable();
                        ad.Fill(dt);
                        gridview1.DataSource = dt;
                        gridview1.DataBind();
                        Panel3.Visible = true;
                    }


but it doesn't work. please i need assistance. Thanks
Posted

ListBox1.Items contains a collection of ListItem objects.

I think ToString() should work, but it's safer to use the object type and get the text or value depending on what you need:

C#
//you don't have to keep creating new commands.  just use a parameter.  It's safer anyway
MySqlCommand cmd2 = new MySqlCommand("select Code, Title, Unit, semester from pla20132014.courses where Code = @code ", conn);
cmd.Parameters.Add(new SqlParameter("@code", SqlDbType.VarChar));
MySqlDataAdapter ad = new MySqlDataAdapter(); 

foreach (ListItem p in ListBox1.Items)
{
    label1.Text = p.Text;              
    
    cmd.Parameters["@code"] = p.Text;

    ad.SelectCommand = cmd2;
    DataTable dt = new DataTable();
    ad.Fill(dt);
    gridview1.DataSource = dt;
    gridview1.DataBind();
    Panel3.Visible = true;
}


Try that and see if it fixes the issue ^_^

Andy
 
Share this answer
 
Comments
EasyHero 10-Sep-15 6:32am    
This worked for only the first item in the loop. didn't work for the remaining. it issued an "Parameter @code already defined" error
Andy Lanng 10-Sep-15 6:34am    
That shouldn't happen. In my code the Parameters.Add code is outside of the loop. cmd.Parameters["@code"] just sets the value. Are you sure your code is the same as mine?
EasyHero 10-Sep-15 7:22am    
i have some observations and possibly questions on line 3 of your code. the mysqlcommand name is cmd2 and in my code i changed the SqlParameter and SqlDbType to that of MySql each. apart from those, i have exactly same code in same position as yours but i'm not still getting any values
Andy Lanng 10-Sep-15 7:48am    
oh yeah - that's mysql - I guess I had my blinkers on.
I don't have the MySql libs. It could well be that my code will not work because of this (which sucks). In that case, create the command within the loop as you had before.
I can't test the libs without them, so I'm afraid my advice on thos libs ends there :S
Sorry for the confusion ^_^
EasyHero 10-Sep-15 7:55am    
no problem. Thanks, you still enlightened me
Try below code:
C#
foreach (ListItem li in listBox1.Items)
{
	if (li.Selected)
	{
		label1.Text = li.ToString();
		MySqlCommand cmd2 = new MySqlCommand("select Code, Title, Unit, semester from pla20132014.courses where Code = '" + li.Value.ToString() + "' ", conn);
		MySqlDataAdapter ad = new MySqlDataAdapter();

		ad.SelectCommand = cmd2;
		DataTable dt = new DataTable();
		ad.Fill(dt);
		gridview1.DataSource = dt;
		gridview1.DataBind();
		Panel3.Visible = true;
	}
}

Here it is checking which items are selected. Based on selected item it will enter inside if condition. If you want you can remove it.

Note: One thing I am not getting why are you writing whole code base inside for loop. Suppose I selected 3 items then code will execute 3 times and it set value to label and Panel will be visible True. So please keep those code block outside forloop which you don't want to execute again and again.
 
Share this answer
 
v2
C#
MySqlCommand cmd2 = new MySqlCommand("select Code, Title, Unit, semester from pla20132014.courses where Code = @cd ", conn);
                MySqlDataAdapter ad = new MySqlDataAdapter();
                DataTable dt = new DataTable();
                cmd2.Parameters.Add("@cd", MySqlDbType.VarChar);
                foreach (ListItem p in ListBox1.Items)
                {    
                    cmd2.Parameters["@cd"].Value = p.Text;
                    ad.SelectCommand = cmd2;

                    ad.Fill(dt);
                    gridview1.DataSource = dt;
                    gridview1.DataBind();
                    Panel3.Visible = true;         
                }                


This worked perfectly
 
Share this answer
 

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