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
Loading
Shanmughapriya MPosted Mar 5, 2008, 7:35 AM
Using the DataSet will help I think to Update the table in Backend via DataAdapter.Update(DataSet)
It is also prefreable to use Command Builder I think.
Blocked AccountPosted Mar 5, 2008, 3:45 AM
For removing this problem check these statements.
- A table. Not a product of a JOIN or a table that includes an expression--just a table.
- A primary key or unique column on the table. Converting from
an Access database does not usually bring over the PK which is needed
to construct the WHERE clause to point to the row to change.
- The column names can't contain special characters.
- The columns can't "participate" in Foreign Key constraints.
Happy Coding!!