0% found this document useful (0 votes)
3K views1 page

Connecting Your Database and Auto Generate ID Using VB - Net 2008 and MySQL Database

This code connects to a MySQL database called MySampleDatabase on localhost. It has two methods: one to insert new account details into a table on button click, and another to automatically generate and display an ID number by getting the maximum current ID and incrementing it. On form load, it calls the ID generation method.

Uploaded by

AgusWibowo
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
3K views1 page

Connecting Your Database and Auto Generate ID Using VB - Net 2008 and MySQL Database

This code connects to a MySQL database called MySampleDatabase on localhost. It has two methods: one to insert new account details into a table on button click, and another to automatically generate and display an ID number by getting the maximum current ID and incrementing it. On form load, it calls the ID generation method.

Uploaded by

AgusWibowo
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 1

Imports MySql.Data.

MySqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.E
ventArgs) Handles Button1.Click
Dim cn As New MySqlConnection
Dim cmd As New MySqlCommand
cn.ConnectionString = "Server=localhost; user id=root; password= walanga
; database= MySampleDatabase"
cmd.Connection = cn
cn.Open()
cmd.CommandText = "Insert into MySampleTable(id, Username, Password, Ema
il) Values('" & txtidnumber.Text & "','" & txtusername.Text & "','" & txtpasswor
d.Text & "','" & txtemail.Text & "')"
cmd.ExecuteNonQuery()
MsgBox("Account Generated", MsgBoxStyle.Information, "Generation Complet
e")
autogenerate_id()
End Sub
Private Sub autogenerate_id()
Dim cn As New MySqlConnection
Dim cmd As New MySqlCommand
cn.ConnectionString = "Server=localhost; user id=root; password= walanga
; database= MySampleDatabase"
cmd.Connection = cn
cn.Open()
Dim number As Integer
cmd.CommandText = "SELECT Max(ID) FROM MySampleTable"
If IsDBNull(cmd.ExecuteScalar) Then
number = 1
txtidnumber.Text = number
Else
number = cmd.ExecuteScalar + 1
txtidnumber.Text = number
End If
cmd.Dispose()
cn.Close()
cn.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.Even
tArgs) Handles MyBase.Load
autogenerate_id()
End Sub
End Class

You might also like