1,105,386 Community Members

How to Update a Data Table?

Member Avatar
vbgaya
Newbie Poster
14 posts since Aug 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have written some coding to add a new row to
an existing data table. After clicking the submit
button I get following error.
"Update requires a valid InsertCommand when
passed DataRow collection with new rows."
Please tell me what the additional code I should
add. The "add" method is called after a user
clicking the submit button. I didn't include the
HTML stuff here.
Here is the code,
<script runat="server">
sub add(obj as object, e as EventArgs)
dim objConn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\scms\database\scms.mdb")
dim objCmd as new OleDbDataAdapter("select * from services", objConn)
dim ds as DataSet = new DataSet()
objCmd.Fill(ds, "services")
dim dr as DataRow = ds.Tables("services").NewRow()
dr(1)=txtServiceName.text
dr(2)=txtSinhalaShortTag.Text
dr(3)=txtTamilShortTag.Text
dr(4)=txtEnglishShortTag.Text
ds.Tables("services").Rows.Add(dr)
objCmd.Update(ds, "services")
end sub
</script>
Please help me. I can't figure out what to do.
vbgaya.

Member Avatar
madmital
Junior Poster
119 posts since Jun 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

There's no need to use DataSet for a simple operation like this.

This should do it:

Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\scms\database\scms.mdb") : conn.Open()
Dim insCom As New OleDbCommand("INSERT INTO services (col1, col2, col3, col3) VALUES('" & txtServiceName.text & "', '" & txtSinhalaShortTag.Text & "', '" & txtTamilShortTag.Text & "', '" & txtEnglishShortTag.Text & "'", conn)
insCom.ExecuteNonQuery()
Member Avatar
Phillip
Newbie Poster
1 post since Sep 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

OK - But that does NOT answer the question
Why is the Update command giving an error

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article