Data Adapter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harmansodhi03
    New Member
    • Aug 2007
    • 18

    Data Adapter

    Dear All,

    I have wriiten the following code to add a new row in author table. But it is generating an error("Update requires a valid InsertCommand when passed DataRow collection with new rows.")
    Please help me on this.


    Dim con As New SqlConnection(" Data Source=.;Initia l Catalog=student ;Integrated Security=True")
    Dim cmd As New SqlCommand("Sel ect * from author", con)
    con.Open()
    Dim adp As New SqlDataAdapter( cmd)
    Dim ds As New DataSet()
    Dim dt As DataTable
    Dim dr As DataRow
    adp.Fill(ds, "author")
    dt = ds.Tables("auth or")
    dr = dt.NewRow()
    dr(0) = TextBox1.Text
    dr(1) = TextBox2.Text
    dr(2) = TextBox3.Text
    dt.Rows.Add(dr)
    adp.Update(ds, "author")
    con.Close()

    Regards

    Harman
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Your dataadapter uses a select command, consequently it errors when you try to update. You need to supply an sql update query. HTH.
    Originally posted by harmansodhi03
    Dear All,

    I have wriiten the following code to add a new row in author table. But it is generating an error("Update requires a valid InsertCommand when passed DataRow collection with new rows.")
    Please help me on this.


    Dim con As New SqlConnection(" Data Source=.;Initia l Catalog=student ;Integrated Security=True")
    Dim cmd As New SqlCommand("Sel ect * from author", con)
    con.Open()
    Dim adp As New SqlDataAdapter( cmd)
    Dim ds As New DataSet()
    Dim dt As DataTable
    Dim dr As DataRow
    adp.Fill(ds, "author")
    dt = ds.Tables("auth or")
    dr = dt.NewRow()
    dr(0) = TextBox1.Text
    dr(1) = TextBox2.Text
    dr(2) = TextBox3.Text
    dt.Rows.Add(dr)
    adp.Update(ds, "author")
    con.Close()

    Regards

    Harman

    Comment

    • deric
      New Member
      • Dec 2007
      • 92

      #3
      You said you want to add a new row, so you need to use InsertCommand of the SqlDataAdapter.

      And, your SqlCommand should be an Insert statement, not Select statement. There's a sample on the link I provided you...

      Comment

      Working...