dcsimg
Results 1 to 4 of 4

Thread: Issues saving data from datagridview to database

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    2

    Issues saving data from datagridview to database

    Hi All, I'm new to this forum and straight off the bat I start bothering you guys with an issue, or as we call it at work a "challenge".

    I'm quite new to Visual Basic and mainly have thought myself to use Visual Basic Express 2010, so I have limited knowledge about the behind the database connection (mainly use the wizard an connection functions of the program) so bare with me when i try to explain my issue.

    The project I'm working on is a planning tool i'm designing to use at work. The mainform consist of 4 datagridviews filled from an access database (.mdb). The database contains multiple columns including 1 keynumber and a priority column. The 4 datagridviews are filtered on either priority 1, 2, 3 or 4. I want to be able to change the priority in of the database entries to move them either up or down using 4 sets of buttons. Now I use the following button code to change the value in the priority column and safe the data from the datagrid to the database. (datagridview number and priority value are different for every button)

    Code:
     DataGridView1.CurrentRow.Cells(0).Value = "2"
            Me.Validate()
            Me.BindingSource1.EndEdit()
            Me.BindingSource2.EndEdit()
            Me.BindingSource3.EndEdit()
            Me.BindingSource4.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DatabaseDataSet)
            Me.DatabaseDataSet.AcceptChanges()
    The problem is that on screen everything seems to work smoothly and I'm able to shift any entry from datagrid to datagrid. However, if I then close the application and re-open it only the changes made to the first line of the database (keynumber 1) are actually saved, all other entries remain unchanged. To figure out where the problem lies I have tried manually changing the priority column in the datagridview and saving it using it with the following code (mainly the same code as before)
    Code:
    Me.Validate()
            Me.BindingSource1.EndEdit()
            Me.BindingSource2.EndEdit()
            Me.BindingSource3.EndEdit()
            Me.BindingSource4.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DatabaseDataSet)
            Me.DatabaseDataSet.AcceptChanges()
            MsgBox("Update Succesfull")
    Changing the priority in this way does safe all the changes when re-opening the database.

    I'm quite puzzled by this because I have no idea why the changes are only saved to the first row and not to any other when using the button but are changed by manually editing the values and saving it with the same code.

    I hope anyone on this forum can help me solving this issue and helps me to understand what is happening.

    I'm happy to supply any extra information/code if needed.

    Greetings Pascal.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Issues saving data from datagridview to database

    Challenge? Pfft!

    Are you accessing a database included in the project? If so click on it in the Solution Explorer and cast your eyes down to the Properties window and where it says Copy Always, change to Copy If Newer (something like that anyway, I don't have VS open at the moment. The problem you're having is that every time you run the program the original database is being copied to the build folder so any changes are obviously eradicated.

    If that's not the case then we do have a problem so get back to us.

    By the way. It's extremely unlikely that you need the EndEdit commands if you're using DGVs.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    2

    Re: Issues saving data from datagridview to database

    Hi dunfiddlin thanks for your reply.

    Yes the database is included in the project and as you suggested I have changed the copy settings. However its unlikely that (and I have tested it) it would affect the issue since I'm encountering the problems I'm describing after building the application and running the .exe file. (maybe should have mentioned that).

    Hope you or anyone has any further ideas.

    Thanks.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,363

    Re: Issues saving data from datagridview to database

    Put any code that retrieves data or updates data in a Try/Catch block, a lot of database errors wont show up without it. It should make it easier to track down the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width