Solved

insert into sql table from list

Posted on 2014-10-02
4
119 Views
Last Modified: 2014-10-03
I need to insert into sql table list of strings ("A", "B", "C" etc) as parameters
this is my code:

  Dim strSQLSubItem As String
Try
            Dim strConn As String = "Data Source=***;Initial Catalog=***;Persist Security Info=True;User ID=***;Password=***"
            Dim Connection As SqlConnection = New SqlConnection(strConn)

            Connection.Open()
Dim v As Integer = Integer.Parse(txtCountPerItem.Text)

            Dim ItemId As Integer = 3

strSQLSubItem = "Insert into InventoryVendorItem_SubItem(ItemId,VendorSubItemDefinition) Values(@ItemId,@VendorSubItemDefinition )"
                                    Using cmdInsertSubItem As New SqlCommand(strSQLSubItem, Connection)
                With cmdInsertSubItem

                    .CommandText = strSQLSubItem
                    .Connection = Connection
                    .CommandType = CommandType.Text

                    .Parameters.Add("@VendorSubItemDefinition", SqlDbType.VarChar)
                    .Parameters.Add("@ItemId", SqlDbType.Int)

 Dim SubItems As New List(Of String)


                    SubItems.Add("A")
                    SubItems.Add("B")
                    SubItems.Add("C")
                    SubItems.Add("D")
                    SubItems.Add("E")
                    SubItems.Add("F")
                    SubItems.Add("G")
                    SubItems.Add("H")
                    SubItems.Add("I")
                    SubItems.Add("J")

                    For i As Integer = 0 To v - 1


                                                .Parameters.Add("@VendorSubItemDefinition", SqlDbType.VarChar).Value = TryCast(SubItems.Item(i), String)
                        .Parameters.Add("@ItemId", SqlDbType.Int).Value = ItemId

                        .ExecuteNonQuery()

                    Next

                End With
            End Using
            Connection.Close()


        Catch Ex As Exception

        End Try

Open in new window


I cannot insert into sql table.

Please, help.
0
Comment
Question by:msout
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40359112
You keep adding parameters to the Command object in the loop. Remove the .Parameters.Add() statements on lines 19 and 20. Then change the loop code (lines 36-44) to:
                    .Parameters.Add("@ItemId", SqlDbType.Int).Value = ItemId
                    .Parameters.Add("@VendorSubItemDefinition", SqlDbType.VarChar)
                    For i As Integer = 0 To v - 1
                        .Parameters("@VendorSubItemDefinition").Value = SubItems.Item(i)
                        .ExecuteNonQuery()
                    Next

Open in new window

Note that I can't make sure this works obviously without access to your database but this should be the general idea.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40359133
quick test with local sqlexpress database was successful:
screen capture of inserted records
0
 

Author Closing Comment

by:msout
ID: 40359635
Great!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now