Groups | Blog | Home
all groups > dotnet ado.net > april 2007 >

dotnet ado.net : insert data into Access database (could it be AutoNumber?)


Scott Starker
4/19/2007 7:56:00 AM
This is a question from a Newbie. Although it executes correctly there is no
new record added to the table (which isn't what I want :). Could it be that
the table has an "AutoNumber" field (RecordID)? I have tried (among other
things):
SQLString = "SELECT * FROM TypeNames"
DBAdapter2 = New OleDbDataAdapter(SQLString, DBConnection)
DBDataSet2 = New DataSet()
DBAdapter2.Fill(DBDataSet2, "TypeNames")
DBTable3 = DBDataSet2.Tables("TypeNames")
foundRow2 = DBTable3.NewRow()
foundRow2("TypeCode") = cmbTypeLookup.Text
foundRow2("TypeDomain") = txtDomain.Text
foundRow2("TypeNameSpan") = txtTypeName.Text
' New row.
DBTable3.Rows.Add(FoundRow2)

I assume that the "AutoNumber" would get inserted automatically but I may be
wrong. Could that be it? How do I do this? Thanks.

Scott

Scott Starker
4/19/2007 11:06:30 AM
As you suggested I used
DBAdapter2.Update(DBDataSet2, "TypeNames")
at the end of this code and it gave me
"Update requires a valid InsertCommand when passed DataRow collection with
new rows."

I thought "InsertCommand" was for another way to insert a row...

Scott

[quoted text, click to view]

Paul Clement
4/19/2007 12:19:45 PM
[quoted text, click to view]

¤ This is a question from a Newbie. Although it executes correctly there is no
¤ new record added to the table (which isn't what I want :). Could it be that
¤ the table has an "AutoNumber" field (RecordID)? I have tried (among other
¤ things):
¤ SQLString = "SELECT * FROM TypeNames"
¤ DBAdapter2 = New OleDbDataAdapter(SQLString, DBConnection)
¤ DBDataSet2 = New DataSet()
¤ DBAdapter2.Fill(DBDataSet2, "TypeNames")
¤ DBTable3 = DBDataSet2.Tables("TypeNames")
¤ foundRow2 = DBTable3.NewRow()
¤ foundRow2("TypeCode") = cmbTypeLookup.Text
¤ foundRow2("TypeDomain") = txtDomain.Text
¤ foundRow2("TypeNameSpan") = txtTypeName.Text
¤ ' New row.
¤ DBTable3.Rows.Add(FoundRow2)
¤
¤ I assume that the "AutoNumber" would get inserted automatically but I may be
¤ wrong. Could that be it? How do I do this? Thanks.

Are you calling the Update method for the DataAdapter somewhere in your code?


Paul
~~~~
Paul Clement
4/20/2007 9:00:25 AM
[quoted text, click to view]

¤ As you suggested I used
¤ DBAdapter2.Update(DBDataSet2, "TypeNames")
¤ at the end of this code and it gave me
¤ "Update requires a valid InsertCommand when passed DataRow collection with
¤ new rows."
¤
¤ I thought "InsertCommand" was for another way to insert a row...

No, it's what the DataAdapter uses to perform the INSERT operation. You've already created the
SelectCommand, which was generated when you instantiated the DataAdapter with your SQLString, but
you also need to assign an InsertCommand value if you're going to perform this operation as well.


Paul
~~~~
AddThis Social Bookmark Button