Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
M doing a project in my college.I created a login page for my software.N its working too.But when i change username and password in database ,its not working..moreover when i leave any one of the fields empty it has to give a error msg about that particular field..eg:passsword is emptyCan u help me plz!!
here is my code:
Imports System.Data
Imports System.Data.SqlClient
Public Class login1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""
        TextBox2.Text = ""


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (ComboBox1.Text = "select") Then
            MessageBox.Show("please select user or admin")
            TextBox1.Text = ""
            TextBox2.Text = ""

        End If

        Try

            If (ComboBox1.Text = "admin") Then
                Dim str As String = "select * from log where adm = 'admin'; "
                Dim con As New SqlConnection()
                con.ConnectionString = "Data Source=PROJECT-856F221\SQLEXPRESS;Initial Catalog=farma;Integrated Security=True"
                con.Open()
                Dim cmd As New SqlCommand(str, con)
                Dim reader As SqlDataReader
                reader = cmd.ExecuteReader
                Dim found As Integer = 0
                Dim un, pwd, un1, pwd1, grp, grp1 As String
                grp = ComboBox1.Text.Trim()
                un = TextBox1.Text.Trim()
                pwd = TextBox1.Text.Trim()
                While (reader.Read())
                    grp1 = reader.GetString(0).Trim()
                    un1 = reader.GetString(1).Trim()
                    pwd1 = reader.GetString(2).Trim()
                    If (grp = grp1 And un = un1 And pwd = pwd1) Then
                        found = 1
                        Exit While
                    End If
                End While
                reader.Close()
                con.Close()
                If found = 0 Then
                    Label3.Visible = True
                    Label3.Text = "User Name or Password Incorrect"
                Else
                    adminhom.Show()
                    Me.Hide()


                End If
            ElseIf (ComboBox1.Text = "user") Then
                user_login()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Sub user_login()
        Try

            Dim str As String = "select * from log where adm = 'user'"
            Dim con As New SqlConnection()
            con.ConnectionString = "Data Source=PROJECT-856F221\SQLEXPRESS;Initial Catalog=farma;Integrated Security=True"
            con.Open()
            Dim cmd As New SqlCommand(str, con)
            Dim reader As SqlDataReader
            reader = cmd.ExecuteReader
            Dim found As Integer = 0
            Dim un, pwd, un1, pwd1 As String
            un = TextBox1.Text.Trim()
            pwd = TextBox1.Text.Trim()
            While (reader.Read())
                un1 = reader.GetString(1).Trim()
                pwd1 = reader.GetString(2).Trim()
                If (un = un1 And pwd = pwd1) Then
                    found = 1
                    Exit While
                End If
            End While
            reader.Close()
            con.Close()
            If found = 0 Then
                Label3.Visible = True
                Label3.Text = "User Name or Password Incorrect"
            Else
                farmerhome.Show()
                Me.Hide()


            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label3.Visible = False

    End Sub


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

    End Sub
End Class
Posted
Updated 19-Feb-12 18:10pm
Comments
Varun Sareen 20-Feb-12 0:10am    
edit for: added pre tag
Varun Sareen 20-Feb-12 0:19am    
Please elaborate more on the login failure on re-login. Above code seems fine.
Ariel Riyo 22-Feb-12 3:13am    
Please post only the codes with problem. and i suggest you can just decalre the connection string in const so you will not copy ang paste the "data source.....". :)

1 solution

Dear Friend,

For the alert message box on the leave event of some textbox is:-
Private void TextBox1_Leave(sender As Object, e As System.EventArgs) Handles textBox1.Leave
{
 if(TextBox1.Text=="") then
    MessageBox.Show("passsword is empty"); //in your case
}


I hope this will help you out.

Thanks
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900