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
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pankaj Kumar ChoudharyPosted Mar 7, 2015, 1:27 PM
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