Solved

insert into sql table from list

Posted on 2014-10-02
4
117 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

705 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

15 Experts available now in Live!

Get 1:1 Help Now