2

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);
    }
}
1
  • When you're accessing items from within a foreach loop, you can't retrieve the data you want from the Control itself, you have to access the data from the variable that you declared in the 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 is li. So to access the SelectedItem data, you'd do something like: foreach(ListItem li in lbMess.Items) { string data = li.Text; } Commented Apr 23, 2014 at 8:12

4 Answers 4

4

In your insert statement you aren't using the li you are iterating.

Try this:

strSQL += string.Format("INSERT INTO message(messageContent,messageDate,staffCode) VALUES ('{0}',getdate(),'{1}');", txtMess.Text, li.Text);
Sign up to request clarification or add additional context in comments.

1 Comment

@mujtaba: Do you need more help? If any of the answers were sufficient, please don't forget to mark your question answered by checking the checkmark before the answer.
1

Youu are using lbMess.SelectedValue which by default will only get the first item, yet you are looping through all the items.

Make sure you use the value of the li object instead. (li.Value or li.Text depending on how you implemented it)

Comments

1

Youu are using lbMess.SelectedValue which by default will only get the first item.

Try this

foreach (ListItem li in lbMess.Items)
{
   if (li.Selected)
   {
     strSQL += string.Format("INSERT INTO message(messageContent,messageDate,staffCode) VALUES ('{0}',getdate(),'{1}');", txtMess.Text, li.Value);
   }

 }

OR:

foreach (ListItem li in lbMess.Items)
{
   if (li.Selected)
   {
     strSQL += string.Format("INSERT INTO message(messageContent,messageDate,staffCode) VALUES ('{0}',getdate(),'{1}');", txtMess.Text, li.Text);
   }

 }

Comments

0

I believe you should iterate the SelectedItems property, like so:

            foreach(var item in listBox1.SelectedItems)
            {
                // Insert into DB.
                strSQL += string.Format("INSERT INTO message(messageContent,
                messageDate,staffCode) VALUES ('{0}',getdate(),'{1}');", 
                    txtMess.Text, (String)item);
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.