Update Database with Changes made to the DataGridView

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 43
Reputation: Yogesh Sharma is an unknown quantity at this point 
Solved Threads: 1
Yogesh Sharma Yogesh Sharma is offline Offline
Light Poster

Update Database with Changes made to the DataGridView

 
0
  #1
Jun 15th, 2008
hey frnds, Firstly I pick the records from the databse into the DataGridView, Then I want that If we made changes to the records in the GridView & Click on Update Button, Changes are Reflected into the Datbase .Plz do reply back the ..
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 169
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 14
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Re: Update Database with Changes made to the DataGridView

 
0
  #2
Jun 15th, 2008
You should use a Command Builder to update records so that changes made to the dataset can be reflected into the database. Here is some sample code-


  1.  
  2. Public Class Form1
  3.  
  4. Dim cn As New Odbc.OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=shreyas; User=root;Password=;")
  5. Dim cmd As Odbc.OdbcCommand
  6. Dim adp As Odbc.OdbcDataAdapter
  7. Dim ds As New DataSet
  8.  
  9. Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  10. cn.Close()
  11. End Sub
  12.  
  13.  
  14. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  15. ' Fill the data grid viewer
  16. cn.Open()
  17. cmd = New Odbc.OdbcCommand("Select * from trial1", cn)
  18. adp = New Odbc.OdbcDataAdapter(cmd)
  19. adp.Fill(ds, "trial1")
  20. Me.DataGridView1.DataSource = ds
  21. Me.DataGridView1.DataMember = "trial1"
  22.  
  23. End Sub
  24.  
  25. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  26. 'Update records using a Command Builder
  27. ' The variable i gives the number of records updated
  28. Dim cmdbuilder As New Odbc.OdbcCommandBuilder(adp)
  29. Dim i As Integer
  30. Try
  31. i = adp.Update(ds, "trial1")
  32. MsgBox("Records Updated= " & i)
  33. Catch ex As Exception
  34. MsgBox(ex.Message)
  35. End Try
  36. End Sub
  37.  
  38.  
  39. End Class
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 169
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 14
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Re: Update Database with Changes made to the DataGridView

 
0
  #3
Jun 15th, 2008
button 1 is the update button.
let me know if this helped you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1
Reputation: deyust is an unknown quantity at this point 
Solved Threads: 0
deyust deyust is offline Offline
Newbie Poster

Re: Update Database with Changes made to the DataGridView

 
0
  #4
Feb 2nd, 2009
Originally Posted by tuse View Post
button 1 is the update button.
let me know if this helped you.
Hello, thanks for the code. Its almost working, I received this message. can you help me?!

"Dynamic SQL generation for the UpdateCommand is not supported against a SelecCommand that does not return any key column information"
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Update Database with Changes made to the DataGridView

 
0
  #5
Feb 2nd, 2009
Let's try not to post to threads that are almost a year old....mkay?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 4
Reputation: av1963 is an unknown quantity at this point 
Solved Threads: 0
av1963 av1963 is offline Offline
Newbie Poster

Re: Update Database with Changes made to the DataGridView

 
0
  #6
Jun 25th, 2009
thanks for your efficient, straightforward solution to updating user input to a datagridview in VB.NET - I'm new to .NET and had spent several hours trying to find code for what I had thought should be a simple procedure - I was ready to give up; thanks again -
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC