Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,547 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,836 people online right now. Registration is fast and FREE... Join Now!




Vb.net Login linked to database

 
Reply to this topicStart new topic

Vb.net Login linked to database

revolution14
22 Aug, 2008 - 04:30 AM
Post #1

New D.I.C Head
*

Joined: 11 Dec, 2007
Posts: 41


My Contributions
hello there!!

what i am trying to do is have a login screen that asks the user for a username and password, it will then check to see if the username and password are in the system and if they are it will let them in.

the database i am trying to connect to has 3 fields (Id,Username,Password)
Id is listed as the primary key and is set to auto number.

I dont really know much about vb.net however here is the code i have come up with so far

CODE

Imports System
Imports System.Data
Imports System.Data.OleDb

Public Class LoginForm1

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub cmdEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnter.Click

        Dim strDSN As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\In The Know\Documents\Database1.mdb"
        Dim strSQL As String = "SELECT * FROM Developer"

        Dim myConn As New OleDbConnection(strDSN)
        Dim myCmd As New OleDbCommand(strSQL, myConn)
        Dim Usrdata As OleDbDataReader = Nothing
        myConn.Open()
        Usrdata = myCmd.ExecuteReader()

        Dim strName As String
        Dim strPass As String
        Dim pesan As String 'pesan is Indonesian and it is the same with Message in english'

        strName = txtUsr.Text
        strPass = txtPwd.Text


        Do Until Usrdata.Recordset.EOF
            If Usrdata.Recordset.Fields("username").Value = strName And Usrdata.Recordset.Fields("password").Value = strPass Then
                Me.Hide()
                Form2.Show()    'if the login succeed then form that contain employee info shown
                Exit Sub

            Else
                Usrdata.Recordset.MoveNext()
            End If

        Loop


    End Sub

End Class


any help would be much appreciated
User is offlineProfile CardPM
+Quote Post

revolution14
RE: Vb.net Login Linked To Database
22 Aug, 2008 - 04:40 AM
Post #2

New D.I.C Head
*

Joined: 11 Dec, 2007
Posts: 41


My Contributions
by the way the database im trying to connect to is an access database and the directory is already listed in my code.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Vb.net Login Linked To Database
22 Aug, 2008 - 06:26 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,947



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
If you are going to query a database then let the query do all the work. No need to loop through the entire table when you are only seeking one record.
CODE

    Private Sub cmdEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnter.Click

        Dim strName As String
        Dim strPass As String
        Dim pesan As String 'pesan is Indonesian and it is the same with Message in english'

        strName = txtUsr.Text
        strPass = txtPwd.Text

        Dim strDSN As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\In The Know\Documents\Database1.mdb"
        Dim strSQL As String = "SELECT id FROM Developer where username = '" & strName _
                        & "' AND Password = '" & strPass & "'"

        Dim myConn As New OleDbConnection(strDSN)
        Dim myCmd As New OleDbCommand(strSQL, myConn)
        Dim Usrdata As String = ""
        Try
            myConn.Open()
            Usrdata = myCmd.ExecuteScalar()

            If Not String.IsNullOrEmpty(Usrdata) Then
                Me.Hide()
                Form2.Show()    'if the login succeed then form that contain employee info shown
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


    End Sub

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:56PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month