2
Reply

Inserting Record using Data Adepter.Update() Command

Chintan Shah

Chintan Shah

Mar 4 2008 3:33 PM
2.2k
I created a Table in SQL Server 2005 and now i am creating an application to directly manipulate this table so i wrote this code and the commented part works successfully but part before it is not running it gives following Exception

"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."


Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim dr As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM dbo.Categories", SqlCon)
        dr.InsertCommand = New SqlCommand("INSERT INTO dbo.Categories VALUES('@CategoryName',@CategoryID)", SqlCon)
        dr.InsertCommand.Parameters.Add("@CategoryName", SqlDbType.VarChar, 15, "CategoryName")
        dr.InsertCommand.Parameters.Add("@CategoryID", SqlDbType.Int, 6, "CategoryID")
        Dim DT As New DataTable
        dr.Fill(DT)
        Dim rw As DataRow = DT.Rows(0)
        rw(0) = TextBox2.Text
        rw(1) = TextBox1.Text
        dr.Update(DT)


        'Dim cmd As SqlCommand = New SqlCommand("INSERT INTO dbo.Categories(CategoryName,CategoryID) " + "VALUES('" + TextBox2.Text + "'," + TextBox1.Text + ")", SqlCon)
        'cmd.Parameters.Add("@CategoryName", SqlDbType.VarChar, 15, "CategoryName")
        'cmd.Parameters.Add("@CategoryID", SqlDbType.Int, 6, "CategoryID")
        'cmd.ExecuteNonQuery()
End Sub

Answers (2)