|
-
May 10th, 2013, 02:05 PM
#1
Thread Starter
New Member
[RESOLVED] Updating Mysql database with vb datagridview
Hi!
I am creating a windows form with vb to edit a mysql database. I have a datagridview where i can view the database with custom queries. But, I want to save the edited cells back to the source database in their respective places. I dont know how to do that. Any help is appreciated. I am attaching my code:
Code:
Imports MySql.Data.MySqlClient
Private Sub showdata_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles showdata.Click
Dim uspatserver As String = "server = localhost; user id = ****; password = ****; database = us_pat"
Dim uspatconnection As MySqlConnection = New MySqlConnection
datagrid.Columns.Clear()
Dim cmd As MySqlCommand = New MySqlCommand
Dim stm As String
If uspatconnection.State = ConnectionState.Closed Then
uspatconnection.Open()
End If
stm = "SELECT * FROM clinicalinfo"
cmd.CommandType = CommandType.Text
cmd.CommandText = stm
cmd.Connection = uspatconnection
Dim dt As New DataTable
dt.Load(cmd.ExecuteReader)
With datagrid
.AutoGenerateColumns = True
.DataSource = dt
End With
cmd.Dispose()
cmd = Nothing
uspatconnection.Close()
uspatconnection.Dispose()
End Sub
Thanks!
Last edited by Siddharth Rout; May 10th, 2013 at 02:24 PM.
Reason: Added Code Tags
-
May 10th, 2013, 02:26 PM
#2
Re: Updating Mysql database with vb datagridview
Welcome to the vbforums vinitagrawal 
I have moved your question from Visual Basic 6 and Earlier to VB.net.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
May 10th, 2013, 03:16 PM
#3
Re: Updating Mysql database with vb datagridview
try this:
Code:
Imports MySql.Data.MySqlClient
Public Class form1
Dim dt As DataTable
Dim da As MySqlDataAdapter
Private Sub showdata_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles showdata.Click
Dim uspatserver As String = "server = localhost; user id = ****; password = ****; database = us_pat"
Dim uspatconnection As MySqlConnection = New MySqlConnection(uspatserver)
DataGrid.Columns.Clear()
Dim cmd As MySqlCommand = New MySqlCommand
Dim stm As String
If uspatconnection.State = ConnectionState.Closed Then
uspatconnection.Open()
End If
stm = "SELECT * FROM clinicalinfo"
cmd.CommandType = CommandType.Text
cmd.CommandText = stm
cmd.Connection = uspatconnection
da = New MySqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)
Dim cb As New MySqlCommandBuilder(da)
With DataGrid
.AutoGenerateColumns = True
.DataSource = dt
End With
cmd.Dispose()
cmd = Nothing
uspatconnection.Close()
uspatconnection.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'save changes
DataGrid.EndEdit()
da.Update(dt)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 10th, 2013, 03:42 PM
#4
Thread Starter
New Member
Re: Updating Mysql database with vb datagridview
 Originally Posted by .paul.
try this:
Code:
Imports MySql.Data.MySqlClient
Public Class form1
Dim dt As DataTable
Dim da As MySqlDataAdapter
Private Sub showdata_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles showdata.Click
Dim uspatserver As String = "server = localhost; user id = ****; password = ****; database = us_pat"
Dim uspatconnection As MySqlConnection = New MySqlConnection(uspatserver)
DataGrid.Columns.Clear()
Dim cmd As MySqlCommand = New MySqlCommand
Dim stm As String
If uspatconnection.State = ConnectionState.Closed Then
uspatconnection.Open()
End If
stm = "SELECT * FROM clinicalinfo"
cmd.CommandType = CommandType.Text
cmd.CommandText = stm
cmd.Connection = uspatconnection
da = New MySqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)
Dim cb As New MySqlCommandBuilder(da)
With DataGrid
.AutoGenerateColumns = True
.DataSource = dt
End With
cmd.Dispose()
cmd = Nothing
uspatconnection.Close()
uspatconnection.Dispose()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'save changes
DataGrid.EndEdit()
da.Update(dt)
End Sub
End Class
Thanks for the help! you made my day. How can i put datatype checking, so that errors from user can save them the pain of reediting?
Last edited by vinitagrawal; May 10th, 2013 at 03:54 PM.
Reason: my mistake
-
May 10th, 2013, 03:46 PM
#5
Re: Updating Mysql database with vb datagridview
your db table should have a primary key field + it should work
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 10th, 2013, 04:16 PM
#6
Thread Starter
New Member
Re: Updating Mysql database with vb datagridview
 Originally Posted by .paul.
your db table should have a primary key field + it should work
yes, it did work. that was my mistake. i was using an alias in select command.
anyways, it works fine now.
I was wondering if there is anyway to check for data types for incorrect entries by user, in case it happens.
-
May 10th, 2013, 04:52 PM
#7
Re: Updating Mysql database with vb datagridview
 Originally Posted by vinitagrawal
I was wondering if there is anyway to check for data types for incorrect entries by user, in case it happens.
not sure about that. start a new thread for that question
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 14th, 2013, 07:33 PM
#8
Thread Starter
New Member
Re: Updating Mysql database with vb datagridview
Thank you, very much. I will.
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
|