Escolar Documentos
Profissional Documentos
Cultura Documentos
In this tutorial, I'm going to show you how to create a simple Login-Logout system using Visual
basic.net and MySQl Database. To start with this application, open Visual Basic->Create a New
Project->Save it as Login.
This time, lets add objects to our windows form and these objects are the following: four Labels,
two Textbox,two buttons and a Groupbox.
Property
Name
Text
Name
Text
Text
Text
Name
Name
PasswordChar
Name
Settings
lbllogin
Login
lblname
Hi,Guest!
Username
Password
txtuname
txtpass
*
btnok
Text
Name
Text
Button2
OK
btncancel
Cancel
After setting the Object properties, arrange all the objects like as shown below.
This time, lets we are now ready to add functionality to our application. To do this, double click
the form and add the following code:
This code will simply disable the group box that holding the label Username and Password same
with the two textbox and two Buttons.
1. GroupBox1.Enabled = False
Next, double click the lbllogin label and add the following code:
This code will check if the lbllogin is set to Logout then it reset the text of lbllogin to
Login same with the lblname to Hi, Guest!, else if the text of lbllogin is equal to
Login then it's enabled the Group box.
1.
2.
lbllogin.Text = "Login"
3.
4.
5.
GroupBox1.Enabled = True
6.
7.
End If
3.
4.
5.
6.
7.
8.
9.
10.
And double click the OK button and add the following code:
Ads not by this site
3.
Try
4.
5.
6.
7.
8.
9.
Else
sql = "select * from tbluseraccounts where username ='"
& txtuname.Text & "' and userpassword = '" & txtpass.Text & "'"
10.
11.
With cmd
12.
.Connection = con
13.
.CommandText = sql
14.
End With
15.
da.SelectCommand = cmd
16.
da.Fill(publictable)
17.
18.
of rows
19.
20.
21.
22.
user_type = publictable.Rows(0).Item(4)
23.
name = publictable.Rows(0).Item(2)
24.
25.
to the variable
26.
27.
28.
29.
lbllogin.Text = "Logout"
30.
31.
GroupBox1.Enabled = False
32.
33.
txtuname.Text = ""
34.
txtpass.Text = ""
35.
36.
Administrator ")
37.
38.
39.
40.
lbllogin.Text = "Logout"
41.
GroupBox1.Enabled = False
42.
txtuname.Text = ""
43.
txtpass.Text = ""
44.
Encoder ")
45.
Else
46.
47.
lbllogin.Text = "Logout"
48.
GroupBox1.Enabled = False
49.
txtuname.Text = ""
50.
txtpass.Text = ""
51.
52.
End If
53.
54.
Else
55.
56.
txtuname.Text = ""
57.
txtpass.Text = ""
58.
End If
59.
60.
61.
da.Dispose()
End If
62.
63.
Catch ex As Exception
64.
MsgBox(ex.Message)
65.
66.
End Try
67.
con.Clone()
This time you can now test your application by pressing F5.
And here's the database table used.
1. CREATE TABLE IF NOT EXISTS `tbluseraccounts` (
2.
3.
4.
5.
6.
7.
8. ) ENGINE=MyISAM
9.
10. -11. -- Dumping data for table `tbluseraccounts`
12. -13.
14. INSERT INTO `tbluseraccounts` (`userID`, `username`, `users_name`,
`userpassword`, `usertype`) VALUES
15. (1, 'admin', 'Joken Villanueva', 'admin123', 'Admin'),
16. (2, 'jason', 'Jason Batuto', 'jason123', 'Encoder');
After testing the program, heres all the following code used in this application.
1. Imports MySql.Data.MySqlClient
2.
3. Public Class Form1
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
End Function
14.
15.
16.
17.
Try
18.
19.
20.
21.
22.
23.
Else
sql = "select * from tbluseraccounts where username
='" & txtuname.Text & "' and userpassword = '" & txtpass.Text & "'"
24.
25.
With cmd
26.
.Connection = con
27.
.CommandText = sql
28.
End With
29.
da.SelectCommand = cmd
30.
da.Fill(publictable)
31.
32.
33.
34.
35.
36.
user_type = publictable.Rows(0).Item(4)
37.
name = publictable.Rows(0).Item(2)
38.
39.
40.
41.
42.
43.
lbllogin.Text = "Logout"
44.
45.
GroupBox1.Enabled = False
46.
47.
txtuname.Text = ""
48.
txtpass.Text = ""
49.
50.
51.
52.
53.
54.
lbllogin.Text = "Logout"
55.
GroupBox1.Enabled = False
56.
txtuname.Text = ""
57.
txtpass.Text = ""
58.
59.
Else
60.
61.
lbllogin.Text = "Logout"
62.
GroupBox1.Enabled = False
63.
txtuname.Text = ""
64.
txtpass.Text = ""
65.
66.
End If
67.
68.
Else
69.
70.
txtuname.Text = ""
71.
txtpass.Text = ""
72.
End If
73.
74.
da.Dispose()
75.
End If
76.
77.
Catch ex As Exception
78.
MsgBox(ex.Message)
79.
80.
End Try
81.
con.Clone()
82.
83.
84.
End Sub
85.
86.
87.
88.
89.
lbllogin.Text = "Login"
90.
91.
92.
GroupBox1.Enabled = True
93.
94.
95.
End If
End Sub
96.
97.
98.
99.
GroupBox1.Enabled = False
End Sub
before we code login button you need to make sure table user is exist
create table user with this sql
CREATE TABLE user (
username VARCHAR( 20 ) NOT NULL DEFAULT ,
password VARCHAR( 50 ) NOT NULL DEFAULT ,
typelogin VARCHAR( 20 ) NOT NULL DEFAULT ,
PRIMARY KEY ( username )
28
29
30
31
areader.Close()
32
' you can add another code or call function in this section
33
Me.Dispose()
34
Else
35
areader.Close()
36
txtpass.Text = ""
37
MessageBox.Show(messageerror)
38
End If
39
Exit Sub
40
err:
41
If areader.IsClosed = False Then
42
areader.Close()
43
End If
44
MessageBox.Show(Err.Description)
45
End Sub
46
47
48
49
now we are finish with login form. you can set startup with this login form or another form.
'-----------------------------------------------------'
4.
5.
'connect to DB
6.
7.
8.
9.
Try
10.
11.
conn.Open()
Catch myerror As MySqlException
12.
13.
End Try
14.
'sql query
15.
16.
17.
18.
19.
myCommand.Connection = conn
20.
myCommand.CommandText = sqlquery
21.
'start query
22.
myAdapter.SelectCommand = myCommand
23.
24.
myData = myCommand.ExecuteReader()
25.
26.
If myData.HasRows = 0 Then
27.
28.
Else
29.
30.
frm1.Show()
31.
Me.Visible = False
32.
End If
PowerPoster
Join Date
Jul 2006
Location
Providence, RI - USA
Posts
9,195
End Sub
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our
duty as we understand it.
- Abraham Lincoln -
PowerPoster
Join Date
Dec 2009
Location
Moscow, Russia
Posts
3,589
'Create an encoding object to ensure the encoding standard for the source text
Dim Ue As New UnicodeEncoding()
'Retrieve a byte array based on the source text
Dim ByteSourceText() As Byte = Ue.GetBytes(SourceTStext)
'Instantiate an MD5 Provider object
Dim Md5 As New MD5CryptoServiceProvider()
'Compute the hash value from the source
Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
'And convert it to String format for return
Return Convert.ToBase64String(ByteHash)
End Function
So, instead of searching for password, you should search for MD5 hash of it.
#define true ((rand() % 2)? true: false) // Debug THAT!
Rate the posts that helped you!
Evaluating Expressions | Simple POP3 Protocol Realization | Timers explained | Delegates explained | A secure login to your
app sample | Numeric Textbox | Printer.Print (or I miss VB6 printing in .Net) | Shuffling any IEnumerable | Marshalling
structures to byte arrays and back | Invoking a method by its name | Plugins architecture WITHOUT references | Permuter
that permutes everything | Encrypt/Decrypt your stuf | Snake Game | Enumerating all Network Adapters in the system |
How to make a web request
PowerPoster
Join Date
Jul 2006
Location
Providence, RI - USA
Posts
9,195
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our
duty as we understand it.
- Abraham Lincoln -
Thread Starter
Member
Join Date
Aug 2010
Posts
38
PowerPoster
Join Date
Dec 2009
Location
Moscow, Russia
Posts
3,589
Thread Starter
Member
Join Date
Aug 2010
Posts
38
Thread Starter
Member
Join Date
Aug 2010
Posts
38
cicatrix
PowerPoster
Join Date
Dec 2009
Location
Moscow, Russia
Posts
3,589
Thread Starter
Member
Join Date
Aug 2010
Posts
38
PowerPoster
Join Date
Dec 2009
Location
Moscow, Russia
Posts
3,589
Also, there might be problems with encodings. Which one is the default one for your
system?
#define true ((rand() % 2)? true: false) // Debug THAT!
Rate the posts that helped you!
Evaluating Expressions | Simple POP3 Protocol Realization | Timers explained | Delegates explained | A secure login to your
app sample | Numeric Textbox | Printer.Print (or I miss VB6 printing in .Net) | Shuffling any IEnumerable | Marshalling
structures to byte arrays and back | Invoking a method by its name | Plugins architecture WITHOUT references | Permuter
that permutes everything | Encrypt/Decrypt your stuf | Snake Game | Enumerating all Network Adapters in the system |
How to make a web request
Thread Starter
Member
Join Date
Aug 2010
Posts
38
PowerPoster
Join Date
Feb 2002
Location
Canada, Toronto
Posts
5,708
in the "OnClick" event of the submit button on the client side, and override the
plain password text with the encrypted one. This way only the encrypted password
will be sent to the server.
Also, MD5 is not strong enough, use SHA instead, you can find in the same link
javascript code to encrypt all the way to 512 bit...
Reply With Quote
Feb 10th, 2011, 11:33 AM #14
Dimitris4463
Thread Starter
Member
Join Date
Aug 2010
Posts
38
PowerPoster
Join Date
Dec 2009
Location
Moscow, Russia
Posts
3,589