none
Adding a row of data to a table

    Question

  • I am attempting to add a row of data to an Access table in my application and am using the following method:

                    Dim DocMasterRow As _MasterBase2_0DataSet.tblDMDocMasterRow
                    DocMasterRow = _MasterBase2_0DataSet.tblDMDocMaster.NewtblDMDocMasterRow()
                    DocMasterRow.intDocNum = glbintDocNum
                    DocMasterRow.intChgNum = glbintCRNum
                    DocMasterRow.strDocTitle = glbstrDocTitle
                    DocMasterRow.strDocRev = glbstrDocRev
                    DocMasterRow.strDocType = glbstrDocType
                    DocMasterRow.strDocOwner = glbstrDocOwner
                    DocMasterRow.strDocWhere = glbstrDocWhere
                    DocMasterRow.blnDocEffective = False
                    DocMasterRow.blnDocObsolete = False
                    DocMasterRow.blnDocSupersede = True
                    _MasterBase2_0DataSet.tblDMDocMaster.Rows.Add(DocMasterRow)

    It is my understanding that the above code would create a row of data then add that data to a new row in the above table.

    I used the following code after the above was executed:

                    Me.Validate()
                    Me.lkpMBItemNumBindingSource.EndEdit()
                    Me.tblDMDocMasterBindingSource.EndEdit()
                    Me.LnkMBChangeRequestBindingSource.EndEdit()
                    Me.TableAdapterManager.UpdateAll(Me._MasterBase2_0DataSet)
    So my question is, why does the new row of data not actually show up in the tblDMDocMaster table?


    gwboolean

    Friday, March 31, 2017 12:00 AM

Answers

  • I finally stumbled across the problem and fixed it.  It turns out that in my TableAdapterManager properties I had failed to set the properties for each of the specific TableAdapters that I had added to the form.  Once I set the properties, as seen below, everything works exactly as expected.  Sorry to do something not too bright, but thanks Karen and Cor for the help.


    gwboolean

    • Marked as answer by gwboolean Friday, March 31, 2017 5:39 PM
    Friday, March 31, 2017 5:39 PM

All replies

  • Hello,

    The following I have one table, Customers in the xsd file where in the code below I set three of the fields in a new row then save it as shown below. If I leave out the UpdateAll the new primary key will be -1.

    Dim rowView As DataRowView = TryCast(CustomersBindingSource.List, DataView).AddNew
    ' Cast to a CustomerRow from a generic DataRow
    Dim newCustomerRow = CType(rowView.Row, NorthDataSet.CustomersRow)
    ' set fields
    newCustomerRow.CompanyName = "Karen's Coffee shop"
    newCustomerRow.ContactName = "Karen Payne"
    newCustomerRow.ContactTitle = "Owner"
    ' Add record to Customers
    NorthDataSet.Customers.AddCustomersRow(newCustomerRow)
    ' save
    CustomersBindingSource.EndEdit()
    Dim affected As Integer = TableAdapterManager.UpdateAll(NorthDataSet)
    ' check to see if the update was successful
    If affected <> 1 Then
        MessageBox.Show("Failed")
    End If

    Screenshot, I was on the row above (id of 60) then pressed a button to generate the new DataRow.

    Granted I took a slightly different route in regards to the BindingSource but it would seem what you did should have worked too.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by gwboolean Friday, March 31, 2017 1:14 AM
    • Unmarked as answer by gwboolean Friday, March 31, 2017 3:21 PM
    Friday, March 31, 2017 12:43 AM
    Moderator
  • Karen,

    I haven't yet had a chance to work through and try what you were showing, but it looks pretty good to me.  I will work through it this evening.  However, you mentioned that what I did should work.  Is there anything you can see in what I did that might have caused my failure?

    I do know of a couple of ways that will accomplish what I want, but I don't like doing it that way.


    gwboolean

    Friday, March 31, 2017 1:02 AM
  • I don't see anything wrong which is why I thought it should work. What I provided I've used to assist other forum questions and know it works :-)

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 31, 2017 1:12 AM
    Moderator
  • Thanks Karen.  I will be incorporating it after dinner and the news.

    gwboolean

    Friday, March 31, 2017 1:14 AM
  • Thanks Karen.  I will be incorporating it after dinner and the news.

    gwboolean

    Hopefully this works for you and if it doesn't you know to un-makr my reply if it does not work :-)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 31, 2017 1:18 AM
    Moderator
  • Karen,

    I set your method up and ran it but ended up with the same result.  Can you take a look at it and tell me what I might have done wrong? 

                    'Add document record
                    Dim rowView As DataRowView = TryCast(tblDMDocMasterBindingSource.List, DataView).AddNew
                    Dim DocMasterRow = CType(rowView.Row, _MasterBase2_0DataSet.tblDMDocMasterRow)
                    DocMasterRow.intDocNum = glbintDocNum
                    DocMasterRow.intChgNum = glbintCRNum
                    DocMasterRow.strDocTitle = glbstrDocTitle
                    DocMasterRow.strDocRev = glbstrDocRev
                    DocMasterRow.strDocType = glbstrDocType
                    DocMasterRow.strDocOwner = glbstrDocOwner
                    DocMasterRow.strDocWhere = glbstrDocWhere
                    DocMasterRow.blnDocEffective = False
                    DocMasterRow.blnDocObsolete = False
                    DocMasterRow.blnDocSupersede = True
                    _MasterBase2_0DataSet.tblDMDocMaster.AddtblDMDocMasterRow(DocMasterRow)
                    Dim affected As Integer = TableAdapterManager.UpdateAll(_MasterBase2_0DataSet)
                    ' check to see if the update was successful
                    If affected <> 1 Then
                        MessageBox.Show("Failed")
                    End If


    gwboolean

    Friday, March 31, 2017 3:23 PM
  • I don't see anything wrong. Are you sure the objects used are targeting the correct controls?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 31, 2017 3:37 PM
    Moderator
  • In this form I have no controls that correspond to the field values in this table.  What I have is a form for a change request and I am attempting to fill a table that has no controls on this form.  It was my though that you could fill directly to a table without having corresponding controls.  I get the idea that this is not kosher. 

    gwboolean

    Friday, March 31, 2017 4:00 PM
  • Are you sure you are looking to the right database. 

    If it is a local database then the setting can be that there is a copy made. While debugging the result is not done in the original but in the copy. 

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/c4aab4b1-3ed9-4901-8c58-8e8791bf12ae/bindingnavigator-save-button-doesnt-save-the-data-to-database?forum=winforms

    Don't look to the bindingmanager but what Karen wrote about copy always. 

    Be aware that your first questions showed code how which we can call quick and dirty, a kind of mix between a strongly and a non strongly dataset. 


    Success
    Cor



    Friday, March 31, 2017 4:17 PM
  • Anything is possible, and in my case likely.  All I can tell you is that when I run this code in my change request for it fails to create and fill a row of data into the table.  When I run the identical code from the form that I would normally use with that table, it does exactly what it is supposed to do.

    I will check out your link and see if it can provide me with the answer.


    gwboolean

    Friday, March 31, 2017 4:34 PM
  • I finally stumbled across the problem and fixed it.  It turns out that in my TableAdapterManager properties I had failed to set the properties for each of the specific TableAdapters that I had added to the form.  Once I set the properties, as seen below, everything works exactly as expected.  Sorry to do something not too bright, but thanks Karen and Cor for the help.


    gwboolean

    • Marked as answer by gwboolean Friday, March 31, 2017 5:39 PM
    Friday, March 31, 2017 5:39 PM