Results 1 to 8 of 8

Thread: [RESOLVED] Updating Mysql database with vb datagridview

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Resolved [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

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Re: Updating Mysql database with vb datagridview

    Quote Originally Posted by .paul. View Post
    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

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Updating Mysql database with vb datagridview

    your db table should have a primary key field + it should work

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Re: Updating Mysql database with vb datagridview

    Quote Originally Posted by .paul. View Post
    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.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Updating Mysql database with vb datagridview

    Quote Originally Posted by vinitagrawal View Post
    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

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    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
  •  



Click Here to Expand Forum to Full Width