There is combobox and few textboxes.
With the help of insert sqlcommand I can store textboxes values in the database.
But with the same insert sqlcommand I need to store one more combobox selected item too..
How to do this?

Dani AI

Generated

A few practical points to make the ComboBox value insert safe and maintainable.

If you only need the currently selected entry, there is no reason to insert every item. Use the control's SelectedValue (when data-bound) or SelectedItem/SelectedIndex/Text to get the value you want, then run a single, parameterized INSERT. 's loop approach can work when you truly want every item inserted, but avoid building SQL with string concatenation and avoid opening the connection inside each iteration. confirmed a solution was found; below are safer, clearer patterns to use.

Example: single selected value (WinForms VB.NET)

Dim sql As String = "INSERT INTO MyTable (NameCol, ChoiceCol) VALUES (@name, @choice)"
Using cn As New SqlConnection(connString)
    Using cmd As New SqlCommand(sql, cn)
        cmd.Parameters.Add("@name", SqlDbType.NVarChar, 200).Value = TextBoxName.Text.Trim()
        Dim choice As String = If(ComboBox1.SelectedValue IsNot Nothing, ComboBox1.SelectedValue.ToString(), ComboBox1.Text)
        cmd.Parameters.Add("@choice", SqlDbType.NVarChar, 100).Value = choice
        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Using

If multiple inserts are required (insert many items), open the connection once, reuse a parameter and optionally wrap in a transaction:

Using cn As New SqlConnection(connString)
    cn.Open()
    Using tx = cn.BeginTransaction()
        Using cmd As New SqlCommand("INSERT INTO MyTable(ItemCol) VALUES(@item)", cn, tx)
            Dim p = cmd.Parameters.Add("@item", SqlDbType.NVarChar, 100)
            For Each itm In ListBox1.Items
                p.Value = itm.ToString()
                cmd.ExecuteNonQuery()
            Next
        End Using
        tx.Commit()
    End Using
End Using

Troubleshooting and cautions:

  • Check SelectedIndex >= 0 before reading the selection.
  • For data-bound controls, use SelectedValue to store IDs and Text to store display text.
  • Always use parameters to prevent SQL injection; prefer explicit types over AddWithValue for predictable types.
  • For ASP.NET WebForms swap ComboBox for DropDownList and use DropDownList.SelectedValue/SelectedItem.Text in the same pattern above.

Recommended Answers

All 2 Replies

You could write a sql statement to instert each one, then place it in a loop.

Example:

    Dim conStr As String = "Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;"

    Dim con As New SqlConnection(conStr)
    Dim cmd As SqlCommand

    Try

        For i = 0 To ComboBox1.Items.Count

            con.Open()

            Dim sqls As String = "INSERT INTO table (column) VALUES ('" & ComboBox1.Items(i).ToString & "')"
            cmd = New SqlCommand(sqls, con)
            cmd.ExecuteNonQuery()

        Next


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

I tried by myself and got it .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.