VB.Net-To-MySQL : A Membership Form open center of the screen and generate membership code automatically

1. Note that first of all download .Net/MySQL connector.
Process already explained   <click Here>

2. Create  Database and  Table (PHPmyAdmin)

ss1

3. Design Membership Form as  seen below!

ss2

 

4. Form behind Vb Code!

Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class member
    Private Sub member_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DateTimePicker1.CustomFormat = "dd-MM-yyyy"
        DateTimePicker2.CustomFormat = "dd-MM-yyyy"
        DateTimePicker1.Format = DateTimePickerFormat.Custom
        DateTimePicker2.Format = DateTimePickerFormat.Custom
        DateTimePicker2.Value = DateTimePicker1.Value.AddYears(1)
        Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - (Me.Height)) / 2)
        TextBox1.Text = NewID().ToString()
        TextBox1.Focus()
    End Sub
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        DateTimePicker2.Value = DateTimePicker1.Value.AddYears(1)
    End Sub

    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        If (IsEmpty()) Then
            MessageBox.Show(" Some  fields have no data!")
            TextBox2.Focus()
            Exit Sub
        Else

            If (SaveData(TextBox1.Text, TextBox2.Text, DateTimePicker1.Value.ToString(), DateTimePicker2.Value.ToString, TextBox3.Text)) Then
                MessageBox.Show(" Yahoo!  Data Saved Sucessfully")
                TextBox1.Text = NewID().ToString()
                allClear()

            Else
                MessageBox.Show(" Oops!  Data  Not Saved ")
            End If
        End If
    End Sub
    Private Function NewID()
        Dim cnn As New MySqlConnection
        Dim cmd As New MySqlCommand
        Dim reader As MySqlDataReader
        Dim mid As Long = 0
        cnn.ConnectionString = "Server=127.0.0.1;Database=library;Uid='root';Pwd='';"
        cmd.CommandText = "select count(mcode) from members"
        cmd.Connection = cnn
        cnn.Open()
        Dim count As Int16 = Convert.ToInt16(cmd.ExecuteScalar())
        If count = 0 Then
            mid = 1000
        Else
            cmd.CommandText = "select max(mcode) from members"
            reader = cmd.ExecuteReader()
            reader.Read()
            mid = reader.GetInt16(0)
            mid = mid + 1
        End If
        cnn.Close()
        Return mid
    End Function
    Private Function SaveData(ByVal code As String, ByVal bn As String, ByVal doj As String, ByVal doe As String, ByVal mob As String) As Boolean

        Dim flag As Boolean = False
        Dim cnn As New MySqlConnection
        Dim cmd As New MySqlCommand
        cnn.ConnectionString = "Server=127.0.0.1;Database=library;Uid='root';Pwd='';"
        cmd.CommandText = "insert  into members(mcode,m_name,m_doj,m_doe,m_mobile) values (@mcode,@m_name,@m_doj,@m_doe,@m_mobile)"
        cmd.Parameters.AddWithValue("@mcode", Int16.Parse(code))
        cmd.Parameters.AddWithValue("@m_name", bn)
        cmd.Parameters.AddWithValue("@m_doj", CDate(doj))
        cmd.Parameters.AddWithValue("@m_doe", CDate(doe))
        cmd.Parameters.AddWithValue("@m_mobile", mob)
        cmd.Connection = cnn
        cnn.Open()
        Dim rows As Integer
        rows = cmd.ExecuteNonQuery()
        If (rows <> 0) Then
            flag = True
        Else
            flag = False
        End If
        cnn.Close()
        Return flag
    End Function
    Private Sub allClear()
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox2.Focus()
    End Sub

    Private Function IsEmpty() As Boolean
        Dim flag As Boolean = False
        If (String.IsNullOrEmpty(TextBox2.Text) Or String.IsNullOrEmpty(TextBox3.Text)) Then
            flag = True
        End If
        Return flag
    End Function

    Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
        Me.Close()
    End Sub
End Class

 

5. Load The form and fill  two record !

ss3

next record..

ss4

6. Open again PHPmyAdmin to see saved record!

ss5