|
-
Oct 15th, 2017, 06:16 PM
#1
Thread Starter
Addicted Member
Can’t refresh Products Table in a typed DataSet and the Database to the original
Create a typed DataSet:
1. Created a Windows Application Form with Visual Studio 2015
2. Bind a DataGridView control with Visual Studio (not through its properties).
3. I added a connection to Northwind Database
4. I created a DataSet for this example with the Categories, Suppliers, and Products tables of the Northwind database
5. The DataSet contains three DataTables, and each DataTable is made of the columns you selected in the wizard. In this Form I will only use the Products Table only with 77 Products (rows).
6. This DataSet is typed, because it knows the structure of the data you’re going to store in it.
7. Selected the Products table. As soon as you select it, an arrow appears next to its name. Click this arrow to open a drop-down list with the binding options for the DataTable.
8. Selected the DataGridView option, and then drop the Products DataTable on the form.
9. The editor created a DataGridView control and bind it to the Products DataTable. In addition, it will create a toolbar at the top of the form with a few navigational and editing buttons
10. Added a Refresh Data button (see code)
PROBLEM: Can’t refresh Products Table in a typed DataSet and the Database to the original version. (See image).
1. Click the + button and add a new row (product). ProductID, primary key is -1 by default as you know.
2. Save it (click button save)
3. Click Refresh Data button. ProductID the first time is 78. Dataset and Database now have 78 products (rows).
4. Select row 78 and click X button (Delete row). Refresh DataSet in DataCode window. Checked with query SELECT * FROM Products in Microsoft SQL Management Studio. It is ok. Only 77 rows as the original database.
5. Close Project, shut down computer.
6. And here is when I have the big problem. Start Project (F5) and add row but is 79 not 78.
7. By the time I am writing this, ProductID is 119 !!!. In my code I run Me.ProductsTableAdapter.Fill (Me.NorthwindDataSet.Products) to Update DataSet but does not work.
Attach image and complete code in Form1 is generated automatically by Visual Studio. Background code is generated automatically by Visual Studio because it is a typed DataSet.
Please help me with this problem. Thank you.
Code:
Public Class Form1
Private Sub ProductsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles ProductsBindingNavigatorSaveItem.Click
Me.Validate()
Me.ProductsBindingSource.EndEdit()
Me.TableAdapterManager.ProductsTableAdapter.
Adapter.ContinueUpdateOnError = True
Me.TableAdapterManager.UpdateAll(Me.NorthwindDataSet)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'NorthwindDataSet.Products' table. You can move, or remove it, as needed.
Me.ProductsTableAdapter.Fill(Me.NorthwindDataSet.Products)
End Sub
Private Sub bttnRefreshData_Click(sender As Object, e As EventArgs) Handles bttnRefreshData.Click
If NorthwindDataSet.HasChanges Then
Dim reply As MsgBoxResult =
MsgBox("The DataSet contains changes." &
vbCrLf & "Reload data anyway?",
MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation)
If reply = MsgBoxResult.No Then Exit Sub
End If
Me.ProductsTableAdapter.Fill(Me.NorthwindDataSet.Products)
End Sub
End Class
Try to upload image but not possible. I can send project via e mail.
It is important to have a clear idea of the difference of an untype DataSet and a Type one. In the Type ones Visual Studio does all the work. Please read carefully my post.
Last edited by ebellounisoft; Oct 15th, 2017 at 07:22 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|