-
Dataset Question
Hello,
I am updating an access database using a dataset. My question is... how come I can add a record to a table that doesn't have a primary key defined, but I cannot update a specified record? Here is the code that I am using...
Code:
Dim cn As New OleDbConnection(AppSettings("cnString"))
Dim da As New OleDbDataAdapter("SELECT * FROM tblEmailAddresses", cn)
Dim ds As New DataSet()
Dim dv As DataView
Dim dr As DataRow
da.Fill(ds, "tblEmail")
'check if the customer's email has already been added
dv = ds.Tables("tblEmail").DefaultView
dv.RowFilter = "Email=" & "'" & strEmail & "'"
If dv.Count > 0 Then
'this email was already added
dv(0).Item("Promotions") = blnAgree
dv(0).Item("Name") = strName
Else
dr = ds.Tables("tblEmail").NewRow()
dr("Name") = strName
dr("Email") = strEmail
ds.Tables("tblEmail").Rows.Add(dr)
End If
'update the Email Address table in the database
cn.Open()
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim cmd As New OleDbCommandBuilder(da)
da.Update(ds, "tblEmail")
The above code works perfectly if a primary key is defined in the access table. I also tried substituting MissingSchemaAction.AddWithKey with MissingSchemaAction.Add and this didn't work either.
-
The DataAdapter uses a primary key to make updates. If you dont have a primary key, you can supply your own updating logic in a commandBuilder and pass it to the DataAdapter.