Simple Insert, Edit, Delete VB .Net 2008 DB MySQL

Simple Insert, Edit, Delete VB .Net 2008 DB MySQL
In this tutorial we will share about Simple Add, Edit, Delete VB .Net 2008 Database MySQL. Please make sure you already install MySQL (we use xampp) and Mysql connector.


Please following below steps :


#1
Create database name = mydb
Create table name = TBL_CUSTOMER
Fill sample data in TBL_CUSTOMER like below picture :

#2
Open your VB 2008 Application
Design Form1 like below picture :
Fill below code at Form1
Imports System.Data.Odbc
Public Class Form1
    Dim CONN As OdbcConnection
    Dim CMD As OdbcCommand
    Dim DS As New DataSet
    Dim DA As OdbcDataAdapter
    Dim RD As OdbcDataReader
    Dim DBLocation As String
    Sub DBConn()
        DBLocation = "Driver={MySQL ODBC 3.51 Driver};database=mydb;server=localhost;uid=root"
        CONN = New OdbcConnection(DBLocation)
        If CONN.State = ConnectionState.Closed Then
            CONN.Open()
        End If
    End Sub
    Sub EmptyForm()
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox1.Focus()
    End Sub

    Sub ShowGrid()
        DA = New OdbcDataAdapter("select * from TBL_CUSTOMER", CONN)
        DS = New DataSet
        DA.Fill(DS, "TBL_CUSTOMER")
        DataGridView1.DataSource = DS.Tables("TBL_CUSTOMER")
        DataGridView1.ReadOnly = True
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TextBox1.Text.Length < 6 Or TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
            MsgBox("Data incompleted, Make sure all form is filled and customer id 6 digits")
            Exit Sub
        Else
            Call DBConn()
            CMD = New OdbcCommand("Select * from TBL_CUSTOMER where Customer_id='" & TextBox1.Text & "'", CONN)
            RD = CMD.ExecuteReader
            RD.Read()
            If Not RD.HasRows Then
                Dim simpan As String = "insert into TBL_CUSTOMER values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
                CMD = New OdbcCommand(simpan, CONN)
                CMD.ExecuteNonQuery()
                MsgBox("Add data successed", MsgBoxStyle.Information, "Information www.belajarvb.com")
            Else
                Dim edit As String = "update TBL_CUSTOMER set Name='" & TextBox2.Text & "',Address='" & TextBox3.Text & "',Phone='" & TextBox4.Text & "' where kodeAnggota='" & TextBox1.Text & "'"
                CMD = New OdbcCommand(edit, CONN)
                CMD.ExecuteNonQuery()
                MsgBox("Edit data successed", MsgBoxStyle.Information, "Information")
            End If
            Call ShowGrid()
            Call EmptyForm()
        End If
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If TextBox1.Text = "" Then
            MsgBox("Customer id is empty, please filled it")
            TextBox1.Focus()
            Exit Sub
        Else
            If MessageBox.Show("Are you sure want to delete..?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Dim hapus As String = "delete * from TBL_CUSTOMER where Customer_id='" & TextBox1.Text & "'"
                CMD = New OdbcCommand(hapus, CONN)
                CMD.ExecuteNonQuery()
                Call ShowGrid()
                Call EmptyForm()
                MsgBox("Deleted successed", MsgBoxStyle.Information, "Information")
            Else
                Call EmptyForm()
            End If
        End If
    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Call EmptyForm()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call DBConn()
        Call ShowGrid()
        Call EmptyForm()
    End Sub

    Private Sub TextBox1_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        TextBox1.MaxLength = 6
        If e.KeyChar = Chr(13) Then
            Call DBConn()
            CMD = New OdbcCommand("Select * from TBL_CUSTOMER where Customer_id='" & TextBox1.Text & "'", CONN)
            RD = CMD.ExecuteReader
            RD.Read()
            If Not RD.HasRows Then
                TextBox2.Text = ""
                TextBox3.Text = ""
                TextBox4.Text = ""
                TextBox2.Focus()
            Else
                TextBox2.Text = RD.Item("Name")
                TextBox3.Text = RD.Item("Address")
                TextBox4.Text = RD.Item("Phone")
                TextBox2.Focus()
            End If
        End If
    End Sub

    Private Sub TextBox2_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        TextBox2.MaxLength = 50
        If e.KeyChar = Chr(13) Then TextBox3.Focus()
    End Sub

    Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        TextBox3.MaxLength = 50
        If e.KeyChar = Chr(13) Then TextBox4.Focus()
    End Sub
    Private Sub TextBox4_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
        TextBox4.MaxLength = 15
        If e.KeyChar = Chr(13) Then Button1.Focus()
    End Sub
End Class
Run your project and You can click here to download Project above / source code

Subscribe to receive free email updates:

0 Response to "Simple Insert, Edit, Delete VB .Net 2008 DB MySQL"

Post a Comment