Avatar of JackW9653
JackW9653
Flag 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
Microsoft SQL Server 2005

Avatar of undefined
Last Comment
JackW9653

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Kevin Cross

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
JackW9653

ASKER
Worked perfectly!  Thank you, Jack
Your help has saved me hundreds of hours of internet surfing.
fblack61