Imports MySql.Data.MySqlClient
Public Class Form1
'Represents an SQL statement or stored procedure to execute against a data source.
Dim cmd As New MySqlCommand
Dim da As New MySqlDataAdapter
'declare conn as connection and it will now a new connection because
'it is equal to Getconnection Function
Dim con As MySqlConnection = jokenconn()
Public Function jokenconn() As MySqlConnection
Return New MySqlConnection("Server=mysite.com;Port=3306;Database=mydata;Uid=myuser;Pwd=mypass;")
End Function
Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
Dim sql As String
Dim publictable As New DataTable
Try
'check if the textbox is equal to nothing then it will display the message below!.
If txtuname.Text = "" And txtpass.Text = "" Then
MsgBox("Password or Username Incorrect!")
Else
sql = "select * from users where username ='" & txtuname.Text & "' and password = '" & txtpass.Text & "'"
'bind the connection and query
With cmd
.Connection = con
.CommandText = sql
End With
da.SelectCommand = cmd
da.Fill(publictable)
'check if theres a result by getting the count number of rows
If publictable.Rows.Count > 0 Then
'it gets the data from specific column and assign to the variable
Dim user_type, name As String
user_type = publictable.Rows(0).Item(1)
name = publictable.Rows(0).Item(2)
'check if the type of user is admin
If user_type = "admin" Then
'welcomes the user as Admiistrator
MsgBox("Welcome " & name & " you login as Administrator ")
'set the lbllogin text to Logout
lbllogin.Text = "Logout"
'disabled the groupbox
GroupBox1.Enabled = False
'reset all the two textbox
txtuname.Text = ""
txtpass.Text = ""
'set the lblname to the specific user
lblname.Text = "Hi, " & name
ElseIf user_type = "Encoder" Then
MsgBox("Welcome " & name & " you login as Encoder ")
lbllogin.Text = "Logout"
GroupBox1.Enabled = True
txtuname.Text = ""
txtpass.Text = ""
lblname.Text = "Hi, " & name
Else
MsgBox("You login as Guest!")
lbllogin.Text = "Logout"
GroupBox1.Enabled = False
txtuname.Text = ""
txtpass.Text = ""
lblname.Text = "Hi, " & name
lbllogin.Enabled = True
DataGridView1.Visible = True
End If
Else
MsgBox("Contact administrator to registered!")
txtuname.Text = ""
txtpass.Text = ""
End If
da.Dispose()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Clone()
End Sub
Private Sub lbllogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbllogin.Click
If lbllogin.Text = "Logout" Then
lbllogin.Text = "Login"
lblname.Text = "Hi, Guest!"
ElseIf lbllogin.Text = "Login" Then
GroupBox1.Enabled = True
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GroupBox1.Enabled = True
lbllogin.Enabled = False
Try
Using da As New MySqlDataAdapter("Select * from `comments`", con)
Using dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
If DataGridView1.Rows.Count > 0 Then
DataGridView1.Columns(0).HeaderText = "ID"
DataGridView1.Columns(0).Width = 50
DataGridView1.Columns(1).HeaderText = "added by"
DataGridView1.Columns(1).Width = 150
DataGridView1.Columns(2).HeaderText = "Name Thread"
DataGridView1.Columns(2).Width = 200
DataGridView1.Columns(3).HeaderText = "Notes"
DataGridView1.Columns(3).Width = 200
End If
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class