Hi All,

I sware I am loosing the will to live, I have tried to accomplish this by my-self for about 2 weeks now and I am still at square one, I prey someone can help me.

I am writing an application for booking conference rooms and I need a login form.

I have created my database called Users in sql and inserted 2 default login accounts, admin and default.

I dont know how to compare the data in my database to the data typed into the username and password text boxes on the login form.

Please see what I have so far

Imports System.Data.SqlClient
Public Class LoginForm1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        NewUser.Show()
    End Sub

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

    Private Sub LoginForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim conn As SqlConnection
        conn = New SqlConnection("Data Source=********;Initial Catalog=mtrmanager;Integrated Security=True")
        Try
            conn.Open()
        Catch ex As Exception
            MessageBox.Show("Connecting to Database Failed - Please Contact Systems Administrator")
        End Try
    End Sub

End Class

I know that I have to create a click event handler for the login button and put my code in there to compare the data, but can anyone help me out with the code before my head burns out.

Thanks very much

John

you are not comparing it..

on your form, in the ok button_click event

select username, password from users where username = " & txtusername.text & " and password = " & txtpassword.text & "

if rs.read
msgbox("GOOD")
continue..
else
msgbox("BAD")
continue
end if

oh course you want to set it up alot better than that breif example..

it lacks in alot of security ect. but that should give you an idea

you are not comparing it..

on your form, in the ok button_click event

select username, password from users where username = " & txtusername.text & " and password = " & txtpassword.text & "

if rs.read
msgbox("GOOD")
continue..
else
msgbox("BAD")
continue
end if

Hi Jlego,

Thanks for the help, what is rs.read? is rs, sql reader.

Thanks again

yeah

please mark as solved if you figured it out :P

please mark as solved if you figured it out :P

not just yet, I went to bed to give my head a rest, back on it today, hopefully I will be able to mark as solved by the end of tonight.

Ta

let me know if u have any issues

you are not comparing it..

on your form, in the ok button_click event

select username, password from users where username = " & txtusername.text & " and password = " & txtpassword.text & "

if rs.read
msgbox("GOOD")
continue..
else
msgbox("BAD")
continue
end if

Ok, I think I am getting somewhere, I am getting the error below.

System.Data.SqlClient.SqlException was unhandled
  Message="Invalid object name 'UserID'."

please see below my code

Imports System.Data.SqlClient
Public Class LoginForm1

    Dim conn As SqlConnection
    Dim rs As SqlDataReader

    Private Sub LoginForm1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn = New SqlConnection("Data Source=JOHN-ELLIS-PC;Initial Catalog=mtrmanager;Integrated Security=True")
        conn.Open()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        NewUser.Show()
    End Sub

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

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

        Dim cmd As New SqlCommand("Select Name,Password from UserID", conn)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader

        If dr.Read Then
            MsgBox("Login Accepted")
            Form1.Show()
            Me.Close()
        Else
            MsgBox("Login Failed, Please try Again")
            TextBox1.Focus()
            TextBox2.Focus()
        End If

    End Sub

End Class

Cheers

John

put a space between name and password...

is your user table name userid ?

but you are still doing it wrong..

select username, password from usertable where username = '" & textbox1.text & "' and password = '" & textbox2.text & "'

put a space between name and password...

is your user table name userid ?

I thought it was working for a second then but I just tried some incorrect details and it still logged me in, I have just seen your last post, yes I have missed that entire part of the code out, I swear I get blind as I get older.

Will keep you posted.

thanks

John

in your project properties...
make sure formlogin1 is the startup form.
close application when all forms are closed

on button login_click

form1.show
me.dispose


on form1_formclosed

application.exit

in your project properties...
make sure formlogin1 is the startup form.
close application when all forms are closed

on button login_click

form1.show
me.dispose


on form1_formclosed

application.exit

THANKYOU THANKYOU THANKYOU - It works a treat, can't thankyou enough for your help on this one, finally I can close the chapter on this part of the project.

Thanks again mate

John

no problem. good luck

i am stuck over here i am making grading system software and i have a user table in it. i want when a user logs in it should check the existance of that user whether he is there in database or not if not it should not go further..
please see below my code:

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=grade;Integrated Security=True;Pooling=False")
    Dim cmd As SqlCommand

    Try
        conn.Open()
        Dim query As String = "select * from Login where Username=@Username,Password=@Password"
        cmd = New SqlCommand(query, conn)
        cmd.Parameters.AddWithValue("@Username", txtusername.Text)
        cmd.Parameters.AddWithValue("@Password", txtpassword.Text)
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        If reader.Read = True Then
            MessageBox.Show("good")
        End If
        MessageBox.Show("user found")
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try

End Sub

End Class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.