Answered by:
Adding a row of data to a table

-
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
Question
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
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
-
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
-
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
-
-
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
-
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
-
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
-
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
-
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.
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
- Edited by Cor Ligthert Friday, March 31, 2017 4:19 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
-
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