How do I code a login to my VB.net program

  • Thread starter Thread starter B&W
  • Start date Start date

B&W

B&W

Soldato
Joined
3 Oct 2003
Posts
7,676
Location
Birmingham
Hi, basically I have textboxes for users to input their username and password.

My application is a front end to manipulate and view information from a access database.

I only require one username and one password.

untitledri4.png
 
Shadez said:
simple store the user info in the DB then run a query with the user name and password from the text boxes if it returns results display the main page if not then give them a error message.

Hi, thanks for the reply. Can you give me some more information on how I would do this.

I guess I would make a new table in the database with 2 fields, user and pwd.

Then I guess I'd have to use some sort of select statement?
 
marc2003 said:
why would you have a confirm password box on a login screen? :p they're only needed when changing/creating a password to prevent accidentally setting it with a typo in it. :)

Yes your right I will remove the confirmation box.
 
Well i tried the database/select statement way.

This is what ive done so far.

Code:
Imports System.Data.OleDb
Public Class frmUserLogin

    Dim mypath = Application.StartupPath & "\Learn 4 Business.mdb"
    Dim Password = ""
    Private strConn As String = "Provider=Microsoft.Jet.OleDB.4.0;Data Source=E:\L4B\L4B\Learn 4 Business.mdb"
    Dim cmd As New OleDb.OleDbCommand
    Dim conn As New OleDb.OleDbConnection

    Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
        Me.Close()
        frmL4B.ShowDialog()
    End Sub

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        Dim conn As OleDbConnection = New OleDbConnection
        conn.ConnectionString = strConn
        conn.Open()

        Dim cmd As OleDbCommand = New OleDbCommand

        cmd.CommandText = "SELECT UserName, Password FROM tblPassword WHERE UserName = '" & txtusername.Text & "' AND Password = '" & txtpassword.Text & "'"""
        cmd.CommandType = CommandType.Text
        cmd.Connection = conn

        Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader
        Try
            conn.Open()

        Catch ex As InvalidOperationException
            MsgBox(ex.Message)

        End Try
        Try
            If dr.Read = False Then
                MessageBox.Show("Authentication Failed...")
            Else
                MessageBox.Show("Login Successful...")

            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

        If conn.State <> ConnectionState.Closed Then
            conn.Close()

        End If

        frmMenu.Show()
        Me.Close()
    End Sub
End Class

I get the following error and it points to the datareader line:

Syntax error in string in query expression 'UserName = 'zahid' AND Password = 'kenya'"'.
 
Dr_Evil said:
You're seriously wasting your time trying to rewrite something that's already there in .Net. You can create the database as .mdf or sql server, either new or existing database. All the components for logging in/out are already there for you to use, you don't need to write this.

:confused:

I need to design a front end for the database, I cant just use the database on its own.
 
Shadez said:
Like i said it might be worth looking at DrE's solution, im an old vb6 developer who has resently moved to .net and dont know everything about it.




well this is a kick your self error..... look at the quotes at the end of the string.

:o . can login now with user and pwd from the db. however its kinda pointless since you can supply an incorrect user/pwd and still access the rest of the program.
 
yeh. silly me. still needs to be fine tuned but more or less works. thx for the help guys.
 
Back
Top Bottom