December 5, 2016

Update SQL via Gridview

Hi there. so i’ve searched around to find how to update my sql database via gridview round a lot of examples but didn’t make it :/ here’s the code.. all i need is an update “button” or something.

Code:
Dim connectionString As String = "Server=localhost; User Id=?; Password=?; Database=?"
    Dim SQLConnection As MySqlConnection = New MySqlConnection
    Dim ds As New DataSet
    Dim adapter As New MySqlDataAdapter
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBox1.SelectedIndex = 0

        SQLConnection = New MySqlConnection()
        SQLConnection.ConnectionString = connectionString
        Try

            If SQLConnection.State = ConnectionState.Closed Then
                SQLConnection.Open()
                ToolStripStatusLabel1.Text = "Database Connection Sccessfully Established"
            Else
                SQLConnection.Close()
                ToolStripStatusLabel1.Text = "Database Connection Terminated"

            End If
            Using SQLConnection As New MySqlConnection(connectionString)
                Dim sqlCommand As New MySqlCommand()
                With sqlCommand
                    .CommandText = "select * from users"
                    .Connection = SQLConnection
                    .CommandType = CommandType.Text

                End With
                Try
                    SQLConnection.Open()
                    Dim table As DataTable
                    adapter = New MySqlDataAdapter(sqlCommand)
                    ds = New System.Data.DataSet()
                    adapter.Fill(ds, "users")
                    table = New DataTable
                    adapter.Fill(table)
                    DataGridView1.DataSource = ds.Tables(0)
                    sqlCommand.ExecuteNonQuery()

                Catch ex As MySqlException
                    MsgBox(ex.Message.ToString)

                Finally
                    SQLConnection.Close()
                End Try
            End Using

        Catch ex As Exception
            MsgBox(ex.ToString)

        End Try
    End Sub

Category : Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by Bolkya Resha and Software Testing Theme