VB.NET Login & Registration Form with SQL Server + Source Code - BTS Au Maroc

vendredi 16 février 2018

VB.NET Login & Registration Form with SQL Server + Source Code





Source Code ModuleConnection.vb



Imports System.Data.SqlClient
Module ModuleConnection
Public connection As SqlConnection ' declaration our connection
' function fo connection
Sub OpenConnection()
Try
' function for connection
' Before create a connection, you must have a database (Sql Server)
' i was create a database in my SQL Server,
' so just create a database and a user table.
' in user table create field id_user(Use auto increment)
' email and level. lets see my database
connection = New SqlConnection("Data Source=DESKTOP-5VBP3C9\SQLEXPRESS;Initial Catalog=crud_sqlserver;Integrated Security=True")
If connection.State = ConnectionState.Closed Then
connection.Open() ' open the connection
End If
Catch ex As Exception
' if failed
MsgBox("Failed to connect, Error at " & ex.ToString)
End Try
End Sub
End Module

Source Code Form Login (FrmLogin.vb):


Imports System.Data.SqlClient ' import namespace
Public Class FrmLogin
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenConnection() ' open our connection
Dim dr As SqlDataReader
Dim cmd As SqlCommand
Dim sql As String
cmd = New SqlCommand
cmd.CommandType = CommandType.Text
cmd.Connection = connection ' our connection
sql = "select * from users where email='" & TextBox1.Text & "' and password=CONVERT(NVARCHAR(32),HashBytes('MD5','" & TextBox2.Text & "'),2)"
' the query above will read a MD5 password format.
' im soryy
cmd.CommandText = sql
dr = cmd.ExecuteReader()
If dr.HasRows Then
dr.Read() ' read all data from database
If dr.Item("level") = 1 Then
' show new form for our Administrators
' but i just show the message for administrator account
MsgBox("Welcome Administrator")
ElseIf dr.Item("level") = 2
' its for user account
MsgBox("Wellcome User")
ElseIf dr.Item("level") = 3
' its for another level access
MsgBox("Wellcome Other")
End If
Else ' if dataread not same with username or password inthe textbox
MsgBox("Access denied")
End If
connection.Close() ' close connection
cmd.Dispose()
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
' show the registrations form
FRMsignup.Show()
End Sub
End Class

Source Code Form Registration (FrmSignUp.vb):


Imports System.Data.SqlClient ' import namespace
Public Class FRMsignup
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim TypeUser As String
Dim Level As Integer

TypeUser = ComboBox1.Text
If TypeUser = "Administrator" Then
Level = 1
ElseIf TypeUser = "User"
Level = 2
ElseIf TypeUser = "Other"
Level = 3
' create other level access here
End If

Dim sql As String = "insert into users(email,password,level)values('" & TextBox1.Text & "',CONVERT(NVARCHAR(32),HashBytes('MD5','" & TextBox2.Text & "'),2)," & Level & ")"
' SHITT, I FORGED IT
' execute to database
SignUp(sql)
MsgBox("Sign Up Success !") ' show message

End Sub
' create function for register
Private Sub SignUp(ByVal sql As String)
Dim cmd As New SqlCommand
OpenConnection() ' open our connection
Try
cmd.Connection = connection ' our connection
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
cmd.ExecuteNonQuery()
cmd.Dispose()
connection.Close() ' close our connection
Catch ex As Exception
MsgBox("Failed " & ex.ToString) ' if failed
End Try
End Sub
Private Sub FRMsignup_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Text = "Administrator"
End Sub
End Class

Video tutorial Login & Registration Form with SQL Server:








Aucun commentaire:

Enregistrer un commentaire

Pages