Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
On a form I have several text boxes binded to a bindingsource and including a datagridview.
I have populated the textboxes with database fields successfully and the datagridview displays the correct information.
I have the following code to save the information to the database but I cannot get it to work.
Private Sub SaveRecordData()                             
  dS.Tables("Recipe".Rows(RecipeBindingSource.Position).Item("Title") = txtTitle.Text
  dS.Tables("Recipe").Rows(RecipeBindingSource.Position).Item("Notes") = txtNotes.Rtf
  adapter.Update(dS, "Recipe")
end sub

I have made the ds (dataset) and adapter global variables and were used when I filled the database.
Sub FillDatabase()
  ConnectionString = gINI(iniFile, "Settings", "ConnectionString", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\RECIPE.MDB")
  Me.RecipeTableAdapter.Fill(Me.recipedataset1.Recipe)
  Dim sQuery As String = ""
  Try
   If RN = 0 Then
     sQuery = "SELECT * FROM [RECIPE] ORDER BY [TITLE], [CATEGORY]"
   Else
     sQuery = "SELECT * FROM [RECIPE] WHERE [USERID]=" & RN & " ORDER BY [TITLE], [CATEGORY]"
   End If
   Dim adapter As New OleDb.OleDbDataAdapter(sQuery, Connection)
   dS = New DataSet()
   dS.Tables.Add("Recipes")
   adapter.Fill(dS, "Recipe")
   Me.RecipeBindingSource.DataMember = "Recipe"
   Me.RecipeBindingSource.DataSource = dS
   Connection.Close()
   BindData() e.g. txtTitle.DataBindings.Add(New System.Windows.Forms.Binding("Text", RecipeBindingSource, "Title", True))
End Sub

I have tried many code examples here in order to save the data, none of which has worked.
Can anyone please help me with this?
Posted
Updated 8-Nov-10 2:52am
v4

When you create your OleDbDataAdapter, you are passing a Select query as one of the parameters. This what is used by the Fill method to populate the DataSet.

In order for the Update method to function, you need to create a corresponding Update command for the OleDbDataAdapter. The easiest way to do this is with an OleDbCommandBuilder object e.g.

Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)


See the OleDbCommandBuilder Class[^] for more information.
 
Share this answer
 
Thanks Geoff
I added the commandbuilder code (Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)) as you suggested and i have received the following error:
Nullreferenceexception was caught
Object reference not set to an instance of an object.
This error occured after the following line:-
adapter.Update(dS, "Recipe")

Any suggestions?
Thanks in anticipation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900