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:
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