Link to home
Start Free TrialLog in
Avatar of JackW9653
JackW9653Flag for United States of America

asked on

Using Listbox Index in SQL INSERT

Hello Experts,

I have a SQL Server 2005 datbase table with the following structure:
NavCode   nvarchar(10)
FormOrder  int
FormName   nvarchar(50)

I want to insert items from a listbox in a WinForms app (VB.Net) into the table using the ordinal position of the item as the FormOrder. I can get the correct position by using the selectedindex + 1but since I'm inserting several rows at a time, I'm not selecting anything.The NavCode field is the same for all inserted records. Here's the code:

If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If

        Dim strNavCode As String = txtNavCode.Text
        Dim strForm As String = ""
        Dim intOrder As Integer =""
       
        Dim strSelect As String = ""


        Dim sqlDA As New SqlClient.SqlDataAdapter(strSelect, cn)

        Try
            If lbWorkflow.Items.Count > 0 Then
                For Each strForm In lbWorkflow.Items
                    strSelect = "INSERT INTO tblWorkflows (NavCode, FormOrder, FormName)" & _
                        " VALUES ('" & strNavCode & "','" & intOrder & "','" & strForm & "')"
                    sqlDA.SelectCommand.CommandText = strSelect
                    sqlDA.SelectCommand.ExecuteNonQuery()
                Next
            End If
        Catch err As SqlException
            MsgBox(err.Message)
        Finally
            cn.Close()
        End Try

How do I set the ordinal position of each item as FormOrder during the Insert query?
Thanks,

JackW9653
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JackW9653

ASKER

Worked perfectly!  Thank you, Jack