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,668
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
 
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.
 
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. :)
 
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.
 
B&W said:
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?

exactly that.
 
noob said:
If you only have one username and password don't bother storing it in a DB just use global variables.

http://en.allexperts.com/q/Visual-Basic-1048/vb-net-global-variables.htm

you dont want to store them as globals because you wont be able to change them. Although you could store a single uid and pw in the registry.


.Net Membership /Role provider components?? not looked at them before might be worth checking this out.
 
Shadez said:
you dont want to store them as globals because you wont be able to change them. Although you could store a single uid and pw in the registry.

Depends if he wants a quick dirty hack. ;) Judging by his response and how much he wants to learn and his available time it might be fine.
 
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'"'.
 
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.
 
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.
 
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.


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

Syntax error in string in query expression 'UserName = 'zahid' AND Password = 'kenya'"'.

well this is a kick your self error..... look at the quotes at the end of the string.
 
IF username.text = "username" and password.text = "password" Then
formmainmenu.show()
Else
Msgbox "get out ere"
End If

;)
 
Trust me - you don't need to code your own login screens, nor do you need to design your user security tables in your database. This has all been provided for already by the .Net framework.

1. There's a script that will generate all your database tables. It holds an extensive set of info for user and membership security data.

2. .Net has it's own login components. In VS2005 you can drag the Login control onto your page, which provides for entering user credentials and logging the user in. Also you can enable the "Forgotten password" mechanism.

You need to watch the video i posted earlier and have a good read on that site i posted earlier. They will help you understand how to use this stuff.
 
Dr_Evil said:
Trust me - you don't need to code your own login screens, nor do you need to design your user security tables in your database. This has all been provided for already by the .Net framework.

1. There's a script that will generate all your database tables. It holds an extensive set of info for user and membership security data.

2. .Net has it's own login components. In VS2005 you can drag the Login control onto your page, which provides for entering user credentials and logging the user in. Also you can enable the "Forgotten password" mechanism.

You need to watch the video i posted earlier and have a good read on that site i posted earlier. They will help you understand how to use this stuff.

I agree with what your saying I've used these controls in ASP.NET they are fantastic. I've done some VB but only version 6. I guess with .NET it's all the same now.

I think this video is better though - http://www.asp.net/learn/videos/view.aspx?tabid=63&id=6
 
Back
Top Bottom