Results 1 to 8 of 8

Thread: Updating Database through dataset OleDbDataAdapter [Solved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66

    Resolved Updating Database through dataset OleDbDataAdapter [Solved]

    Hi,

    I have a problem with updating my database with this code.

    VB Code:
    1. Private moDS As DataSet

    VB Code:
    1. Private Sub DataUpdate()
    2.         Dim oAdapter As OleDbDataAdapter
    3.         Dim oBuild As OleDbCommandBuilder
    4.         Dim oDR As DataRow
    5.         Dim strSQL As String
    6.         Dim strID As String
    7.         Dim strConn As String
    8.  
    9.         ' Get Primary Key From List Box
    10.         strID = CType(lstLeden.SelectedItem, PDSAListItemNumeric).ID.ToString()
    11.  
    12.         ' Find Row To Update
    13.         oDR = moDS.Tables("Gegevens").Rows.Find(CInt(strID))
    14.  
    15.         ' Begin the editing process
    16.         oDR.BeginEdit()
    17.  
    18.         ' Load new data into row
    19.         oDR("Voorletters") = txtVoorletters.Text
    20.         oDR("Tussenvoegsel") = txtTussenvoegsel.Text
    21.         oDR("Achternaam") = txtAchternaam.Text
    22.         oDR("Aanspreeknaam") = txtAanspreeknaam.Text
    23.         oDR("Geslacht") = txtGeslacht.Text
    24.         oDR("Adres") = txtAdres.Text
    25.         oDR("Postcode") = txtPostcode.Text
    26.  
    27.         ' End the editing process
    28.         oDR.EndEdit()
    29.  
    30.         Try
    31.             ' Get Connection String
    32.             strConn = ConnectionString()
    33.             ' Build SQL String
    34.             strSQL = "SELECT *, Achternaam + ', ' + Voorletters AS FullName FROM Gegevens"
    35.             ' Create New DataAdapter
    36.             oAdapter = New OleDbDataAdapter(strSQL, strConn)
    37.             ' Create CommandBuild from Adapter
    38.             ' This will build INSERT, UPDATE and DELETE SQL
    39.             oBuild = New OleDbCommandBuilder(oAdapter)
    40.  
    41.  
    42.             ' Get Update Command Object
    43.             oAdapter.UpdateCommand = oBuild.GetUpdateCommand()
    44.  
    45.             ' Submit UPDATE through Adapter
    46.             oAdapter.Update(moDS, "Gegevens")
    47.             ' Tell DataSet changes to data source are complete
    48.             moDS.AcceptChanges()
    49.  
    50.             ' Reload the list box
    51.             ListLoad()
    52.  
    53.         Catch oException As Exception
    54.             MessageBox.Show(oException.Message)
    55.  
    56.         End Try
    57.     End Sub

    When i run this piece of code it says that "Automatically Generate an SQL Statement is not supported"

    Why is that, and what can i do about it?
    Last edited by digita; Nov 22nd, 2004 at 09:02 AM.

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    have you try instantiating your dataset?

    VB Code:
    1. Private moDS As New DataSet()

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66
    yes, i did that but that didn't work,

    I think that it is something that the selectcommand doesn't provide information about unique columns, or something like that

  4. #4
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    Originally posted by digita
    yes, i did that but that didn't work,

    I think that it is something that the selectcommand doesn't provide information about unique columns, or something like that
    yah...that's it..

    you should provide primary key to update your Gegevens table.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66
    where should i do that? i mean where in the code?

    because i already have one assigned in this piece

    VB Code:
    1. Private Sub DataSetCreate()
    2.         Dim oAdapter As OleDbDataAdapter
    3.         Dim strSQL As String
    4.         Dim strConn As String
    5.  
    6.         ' Get Connection String
    7.         strConn = ConnectionString()
    8.  
    9.         ' Build SQL String
    10.         strSQL = "SELECT *, Achternaam + ', ' + Voorletters AS FullName FROM Gegevens"
    11.  
    12.  
    13.         moDS = New DataSet
    14.         Try
    15.             ' Create New Data Adapter
    16.             oAdapter = New OleDbDataAdapter(strSQL, strConn)
    17.             ' Fill DataSet From Adapter and give it a name
    18.             oAdapter.Fill(moDS, "Gegevens")
    19.             ' Create a Primary Key
    20.             With moDS.Tables("Gegevens")
    21.                 .PrimaryKey = New DataColumn() _
    22.                  {.Columns("Id")}
    23.             End With
    24.  
    25.         Catch oExcept As Exception
    26.             MessageBox.Show(oExcept.Message)
    27.  
    28.         End Try
    29.     End Sub

    And this code is executed on form load, so i guess tha there already is an primary key

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66
    the actual errorcode is:

    "Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information"

    What can I to make that SelectCommand return key column information?

  7. #7
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    Originally posted by digita
    the actual errorcode is:

    "Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information"

    What can I to make that SelectCommand return key column information?
    edit your table in your SQL database. put the yellow key on your column you want to be unique. i.e. your id...

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    66
    Thanks That did it, now running smooth

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