Answered by:
Inserting ListBox item into sql server table

Question
-
Answers
-
I've got the index of each item selected
foreach (Object item in ListBoxName.SelectedItems) { int index = ListBoxName.Items.IndexOf(item); }
How do I get the item value (text)?
string value = ListBoxName.Items[index].ToString();
this gives System.Data.DataRowView
got it
string value = LitBoxName.GetItemText(item);
- Edited by RichardDunneBSc Thursday, August 29, 2019 4:17 PM
- Marked as answer by RichardDunneBSc Thursday, August 29, 2019 4:17 PM
All replies
-
-
I solved this problem
for(int x=0;x<ListBoxName.SelectedItems.Count;x++)
then
Query.Parameters.AddWithValue("@Value", ListBoxName.SelectedItems[x].toString());
I have two single column ListBox's, both displaying the same column from separate tables. I am essentially moving an item from one table to another. Both ListBox's DisplayMember are set to the columnName. Although the insert works, the item displays as System.Data.DataRowView, not the item value. Any idea why?
-
Hi RichardDunneBSc,
Welcome to the MSDN forum.
As far as l know, item.toString() is object.toString method, the result is "object", so you will face the error.
The right code is you should convert the object to listitem and then use the listitem value's object type because it is string type.
Hope it could help you.
Best Regards,
Perry
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com
- Edited by Perry Qian-MSFTMicrosoft contingent staff Thursday, August 29, 2019 9:44 AM
-
I've got moving an item from one ListBox (Sql table column) to another working, but its not moving the correct item. Its selecting the first item in the ListBox, not the first of selected item(s).
for (int x = 0; x < ListBoxName.SelectedItems.Count; x++) { int index = ListBoxName.SelectedItems.IndexOf(ListBoxName.SelectedItems[x]); string value = ListBoxName.GetItemText(ListBoxName.Items[index]); MessageBox.Show(value);
Also, when a ListBox is filled, it highlights (selects) the first item by default. Is there any way to prevent this?
-
I've got the index of each item selected
foreach (Object item in ListBoxName.SelectedItems) { int index = ListBoxName.Items.IndexOf(item); }
How do I get the item value (text)?
string value = ListBoxName.Items[index].ToString();
this gives System.Data.DataRowView
got it
string value = LitBoxName.GetItemText(item);
- Edited by RichardDunneBSc Thursday, August 29, 2019 4:17 PM
- Marked as answer by RichardDunneBSc Thursday, August 29, 2019 4:17 PM
-
One small problem solved, now trying to figure out this one,