none
Saving Updates to Data Table using Binding Navigator Save Item Button

    Question

  • Hello, I am trying to understand how the binding navigator buttons are actually working.

    I started by creating a table called "Students" in MS Access,

    I then "dragged and dropped" that table onto my VB form after importing the data connection.

    When I debug, I click the add new (the yellow plus "+") then I type info into my fields (700_Number, First_Name, MI, Last_Name). (700_Number=Primary Key)

    After I am finished typing into the four text boxes, I click save.  If I end my program then start the debugger again, the record is still there...it goes away only if I change some of the code anywhere in my program.

    Also, I am trying to use the record I just put into my database for another part of my program.  I use this line of code:

     MsgBox(frmAddStudent.DrByrdsTaskListAppDBDataSet.Tables("Students").Rows(0))

    This is just to see if what I am trying to put into my database is actually making it there.  Every time I run, this message box turns up blank.

    Any advice to solve this and/or understanding how to properly use databases in program would be greatly appreciated!  Thank you so much!

    -Mike

    Thursday, June 07, 2012 6:20 PM

Answers

  • hard saying not seeing it, but I would guess that for the first issue you had no records in the table to begin with.  You added a new record in teh debug session and then successfully saved it.  Then when you opened the debugger a second time it performed a select and found one record and automatically displayed it.  The select was probably done the first time only there were no records to retrieve so the controls were blank.  Does this sound right?

    As for the second issue - not sure but it might be that you are referencing the shared DrByrdsTaskListAppDBDataSet class (by calling it by its class name) rather than an instance of the dataset.  You might do better by asking the form's binding manager or the underlying currency manager for its current item (what is currently bound to the controls).

    Thursday, June 07, 2012 6:59 PM

All replies

  • hard saying not seeing it, but I would guess that for the first issue you had no records in the table to begin with.  You added a new record in teh debug session and then successfully saved it.  Then when you opened the debugger a second time it performed a select and found one record and automatically displayed it.  The select was probably done the first time only there were no records to retrieve so the controls were blank.  Does this sound right?

    As for the second issue - not sure but it might be that you are referencing the shared DrByrdsTaskListAppDBDataSet class (by calling it by its class name) rather than an instance of the dataset.  You might do better by asking the form's binding manager or the underlying currency manager for its current item (what is currently bound to the controls).

    Thursday, June 07, 2012 6:59 PM
  • I had another thought - how are you showing frmAddStudent?  Is it the start form of your app?  if not then are you doing something like:

    Dim frm as new frmAddStudent
    frm.Show()

    If you are then you need to ask the instance of the form, not the default form.  Again, this is a case of using the shared instance of the form (by using the form's class name) rather than the instance (which in my example would be "frm").  Here's a post I did regarding that topic:  http://codemidden.wordpress.com/2012/02/02/why-the-default-form-instance-does-more-harm-than-good/

    Also, do you know how to set breakpooints and step through code?  Doing this you can stop at any given line of code and see what the properties are for any given object.  Ask if you need help doing this.

    Thursday, June 07, 2012 7:04 PM
  • Thanks a lot!  I don't know how to ask for the binding navigator item?  Also, I am not making the frmAddStudent as a "new" form.  I am simply saying frmAddStudent.show().  Do I still need to do instance and not default?

    Thursday, June 07, 2012 7:11 PM
  • Also, when I click the save button on the binding navigator, it preforms these lines of code:

     
     Private Sub StudentsBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles StudentsBindingNavigatorSaveItem.Click
    
            Try
                Me.Validate()
                Me.StudentsBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.DrByrdsTaskListAppDBDataSet)
                MsgBox("Update Successful")
    
            Catch ex As Exception
    
                MsgBox("Update Failed" & ex.ToString)
    
            End Try
    
    
        End Sub

    With the "Me.TableAdapterManager.UpdateAll" part, is that when it updates my copy of my Access DB that I copied into my program (when it asked my to I clicked "Yes").  I don't understand when a new record is getting created/ saved into the datatable "Students".  Databases, DataGridViews, DataBindings, and DataTables are not strong vocab words for me LOL.

    Thursday, June 07, 2012 7:17 PM
  • Yes, working with data can be a bit of a labyrinth these days.  And consider that you are using one of MS's tools that is designed to not have you be concerned with the gritty details!

    I'm unsure now...  is your question actually answered now (you marked my response as answer)?

    The records are getting created and updated whenever you make the call to Update or UpdateAll.  You can actually step through the code to see it at work -- I recommend doing this just to learn something about the apparent black box here.  Set a breakpoint on the UpdateAll line and then press F11 to step into the next line of code that is run.  Keep pressing F11 to see the whole thing through.

    As for the default form vs. form instance, no you do not need to use a form instance but it does provide some advantages in the long run in terms of flexibility and maintainability and debugging.  I think my article covers these points.  Using the default instance is a habit worth breaking early, but don't feel pressured to do something about it for this project if you need to get something released.  Just consider it next time.

    Thursday, June 07, 2012 8:19 PM
  • Yeah my question was kindof answered, but I had like a "sub question" to my original I suppose you could say.  Is there a way to "Add Watch" to a database that you brought over from MS Access?

    What's going to be easier here?  Like can I just use the database I already brought over to store everything and call to it later in my program?

    Or, should I create a New DataTable in my program and store all information there, export the data table to a Access or Excel Save file, then import it again when I need specific fields from it. 

    That's basically what I am trying to do, I need to insert a record into my data base or data table (which ever is the appropriate term for where a record is stored) then close the form via which said record was inserted, close said form, open a different form that serves as a different UI and call a specific field from the record I just created in the other, now closed, form... LoL.

    Thank you for your help

    -Mike

    Friday, June 08, 2012 12:58 PM
  • Hi Mike.  I'm not entirely sure of your question (i.e. what you mean by "Add Watch") but it sounds like you are asking about general strategies to approaching the persistence of data.  Have you already established a database somewhere?  I would think yes -- Access likely -- if you have created table adpaters on your forms, and as such I would think you could just read and write to that .mdf file from any form you have open.  Is there some other persistence need you have beyond your current DB schema?  If so can you explain it in more detail?

    Not sure what .NET framework you are targeting but something to consider as a replacement to Access and Excel will be LocalDB (see http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx)  I haven't played with it myself yet but it looks very promising.  I know you feel a bit green coming into the process of working with data but once immersed in it you will find your comfort level rise fairly quickly and then maybe you can look at something like LocalDB.

    Friday, June 08, 2012 2:52 PM
  • Hi Dig-Boy, thanks for all your help!  It means a lot!  If I could get your indulgence just a bit more, I feel like I will understand whats going on best, if I link the database within my program to a database I can open up in access.  Here's what I mean:

    First, I will open up MS Access and create a new database, with a new data table with fields I specify (I know how to do this)

    Second, I will open up MS Visual Studio and import the database I just created in MS Access, then "drag and drop" that data table onto my desired form "frmMain [Design]" (I know how to do this)

    Third, I want to "test/debug" my program in Visual Studio and try inputting a new record into the data table I just put onto my form.  Then save the changes and stop the debugger.  (I don't know if the debugger allows me to make AND permanently save changes to records in my data table?)

    Fourth, after changes have been saved, I want to the reopen MS Access and open up my database and data table and see the record I just added via my program in Visual Studio (I do not know how to do this, nor do I know if it's even possible?)

    Fifth and Finally, I want to be able to make changes to records to by data table in MS Access, save those changes, then restart the debugger in my program and see the changes to the data table within my program that I just made via MS Access.  (I do not know how to do this, nor do I know if it's even possible?)

    If there's anything you are able/willing to do to help me figure my way through this, I would be greatly appreciative.  Either way, thank you for this, it means a lot.

    -Mike

    Wednesday, June 13, 2012 1:34 PM