none
Inserting ListBox item into sql server table RRS feed

  • Question

  • I have 

    foreach (Object item in ListBoxName.SelectedItems)

    then in my table insert, @Value

    then

    Query.Parameters.AddWithValue("@Value", item.toString());

    Using try catch, I get

    Must declare the scalar variable \"@Value\"."

    What am I doing wrong?

    Wednesday, August 28, 2019 4:16 PM

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);



    Thursday, August 29, 2019 2:50 PM

All replies

  • Maybe this Query variable is not the command that is actually executed?

    Or maybe the invoked query or stored procedure is incorrect.

    Wednesday, August 28, 2019 7:04 PM
  • 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?

    Wednesday, August 28, 2019 8:01 PM
  • 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


    Thursday, August 29, 2019 9:12 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?

    Thursday, August 29, 2019 10:49 AM
  • 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);



    Thursday, August 29, 2019 2:50 PM
  • One small problem solved, now trying to figure out this one, 

    Sql server timeout and objects overwritten

    Thursday, August 29, 2019 8:08 PM