Set the Value has Selected to Listbox by getting value from Database

Posted by Venkat | Labels:

As we already saw how to get the items(Multiple selection item) from the Listbox. Suppose If we store that value like this(2,4,6,) ie: I concatenate the (,) for each valu -  2,4,6 are the value of the listbox so we are going to rebind this value to List and set it has Selected.

Here the subject is the field on the Table where the [ 2,4,6, ] was stored.

1) First we get the database value to string Variable

2) Then using Split property - remove the (,) from the String and assign to StringArray

3) Atlast use foreach loop to set the item to be selected.

  string subjectList = getUserProfile_SqlDataReader["subject"].ToString();
                            string[] subjectListItems = subjectList.Split(',');
                            foreach (string s in subjectListItems)
                            {
                                foreach (ListItem item in Subject_ListBox.Items)
                                {
                                    if (item.Value == s) item.Selected = true;
                                }
                            }

PayOffers.in