Results 1 to 2 of 2

Thread: Dataset Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    85

    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.

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width