Home » VB.NetRSS

Changes not being saved to database

I have been searching the Forums for a solution to my problem, but I can't find anything that relates to my specific situation.  All of the postings regarding not being able to save changes to a database have had to do with a datagridview.  On forms that I do have a datagridview, my changes are saved.  But I have a form with textboxes, comboboxes, and pictureboxes bound to a bindingsource, with the records separated using a bindingnavigator.  I hope I'm describing my form correctly.  I can't seem to insert a picture of my form that might explain it better.  I tried using the code that works on the forms with a datagridview, but if I add a record, delete a record, or modify a record, the changes are not saved when I open the form back up from my main menu form.  Here is the code that I am using:

PrivateSub Back_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Back_Button.Click
    TryMe.Validate()
      Me.PlayerInfoBindingSource.EndEdit()
      Dim deletedTLRecords As DataDataSet.Player_InfoDataTable = CType(DataDataSet.Player_Info.GetChanges(Data.DataRowState.Deleted), DataDataSet.Player_InfoDataTable)
      Dim newTLRecords As DataDataSet.Player_InfoDataTable = CType(DataDataSet.Player_Info.GetChanges(Data.DataRowState.Added), DataDataSet.Player_InfoDataTable)
      Dim modifiedTLRecords As DataDataSet.Player_InfoDataTable = CType(DataDataSet.Player_Info.GetChanges(Data.DataRowState.Modified), DataDataSet.Player_InfoDataTable)
      TryIfNot deletedTLRecords IsNothingThenMe.Player_InfoTableAdapter.Update(deletedTLRecords)
        Else
          MsgBox("No records were deleted")
        EndIfIfNot newTLRecords IsNothingThenMe.Player_InfoTableAdapter.Update(newTLRecords)
        Else
          MsgBox("No records were added")
        EndIfIfNot modifiedTLRecords IsNothingThenMe.Player_InfoTableAdapter.Update(modifiedTLRecords)
          MsgBox("Updates made")
        Else
          MsgBox("No records were changed")
        EndIfCatch ex As Exception
        MsgBox("Changes were not made to the Tracking List. There may be an error with the information that was entered.")
      FinallyIfNot deletedTLRecords IsNothingThen
          deletedTLRecords.Dispose()
        EndIfIfNot newTLRecords IsNothingThen
          newTLRecords.Dispose()
        EndIfIfNot modifiedTLRecords IsNothingThen
          modifiedTLRecords.Dispose()
        EndIf
        DataDataSet.AcceptChanges()
      EndTryCatch ex As Exception
      MsgBox("Changes were not made to the Tracking List. There may be an error with the information that was entered.")
    Finally
      Main_Menu.Show()
      Me.Close()
    EndTryEndSub

When I run the application and make a change to one of the textboxes or comboboxes and click on the button, I get the following messages:  "No records were deleted", "No records were added", and "No records were changed", which means that deletedTLrecords, newTLrecords, and modifiedTLrecords do not contain any information.  If I make a change to the record, I should not be getting the message "No records were changed", but should see "Updates made".

Why does this code work for a form with a datagridview, but not on a form that just contains textboxes and comboboxes?

Here is some information that might be helpful:

I'm using Visual Studio 2008 and creating a Windows Forms Application Database is an Access database The dataset is set to Copy if Newer

I'm at a standstill at this point in being able to complete my application until I can figure out why I can't save from this form.  Hopefully someone can help me out.

Thanks,
John

 

13 Answers Found

 

Answer 1

John,

Why don't you do it like all others, I don't know if you have a related datataset (I don't think so) then all what you are doing is done in the Update method of the dataadapter.

So in fact can all your code  be replaced by:

Me.PlayerInfoBindingSource.EndEdit()
Me.Player_InfoTableAdapter.Update(DataDataSet.Player_Info)

The dataadapter selects itself the modificated rows.

Can you try this and see what is the result?

 


Success
Cor
 

Answer 2

Cor,

I did what you suggested, but that didn't help.  You asked if I had a related dataset and I believe I do.  The Player_Info table has relationships to several other tables in my dataset (one-to-many connections).  Does this mean that I would have to run  my initial code  for each bindingsource  and tableadapter that I have loaded on my form?  If so, do I have to run the code in a particular order?

John

 

Answer 3

Can you try it like this

Try
Me.PlayerInfoBindingSource.EndEdit()
Me.Player_InfoTableAdapter.Update(DataDataSet.Player_Info)
Catch ex as Exception
 messagebox.show(ex.ToString)
end Try

And remove (comment out) all the other code  in that method?

Be aware that I will watch football (soccer) so I wont help you before tomorrow so somebody else has to do that then.

 

 


Success
Cor
 

Answer 4

Cor,

I tried that too.  Nothing saved  and no message  came up, so it didn't call an exception.

I don't know if this has anything to do with it, but before I get to my form, I have another form  that comes up to allow the user to filter the data.  I've been doing my testing by not selecting anything to filter.  Here is the code  I have on the filter form when the user continues on to the form I'm having trouble saving data (Tracking_List):

If constructString() = ""Then
      Tracking_List.PlayerInfoBindingSource.ResetBindings(True)
    Else
      Tracking_List.PlayerInfoBindingSource.Filter = constructString()
    EndIf

So if I don't select anything to filter, then it calls the ResetBindings of the BindingSource.  Could this be the problem?  At this point I'm grasping at straws.  The Tracking_List form is basically the most important form to my application.  So if I can't get changes to the form saved, I'm dead in the water.

Have fun watching football - I'm assuming you are referring to the World Cup final.

Thanks,
John

 

Answer 5

Cor,

Here is something else I found that might help.  On the Tracking_List form  I have a TableAdapterManager (can't remember why I put one in there).  When I look at the properties, there are some TableAdapters listed that have (none) selected as the property (Player_InfoTableAdapter is associated with Player_InfoTableAdapter).  When I try to change  the (none) to the proper TableAdapter I get a message  that says "The selected TableAdapter must use the same connection as the TableAdapterManager."  So could the TableAdapterManager be what's causing the problem?

John 

 

Answer 6

Put this after the EndEdit

If DataDataSet.Player_Info.GetChanges IsNot Nothing then MessageBox.Show("there is something to update")

Otherwise you have probably done something somewhere which has not much to do with the code  you show.

 


Success
Cor

 

 

Answer 7

Cor,

added  your code.  Here is what I discovered.  When I make a change  to a textbox or combobox and click  the button, I do not get the "there is something to update" message.  But, if I make a change to a textbox or combobox and then use the  bindingnavigator  to go to the next record, then click the button, I get the message.  Either way, the data does not save.

To recap, here is the code  I am using:

TryMe.PlayerInfoBindingSource.EndEdit()
      If DataDataSet.Player_Info.GetChanges IsNotNothingThen MessageBox.Show("there is something to update")
      Me.Player_InfoTableAdapter.Update(DataDataSet.Player_Info)
    Catch ex As Exception
      MessageBox.Show(ex.ToString)
    EndTry

This is so very confusing.  I didn't think I was doing anything very complicated.  Obviously I am somewhere that is causing this problem.

John

 

Answer 8

Cor,

I've been doing some more testing and I may be getting closer to isolating the problem.  I mentioned that I have a form  to filter the data before getting to the Tracking_List form.  The testing I did did not filter the data.  Therefore, the  code  - Tracking_List.PlayerInfoBindingSource.ResetBindings(True) is run  before the Tracking_List form is opened.  Here are some of the things that I found:

If I make a change  to the first record  and close  the form, I do not get your message  "there is something to update" and the change is not saved. If I make a change to the first record and go to the next record using the bindingnavigator, then close the form, I get the message "there is something to update", but the change is not saved. If I go to the second record using the bindingnavigator  and make a change, then close the form, I get the message "there is something to update".  When I reopen the form and go to the second record, the change was saved.  But, when I close the form I get the message "there is something to update" even though I didn't make another change. Every time I reopen the form after that, if I close the form from the first record, I do not get the message "there is something to update".  But if I reopen the form and go to any other record, when I close the form I get the message "there is something to update" even though I do not make any changes. If I close the application  and then open  it up again and open the form and go to the first record and then close the form, I do not get the message "there is something to update".  But if I open the form and go to the second record, I get the message "there is something to update" even though I did not make any changes.

Something is definitely going on with the first record and my ability to save  changes.  Also, once it does save a change to another record, the message "there is something to update" keeps showing up, even if I do not make any further changes to any of the records.  I am using your most recent code suggestion of:

TryMe.PlayerInfoBindingSource.EndEdit()
      If DataDataSet.Player_Info.GetChanges IsNotNothingThen MessageBox.Show("there is something to update")
      Me.Player_InfoTableAdapter.Update(DataDataSet.Player_Info)
    Catch ex As Exception
      MessageBox.Show(ex.ToString)
    EndTry
Thanks,
John
 

Answer 9

Cor,

The strangest thing just happened.  While waiting to hear back  from you I decided to work  on two PictureBoxes I have on the form  that didn't seem to be working properly.  In the process of making them work, suddenly I can save  changes!!!  All 5 of the scenarios I described in my previous post now work as they should.  The PictureBoxes (and the two textboxes  I have that store the path of the images) must have been causing the problem  all along.  Maybe they were not correctly  binding back to the datatable.  It's funny how sometimes you think a problem is in one area (the saving code) and all along it was a problem with something else.

I'll let you know if I encounter any other problems with saving data.  I haven't tested adding a new record  or deleting a record yet.  But as of now I think I have solved my problem (with your help of course).

Thanks!
John

 

Answer 10

Do you have Option Strict Off or do you use Reflection?

Both can be a reason of this kind of errors.

 

Answer 11

Hi reichard2,

I saw in your original post that you set the "Copy to Output Directory" property to "Copy if newer", however, "Copy if newer" option is not recommended.

"The database  file is copied from the project directory to the bin directory the first time the project is built. Every subsequent time you build the project, the Date Modified property of the files is compared. If the file in the project folder is newer, it is copied to the bin folder, replacing the file currently there. If the file in the bin folder is newer, then no files are copied. This setting persists any changes made  to the data during run  time, meaning every time you run your application  and save  changes to the data, those changes are visible the next time you run your application.

Caution:

The database file can change  even when no changes are made to the data. Simply opening a connection (for example, expanding the Tables node in Server Explorer) on a data file can mark it as newer. Because of this unpredictable behavior, we recommended that you do not use this option."

I am glad to hear that you have solved your problem. Please remember to mark the replies as answer if they help. Thanks.

Best regards,
Alex Liang

 

Answer 12

Alex,

Then which option do you suggest I use?  Do Not Copy or Copy Always?

Thanks,
John

 

Answer 13

Hi John,

That deponds on your requirement. If you set it to "Copy always", the database  file is copied from the project directory to the bin directory every time you build your application. Every time you build your application  and save  changes to the data, those changes are overwritten when the original file is copied to the bin directory, replacing the copy that you just changed. You do not see the updated data the next time you run  your application. Any changes made  to the data file in the output folder will be overwritten the next time you run the application. If you set it to "Do not copy", the file is never copied or overwritten by the project system. Because your application creates a dynamic connection string that points to the database file in the output directory, this setting works  only for local database files when you manually copy the file yourself. You must manually copy the file to the output directory after setting to Do not copy.

Best regards,
Alex Liang

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter