I have values in the listbox from which I have to select one or more values and save it in the database. But when I select more than one value (ex: 2 items selected) it is only saving the first value of the items I have selected (ex: twice).
Here is the query to save in the database.
foreach (ListItem li in lbMess.Items)
{
if (li.Selected)
{
strSQL += string.Format("INSERT INTO message(messageContent, messageDate, staffCode) VALUES ('{0}',getdate(),'{1}');", txtMess.Text, lbMess.SelectedValue);
}
}
foreach(ListItem li...For example, if you say "Get me the values of all selected items in this control" - Where will you store those values? You'd store them in a variable, right? So in your case, that variable isli. So to access the SelectedItem data, you'd do something like:foreach(ListItem li in lbMess.Items) { string data = li.Text; }