Problem with adding new records in Database while saving changes on exisiting ones works!  
Author Message
Christos25





PostPosted: Visual Basic General, Problem with adding new records in Database while saving changes on exisiting ones works! Top

Hello,

I'm developing an application which handles an Access database and I can't save a new record in any table of it though I can save changes that I do in it!

I am using the following code to add a new record:

Me.HUMAN_RESOURCESBindingSource.AddNew()

and I'm using the following code to save my changes:

Me.Validate()

Me.HUMAN_RESOURCESBindingSource.EndEdit()

Me.HUMAN_RESOURCESTableAdapter.Update(Me.DATADataSet.HUMAN_RESOURCES)

which is exactly the same that I use to save my changes when I edit a record.

While the code works perfectly on saving my changes to existing records, it doesn't work when I add a new one! I also tried to add a new record with the BindingNavigator but I get the same results!

Thanks for any assistance.



Visual Basic14  
 
 
PaulYuk_MS





PostPosted: Visual Basic General, Problem with adding new records in Database while saving changes on exisiting ones works! Top

Your code looks just fine for adding new rows to a DataSet / DataTable. We use almost the exact same code in the Movie Collection Starter Kit sample.

One quick thing to check, do you have a primary key set both in your Database table and in your Dataset table Without it, you might not be able to add rows.

Also, do you create the id/pk value for your new row or does the database do that for you


 
 
Christos25





PostPosted: Visual Basic General, Problem with adding new records in Database while saving changes on exisiting ones works! Top

Yes I do have a primary key in both database table and dataset table.

It is an Autonumber field and the database completes it automaticaly.

I know that it works ok since I can see it in my datagrid and I can verify it is unique.

One strange thing is that although the new row isn't added the pk increases every time I run my application and try to insert a new one. I also have checked that the table hasn't got any new rows through Access, thinking that perhaps for some reason the new rows are added but aren't visible through my application.It doesn't happen.

I'm thinking that probably the problem is with the connection to my database since it is password protected and uses an also password protected workgroup information file. But I log on to it through my application with the account that has full rights to it!

Maybe it has something to do with the "OLEdb" options in the "Advanced Properies" of the database connection window of VS. I've searched through the net to find what everyone of these does but I couldn't find any info!

Thanks for your post.


 
 
Christos25





PostPosted: Visual Basic General, Problem with adding new records in Database while saving changes on exisiting ones works! Top

Sorry, my fault.

I've checked again the database through Access and feagured that the new rows ARE added (I was checking a wrong copy of the database before!)! But again no new rows are visible through my application. Only the ones I enter through Access are visible, and the pk increases normally for every new row I enter through Access and my application so it's not the problem!


 
 
PaulYuk_MS





PostPosted: Visual Basic General, Problem with adding new records in Database while saving changes on exisiting ones works! Top

Are you willing to share a simplified project and database that repro's the problem I would have a look at it.

It's hard to tell what could be causing these rows not to be shown or refreshed after an update.


 
 
Christos25





PostPosted: Visual Basic General, Problem with adding new records in Database while saving changes on exisiting ones works! Top

Actually the problem is solved!

I tried several ways , by replacing my original code, to solve it but none of them worked.

Then I replaced them with first code and it works fine!

I couldn't tell why it didn't work from the begining. Maybe VB was stuck (it has caused me some more unexplained problems until now!)

Anyway, thanks for all the time you shared for my problem.

Best Regards,

Christos


 
 
PaulYuk_MS





PostPosted: Visual Basic General, Problem with adding new records in Database while saving changes on exisiting ones works! Top

Hi Christos,

Thanks for the heads up that you found a solution. If you figure out why VB was stuck pls let us know. We want to make the product better.

I'm glad it's working :)

thanks,

Paul