943,926 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 42572
  • VB.NET RSS
Jun 15th, 2008
0

Update Database with Changes made to the DataGridView

Expand Post »
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 ..
Reputation Points: 10
Solved Threads: 1
Light Poster
Yogesh Sharma is offline Offline
43 posts
since Apr 2008
Jun 15th, 2008
0

Re: Update Database with Changes made to the DataGridView

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-


VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007
Jun 15th, 2008
-1

Re: Update Database with Changes made to the DataGridView

button 1 is the update button.
let me know if this helped you.
Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007
Feb 2nd, 2009
0

Re: Update Database with Changes made to the DataGridView

Click to Expand / Collapse  Quote originally posted by tuse ...
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"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deyust is offline Offline
1 posts
since Feb 2009
Feb 2nd, 2009
0

Re: Update Database with Changes made to the DataGridView

Let's try not to post to threads that are almost a year old....mkay?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 25th, 2009
0

Re: Update Database with Changes made to the DataGridView

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 -
Reputation Points: 13
Solved Threads: 0
Newbie Poster
av1963 is offline Offline
4 posts
since Jun 2009
Jul 3rd, 2010
0

connection error

Hi, im new for .net.
im am using same code, but only connection string is changed,
Dim cn As New Odbc.OdbcConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\OFC\aadata\wms1.mdb")

it showing error,
A first chance exception of type 'System.Data.Odbc.OdbcException' occurred in System.Data.dll
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified


what is that?


Click to Expand / Collapse  Quote originally posted by tuse ...
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-


VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kartheeee is offline Offline
5 posts
since Sep 2008

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: MS Access Database file on Local Network
Next Thread in VB.NET Forum Timeline: VB.NET and MYSQL based registration system





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


Follow us on Twitter


© 2011 DaniWeb® LLC