add record to table

Discussion in 'Case Modding' started by mhh12, Jan 22, 2011.

  1. mhh12

    mhh12

    Joined:
    Jan 22, 2011
    Messages:
    1
    This code is trying to add one row of five columns to a table called KDistrictBridge. It doesn't do it.

    I get a message about declaring a scalar variable @EeID. What about all the other variables? Is @EeID sczalar because I designated it as primary key? What else is wrong with this code? Many thanks for your help.

    ' Ddataset is defined at start of Sub Button1_Click()

    Dim strKDisctBridge As String = "SELECT * FROM KDisctBridge WHERE EeID=@EeID "
    Dim tblKDisctBridge As New SqlCommand(strKDisctBridge, Ddataset)
    Dim sqlInsert As String = "INSERT INTO KDistrictBridge " & _
    "(EeID,DdID,District,CcID,DisctSet) VALUES " & _
    "(@EeID,@DdID,@District,@CcID,@DisctSet)"
    Try
    ' make the data adapter
    Dim da As New SqlDataAdapter
    da.SelectCommand = New SqlCommand(strKDisctBridge, Ddataset)
    ' make and fill the KDisctBridge dataset
    Dim ds As New DataSet
    da.Fill(ds, "KDisctBridge")
    ' get the datatable out of the Ddataset list of tables
    Dim dt As DataTable = ds.Tables("KDisctBridge")
    ' add a row to the table
    Dim newrow As DataRow = dt.NewRow
    newrow("EeID") = 1
    newrow("DdID") = 2
    newrow("District") = "Able"
    newrow("CcID") = 3
    newrow("DisctSet") = "Baker"
    dt.Rows.Add(newrow)
    ' insert the new row
    ' create the command
    Dim insertCmd As New SqlCommand(sqlInsert, Ddataset)
    insertCmd.Parameters.Add(New SqlParameter("", "EeID"))
    insertCmd.Parameters.Add(New SqlParameter("", "DdID"))
    insertCmd.Parameters.Add(New SqlParameter("", "District"))
    insertCmd.Parameters.Add(New SqlParameter("", "CcID"))
    insertCmd.Parameters.Add(New SqlParameter("", "DisctSet"))
    da.InsertCommand = insertCmd
    da.Update(ds, "KDisctBridge")
    Catch ex As SqlException
    MsgBox("error at catch" & ex.ToString())
    Console.WriteLine("Error: " & ex.ToString())
    Finally
    Ddataset.Close()
    End Try
    End Using
     
    mhh12, Jan 22, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joachim Krais
    Replies:
    2
    Views:
    15,367
    Andre Beck
    Nov 23, 2003
  2. John Ramsden
    Replies:
    0
    Views:
    1,047
    John Ramsden
    Jul 24, 2004
  3. zher
    Replies:
    2
    Views:
    9,181
  4. Darren Green
    Replies:
    1
    Views:
    7,139
    Walter Roberson
    Mar 19, 2005
  5. reply@newsgroup.please
    Replies:
    2
    Views:
    1,162
    Bruce T. Berger
    Dec 1, 2003
Loading...

Share This Page