Data cannot be saved into DB

cc96ai

Member
Joined
Mar 8, 2006
Messages
10
Programming Experience
Beginner
i m new in vb.net
i just try to do simple insert record. I am using MSAccess for database
and I follow msdn sample code

' Visual Basic
Dim anyRow as DataRow = DatasetName.ExistingTable.NewRow
anyRow.FirstName = "Jay"
anyRow.LastName = "Stevens"
ExistingTable.Rows.Add(anyRow)

however the data only can when the application session
which means the data will gone after the application is closed
and I checked the database , the new record is not existing in DB
anyone can help?

Do i need comment ? i try AcceptChanges(), which is not working too.
 
Thx for all the reply.

I think i miss something in here,
I try to use "Visual Database Tools" to create [dataset, adapater, bindingsource].

I cannot access the oleDbDataAdapter from my coding
and I try to use "update" function
No error, but just didnt save into DB

[OR]
I must write the connection , adapter by coding, which I cannot use the generated code to insert / update data?

please help, Thx
 
There are a couple of ways to accomplish this. Fist as you have stated you can drop a oledbdatadapter onto your form and it will run the configuation wizard. From there you can specify what data to retrieve, and also generate UPDATE,INSERT and DELETE commands to return data to your database. I'm not sure what you mean by 'you can access the oledbdataadapter from code'? The default name of the adapter created by the wizard is 'OleDbDataAdapter1' then OleDb...2 and so on.

Or...
You can do all this programatically. I personally prefer this approach. You need to instantiate a OleDbDataAdapter object and set it's properties for inserting, deleteing etc

The objects you will need to Succesfully retrieve data, then send changes back to the database are..

VB.NET:
System.Data.Oledb.OledbDataAdapter
System.Data.OleDb.OledbConnection
System.Data.OleDb.OledbCommand (you'll need a few of these)
System.Data.Datatable.
Plus some means of altering the data i.e Datagrid/Bound Controls Etc...
 
if its 2005...

Me.BindingSource.EndEdit()
Me.YourTableAdapter.Update(Me.YourRecordset)

2003 is very similar... its just EndCurrentEdit I think...
 
sorry vis...

anyway I found your answer cc96ai

You need to use this command.
VB.NET:
[COLOR=#0000ff]Me[/COLOR][SIZE=2].BindingContext(DsYourDataSet, "DbTableName").EndCurrentEdit()[/SIZE]
so if I had a dataset names DsCust which was to update the Customers table it would look like

VB.NET:
Me.BindingContext(DsCust, "Customers").EndCurrentEdit()
This will allow your changes to be saved. I know it works, I had the same problem back in the day :p

Have fun!!!
 
Then you will need;

VB.NET:
Me.BindingSource.EndEdit()
Me.YourTableAdapter.Update(Me.YourRecordset)
 
Back
Top