943,095 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 4835
  • VB.NET RSS
Jan 27th, 2010
0

Update MYSQL table with Datagridview

Expand Post »
I Use visual Basic 8.0 and Mysql 5.0
I have one Table1:
Table1:
RecordID , Integer = primary key auto_increment NOT NULL
Articlenr , Integer
Articlename, Char(20)

Table1: has two records:
RecordID, Articlenr ,Articlename
1 , 1000, Hamer
2 , 2000, saw

I want to use Datagridview to update Mysql table1.
All changes as update, change, delete automatically processed into Mysql table1.
Here by my code
really want to now can I link Mydataset to DataGridView1 and Mysql table1?
I now i must do something with MySqlCommandBuilder!

VB.NET Syntax (Toggle Plain Text)
  1.  
  2. Dim MyCommand1 As New MySql.Data.MySqlClient.MySqlCommand
  3. Dim Myadapter As New MySql.Data.MySqlClient.MySqlDataAdapter
  4. Dim MyBuilder As New MySql.Data.MySqlClient.MySqlCommandBuilder
  5. Dim MyDataTable As New DataTable
  6. DIM MyDataset As Dataset
  7.  
  8. MyCommand1.CommandText = "select * from Table1"
  9. MyCommand1.Connection = conn
  10. Myadapter.SelectCommand = MyCommand
  11. Myadapter.Fill(MyDatatable)
  12. Myadapter.Fill(MydataSet)
  13. DataGridView1.DataSource = MyDatatable
  14.  
  15. If MydataSet.GetChanges() Is Nothing Then
  16. MessageBox.Show("The table contains no changes to save.")
  17. Else
  18. Dim rowsAffected As Integer = Myadapter.Update(MyDatatable)
  19. If rowsAffected = 0 Then
  20. MessageBox.Show("No rows were affected by the save operation.")
  21. Else
  22. MessageBox.Show(rowsAffected & " rows were affected by the save operation.")
  23. End If
  24. End If
Similar Threads
Reputation Points: 9
Solved Threads: 0
Junior Poster
dre-logics is offline Offline
102 posts
since Aug 2009
Jan 27th, 2010
0
Re: Update MYSQL table with Datagridview
Table table1 must have a primary key.
Moderator
Reputation Points: 2134
Solved Threads: 1227
Posting Genius
adatapost is offline Offline
6,524 posts
since Oct 2008
Jan 28th, 2010
0
Re: Update MYSQL table with Datagridview
Table1 has a primary key:

RecordID , Integer = primary key auto_increment NOT NULL
Reputation Points: 9
Solved Threads: 0
Junior Poster
dre-logics is offline Offline
102 posts
since Aug 2009
Jan 28th, 2010
0
Re: Update MYSQL table with Datagridview
You have to split your code into two Sub (function).

VB.NET Syntax (Toggle Plain Text)
  1. .....
  2. Dim MyCommand1 As New MySql.Data.MySqlClient.MySqlCommand
  3. Dim Myadapter As New MySql.Data.MySqlClient.MySqlDataAdapter
  4. Dim MyBuilder As MySql.Data.MySqlClient.MySqlCommandBuilder
  5.  
  6. Dim MyDataTable As New DataTable
  7. DIM MyDataset As Dataset
  8.  
  9.  
  10. Private Sub Form1_Load(...)
  11.  
  12. MyCommand1.CommandText = "select * from Table1"
  13. MyCommand1.Connection = conn
  14. Myadapter.SelectCommand = MyCommand
  15. Mybuilder=new MySql.Data.MySqlClient.MySqlCommandBuilder(Myadapter)
  16.  
  17.  
  18. Myadapter.Fill(MydataSet,"Table1")
  19. MyDataTable = MyDataSet.Tables("Table1")
  20.  
  21. DataGridView1.DataSource = MyDatatable
  22. End Sub
  23.  
  24. private Sub Button1_Click(....)
  25. If MydataSet.GetChanges() Is Nothing Then
  26. MessageBox.Show("The table contains no changes to save.")
  27. Else
  28. Dim rowsAffected As Integer = Myadapter.Update(MyDatatable)
  29. If rowsAffected = 0 Then
  30. MessageBox.Show("No rows were affected by the save operation.")
  31. Else
  32. MessageBox.Show(rowsAffected & " rows were affected by the save operation.")
  33. End If
  34. End If
  35. End Sub
Last edited by adatapost; Jan 28th, 2010 at 3:17 am.
Moderator
Reputation Points: 2134
Solved Threads: 1227
Posting Genius
adatapost is offline Offline
6,524 posts
since Oct 2008
Jan 28th, 2010
0
Re: Update MYSQL table with Datagridview
thanks , thus works fine!!
Reputation Points: 9
Solved Threads: 0
Junior Poster
dre-logics is offline Offline
102 posts
since Aug 2009
Feb 11th, 2010
0
Re: Update MYSQL table with Datagridview
I start my application with form1 and go to form2.
In form2 i put in this code .
I just use exactly the code as shown above!

Now something strange happens?
The datagridview1 remains empty.
While Table1 certainly filled with data!!! (But it has worked a few days ago)
Get no error message !

If i use this code :

VB.NET Syntax (Toggle Plain Text)
  1.  
  2. MyCommand1.CommandText = "select * from Table1"
  3. MyCommand1.Connection = conn
  4. Myadapter.SelectCommand = MyCommand
  5. Myadapter.Fill(MyDatatable)
  6. DataGridView1.DataSource = MyDatatable

The datagridview1 is filled with data from table1.
Reputation Points: 9
Solved Threads: 0
Junior Poster
dre-logics is offline Offline
102 posts
since Aug 2009
Feb 14th, 2010
0
Re: Update MYSQL table with Datagridview
sorry it was problem with my system!!!
Reputation Points: 9
Solved Threads: 0
Junior Poster
dre-logics is offline Offline
102 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Find Logic for reading SQL data in VB.NET
Next Thread in VB.NET Forum Timeline: oledb database update problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC