954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Login form in VB.net

does anybody know how to create a login form in vb.net? I have an access database and I am trying to create a login form with a user name and password to have multiple users login.

Please help.

jaz854
Newbie Poster
9 posts since Jun 2007
Reputation Points: 13
Solved Threads: 0
 
arjunsasidharan
Practically a Posting Shark
826 posts since Aug 2006
Reputation Points: 347
Solved Threads: 13
 

Dim com As New MySqlCommand
Dim dr As MySqlDataReader
Dim uSQL As String

Dim ctr As Integer = 0

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

Call OpenDB()

uSQL = "SELECT username, password, user_account from quser where username = '" & txtUsername.Text & "' and password = '" & txtPassword.Text & "'"

Try
conn.Open()

com = New MySqlCommand(uSQL, conn)
dr = com.ExecuteReader

ctr += 1

If Not dr.HasRows And ctr = 1 Then
lblTrial.Text = "You have 2 trials remaining!"
MsgBox("Wrong Username or password.", MsgBoxStyle.Information)
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.Focus()

ElseIf Not dr.HasRows And ctr = 2 Then
lblTrial.Text = "You have 1 trial remaining!"
MsgBox("Wrong Username or password.", MsgBoxStyle.Information)
txtUsername.Text = ""
txtPassword.Text = ""
txtUsername.Focus()

ElseIf Not dr.HasRows And ctr = 3 Then
lblTrial.Text = "You have no trial remaining!"
MsgBox("Wrong Username or password." & vbCrLf & "Please restart the program!", MsgBoxStyle.Information)

Else
While dr.Read
MainForm.Show()
txtUser.Text = dr("user_account").ToString()
Me.Hide()
'MsgBox("Hello World")
End While
End If

If ctr = 3 Then
Me.Close()
End If


Catch ex As Exception
conn.Close()
End Try

End Sub

eilrig
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

1.)Create a database in Ms. Access e.g. create table, fields - username and password and save the table as LOGIN or what you want.
2.)Click on your table you created (LOGIN or what you wrote) and fill in the data username - yes and password - no.
3.)Go to VB.NET program and open a form and connect the database you created.
4.)To connect a database, check on the top of the menu bar in VB.NET and look for data, then click on add new data source.
5.)Data Source Configuration Wizard appear, click on next. Then you can see a new connection tab, click onto it.
6.)Add connection appears,on Data source the default you can see is Microsoft SQL Server Database File (SqlClient) but since we are using Ms Access we will change it, by clicking the change tab next to it.
7.)Then browse for your database file name by clicking on browse and locate it where it is on your computer (place where you saved your database).
8.)Click on ok,next.
9.)Check the tables and views and click finish.
10.)Go to an empty form and drag the Username and password from the data source. If you cannot see the data source,go to data and then show data sources.
11.) Double click on your form in an empty space and copy this(under Public Class Form1 and above Private sub form1.....):

Dim DbCon As New OleDb.OleDbConnectio
Dim dbUp As New OleDb.OleDbCommand
Dim Read As OleDb.OleDbDataReader

11.)Go back to the design form.
12.)Add 2 buttons on the same form, rename both the buttons,one saying ok and the other as cancel.
13.)Double click on the ok button and enter the codes as follows:


Private Sub Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DbCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\User\Desktop\Database.mdb"
DbCon.Open()
dbUp.Connection = DbCon
dbUp.CommandType = CommandType.Text
dbUp.CommandText = "Select * from LOGIN where Username=yes and Password=no "
dbUp.Parameters.Add("Username", Data.OleDb.OleDbType.Variant)
dbUp.Parameters.Add("Password", Data.OleDb.OleDbType.Variant)
dbUp.Parameters("Username").Value = UsernameTextBox.Text
dbUp.Parameters("Password").Value = PasswordTextBox.Text
Read = dbUp.ExecuteReader
With Read
If .Read Then
Me.Hide()
Form2.Show()
Else
UsernameTextBox.Clear()
PasswordTextBox.Clear()
MessageBox.Show("Invalid Username or Password")
UsernameTextBox.Focus()
End If
End With

14.)Then go to the design page again and double click on cancel button and copy the following:

If MessageBox.Show("Do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
End
Else
End If
End Sub

15.) Once you have done that run your program and check whether the codes worked.

Jollyyy100
Junior Poster in Training
84 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

do you know already how to do a log in form in vb.net?

akotoh!
Newbie Poster
2 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

yes, ofcourse, you need to use the toolbox, it comes with the buttons and all the little things you use to create your GUI. its very easy. then simply code your program.

else the internet is a pretty good resource :P

RachaelKlein159
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

1.)Create a database in Ms. Access e.g. create table, fields - username and password and save the table as LOGIN or what you want. 2.)Click on your table you created (LOGIN or what you wrote) and fill in the data username - yes and password - no. 3.)Go to VB.NET program and open a form and connect the database you created. 4.)To connect a database, check on the top of the menu bar in VB.NET and look for data, then click on add new data source. 5.)Data Source Configuration Wizard appear, click on next. Then you can see a new connection tab, click onto it. 6.)Add connection appears,on Data source the default you can see is Microsoft SQL Server Database File (SqlClient) but since we are using Ms Access we will change it, by clicking the change tab next to it. 7.)Then browse for your database file name by clicking on browse and locate it where it is on your computer (place where you saved your database). 8.)Click on ok,next. 9.)Check the tables and views and click finish. 10.)Go to an empty form and drag the Username and password from the data source. If you cannot see the data source,go to data and then show data sources. 11.) Double click on your form in an empty space and copy this(under Public Class Form1 and above Private sub form1.....):

Dim DbCon As New OleDb.OleDbConnectio Dim dbUp As New OleDb.OleDbCommand Dim Read As OleDb.OleDbDataReader

11.)Go back to the design form. 12.)Add 2 buttons on the same form, rename both the buttons,one saying ok and the other as cancel. 13.)Double click on the ok button and enter the codes as follows:

Private Sub Ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DbCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\User\Desktop\Database.mdb" DbCon.Open() dbUp.Connection = DbCon dbUp.CommandType = CommandType.Text dbUp.CommandText = "Select * from LOGIN where Username=yes and Password=no " dbUp.Parameters.Add("Username", Data.OleDb.OleDbType.Variant) dbUp.Parameters.Add("Password", Data.OleDb.OleDbType.Variant) dbUp.Parameters("Username").Value = UsernameTextBox.Text dbUp.Parameters("Password").Value = PasswordTextBox.Text Read = dbUp.ExecuteReader With Read If .Read Then Me.Hide() Form2.Show() Else UsernameTextBox.Clear() PasswordTextBox.Clear() MessageBox.Show("Invalid Username or Password") UsernameTextBox.Focus() End If End With

14.)Then go to the design page again and double click on cancel button and copy the following:

If MessageBox.Show("Do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then End Else End If End Sub

15.) Once you have done that run your program and check whether the codes worked.


i haveing the problem withRead = dbUp.ExecuteReader

Attachments Untitled.png 26.57KB
alabai
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

I personal want a code to upload an image with a textbox

Khosa
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

hi Alabai

i am haveing the same problem with Read = dbUp.ExecuteReader.
did u sort out this issue, if yes please tell me how

edriso
Newbie Poster
2 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Does anyone know how to do a login form in vb.net 2008 with an sql database?

tendaimare
Junior Poster in Training
71 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim count As Integer
 
        Try
 
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            Else
                conn.Open()
                userstring = "Select * from Hotel.dbo.Users where UserName = '" & UsernameTextBox.Text & "' and Password = '" & PasswordTextBox.Text & "'"
                usercmd = New SqlCommand(userstring, conn)
                count = usercmd.ExecuteScalar
 
                If count > 0 Then
                    MsgBox("records exist")
                    form2.show()
                    'load main form of application
 
                Else
                    MsgBox("no records found")
                   're-enter login username and password
 
                End If
 
                conn.Close()
 
            End If
 
        Catch ex As Exception
            Throw ex
            conn.Close()
 
        End Try
 
    End Sub
Netcode
Veteran Poster
1,021 posts since Jun 2009
Reputation Points: 43
Solved Threads: 67
 

well why wont ya try this stuff

Imports System.Data.OleDb

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim sql = "SELECT User_Name,password FROM PasswordTable WHERE User_Name = '" & usertxt.Text & "' AND password = '" & passtxt.Text & "'"

cmd = New OleDbCommand(sql, conn)
Dim dr As OleDbDataReader = cmd.ExecuteReader
Try
conn.Open()

Catch ex As InvalidOperationException
MsgBox(ex.Message)

End Try
Try
Catch ex As System.InvalidOperationException
If dr.Read = False Then
MessageBox.Show("Authentication Failed...")
Else
MessageBox.Show("Login Successful...")
Me.Hide()
Form1.Show()
End If
End Try
conn.Close()
End Sub

kensh
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

@jollyyy100

I'm having problem with the DbCon.Open()

Lhey
Newbie Poster
1 post since May 2012
Reputation Points: 0
Solved Threads: 0
 

Post: Markdown Syntax: Formatting Help
You