Results 1 to 5 of 5

Thread: Help Constructing INSERT

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    34

    Help Constructing INSERT

    Im needing to construct an INSERT statement into a table called RUNSIZEUPCS using fields contained in hidden text boxes and a dataset to build out my record. I think I am close; however, I'm not sure about my insertsql statement since the New_UPC field is underlined in green. The following is the code that I have. Please help.
    vb.net Code:
    1. Public Sub Insert_RunsizeUPCs()
    2.         Dim Command As SqlCommand
    3.         Dim adapter As New SqlDataAdapter
    4.         Dim dsin As New DataSet
    5.         Dim i As Integer
    6.         Dim sql As String
    7.         Dim insertsql As String
    8.         Dim Code_manip As Integer
    9.         Dim New_Name As String
    10.         Dim New_UPC As String
    11.  
    12.         Get_Max_Code()
    13.         Code_manip = CInt(TxtCode.Text)
    14.         Code_manip = Code_manip + 1
    15.  
    16.         sql = "SELECT U_size FROM SIZERUNS WHERE U_run = " & TextBox2.Text
    17.         insertsql = "INSERT INTO RUNSIZEUPCS (Code,Name,U_item,U_size,U_upc) VALUES(Code_manip, New_Name, " & TextBox1.Text & ", " & dsin.Tables(0).Rows(i).Item(0) & New_UPC & ")"
    18.         'Connection = New SqlConnection(ConnectionString)
    19.  
    20.         Try
    21.             Connection.Open()
    22.             Command = New SqlCommand(sql, Connection)
    23.             adapter.SelectCommand = Command
    24.             adapter.Fill(dsin)
    25.             adapter.Dispose()
    26.             Command.Dispose()
    27.             Connection.Close()
    28.  
    29.             For i = 0 To dsin.Tables(0).Rows.Count - 1
    30.                 Code_manip = Code_manip + i
    31.                 New_Name = String.Concat(TextBox1.Text, dsin.Tables(0).Rows(i).Item(0))
    32.                 New_UPC = UPC_Code_Manip()
    33.                 adapter.InsertCommand = New SqlCommand(insertsql, Connection)
    34.                 adapter.InsertCommand.ExecuteNonQuery()
    35.             Next i
    36.         Catch ex As Exception
    37.             MsgBox("Can not open connection ! ")
    38.         End Try
    39.  
    40.  
    41.     End Sub
    Last edited by Hack; Oct 19th, 2011 at 08:52 AM. Reason: Added Highlight Tags

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help Constructing INSERT

    Moved From The CodeBank (which is for sharing code rather than posting questions )

  3. #3
    Lively Member
    Join Date
    Jun 2008
    Location
    Bayang Magiliw, Perlas Ng Silangan
    Posts
    100

    Re: Help Constructing INSERT

    Code:
    Dim New_UPC As String
    :
    :
    sql = "SELECT U_size FROM SIZERUNS WHERE U_run = " & TextBox2.Text
    insertsql = "INSERT INTO RUNSIZEUPCS (Code,Name,U_item,U_size,U_upc) 
    VALUES(Code_manip, New_Name, " & TextBox1.Text & ", " & dsin.Tables(0).Rows(i).Item(0) & New_UPC & ")"
    'Connection = New SqlConnection(ConnectionString)
    you declared the variable alright but forgot to initialized/put value in it before using. try to put something in New_UPC prior to using it.
    Last edited by Hack; Oct 19th, 2011 at 11:09 AM.

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    34

    Re: Help Constructing INSERT

    Thank you for looking at it. I figured out what was going on just a bit ago.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help Constructing INSERT

    Please post your solution. It could help others with the same or similar problem.

    Also, as a newer member I ask that if you consider your question to be resolved please help us out by pulling down the Thread Tools menu and clicking the Mark Thread Resolved menu item. That will let everyone know that you have your answer.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width