hello
I want to display the records of student who's name were selected from listbox. The selection mode of listbox is multiple so user can select multiple options at a time and display it in grid view
please reply soon
Multiple selection listbox and display records from database
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Mar 7, 2015, 8:27 AM EST
First select the SelectionMode Property of Listbox to Multiple
Now on button click event you can use this method
SqlConnection con = new SqlConnection("Put here Your Connection string");
con.Open();
string str="select * from tablename where Student_Name in (";
int ln;
foreach (ListItem lt in ListBox1.Items)
{
if (lt.Selected)
{
str += "'"+lt.ToString()+"'"+",";
}
}
ln = str.Length;
str = str.Substring(0, ln - 1);
str += ")";
SqlCommand com = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable tab = new DataTable();
da.Fill(tab);
GridView1.DataSource = tab;
GridView1.DataBind();
con.Close();
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected)
{
lblValues.Text += li.Text+"
";
}
}
- See more at: http://www.dotnetfox.com/articles/how-to-get-multiple-selected-value-in-listbox-control-Asp-Net-using-C-Sharp-1047.aspx#sthash.NutcXrWv.dpuf