so i have taken it to myself to try to help out by posting some example code as i go along.
first code I want to post is with working with the login form.
to create a login form window, go to the solution explorer screen and right-click on the bolded text and go to add-> New item.
look for login form and select it.
then double click on the any of the buttons there, both by default just close out the program, you can keep the default value of that set for cancel unles you have other plans.
before we get too far, we need to import some values at the very top so that the program knows what we're doing:
Imports MySql.Data.MySqlClient Imports System.Data
in the ok action remove the me.close code as we wont need it.
then we need to connect to the db, we also need to make sure the connection went through.
Dim conn As MySqlConnection
'connect to DB
conn = New MySqlConnection()
conn.ConnectionString = "server=localhost; user id=myroot; password=mypwd; database=login"
'see if connection failed.
Try
conn.Open()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to Database: " & myerror.Message)
End Trypretty easy, now lets get ready to query the server.
'sql query Dim myAdapter As New MySqlDataAdapter Dim sqlquery = "SELECT username, password FROM userlist Where username='" & UsernameTextBox.Text & "' and password='" & PasswordTextBox.Text & "'" Dim myCommand As New MySqlCommand() myCommand.Connection = conn myCommand.CommandText = sqlquery 'start query myAdapter.SelectCommand = myCommand Dim myData As MySqlDataReader myData = myCommand.ExecuteReader()
here we created a variable tha will handle the command we need to query the db and then made another variable to read the data.
now we have to check to ensure the user entered the correct login info.
'see if user exits.
If myData.HasRows = 0 Then
MessageBox.Show("Invalid Login Details", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Dim frm1 = New Form1
Frm1.Show()
Me.Visible = False
End Ifhere the if statement sees if the entered data from our user returns any rows, if it does we then display the form the users wants to access and then hide the login form until needed again.
now we have one final item to do and then this lesson is over, we need to make the login form visible after the user closes out the window they were at.
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing LoginForm1.Visible = True End Sub
the final item you need to do is make loginform1 be the first thing that displays and that can be done though the properties portion of the project.
the way the code works is when the user loads the program the login form displays, they enter their login code and then they have access to do whatever they wish to do. when their done they just simply close the window and then the login form will re-appear to either let another user login or to close out the program.
this is of course a simple example, later in this series I will go further into things.
feel free to comment and offer any additions to this.
if your stuck on anything with this, please let me know

Sign In
Register
Help
This topic is locked


MultiQuote

