Hello,

I'm a newbie in VB.Net. I'm creating an application using VB.Net. I have a loggin form which has a username and a password textboxes. I have the following code which is working, which I believe is actually validating the username and password. My hicccup now that I need help is with the If...Else statement. How do I write the codes for the If......Else part, like if the username is correct and the password is wrong then the msg should be "The Password is incorrect" or vise versa, or if both are incorrect then it should also generate a message. My workin code is below, please help with the if...else bit.

Try
            conn.Open()
    Dim SelectStm As String = "SELECT * FROM tblStaff WHERE UserName = @UserName AND Password = @Password"
    Dim objCmd As SqlCommand = New SqlCommand(SelectStm, conn)
            objCmd.Parameters.AddWithValue("@UserName", txtUserName.Text)
            objCmd.Parameters.AddWithValue("@Password", Trim(txtPassword.Text))

    Dim objAdpt As SqlDataAdapter = New SqlDataAdapter(objCmd)

    Dim objDt As DataTable = New DataTable() 'Dim objDt As DataTable
            objAdpt.Fill(objDt)                      '    objDt = New DataTable() or Overall is: "Dim objDt As New DataTable"

            If objDt.Rows.Count > 0 Then
    'If user is found, then show the following
                Me.grpAttendenceCheck.Visible = True
                Me.grpAttendenceCheck.Size = New System.Drawing.Size(510, 157)
                Me.grpPrintTime.Visible = False

            Else

                If (MessageBox.Show("The username and password is incorrect, please confirm and enter them again", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK) Then
                    txtPassword.Text = ""
                    txtUserName.Text = ""
                    txtUserName.SelectedIndex = -1
                Else
                    End
                End If


            End If
            conn.Close()

        Catch ex As Exception
            MessageBox.Show("Failure")

        End Try

See if this helps.
2 TextBoxes (txtUserName and txtPassword), 1 Button(Button1)

Public Class Form1

    Private myCoolUserName As String = "codeorder"
    Private myCoolPassword As String = ".net"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not txtUserName.Text = myCoolUserName Then '// check if User Name is NOT Correct.
            MsgBox("Incorrect User Name.", MsgBoxStyle.Critical) '// Notify User.
            txtUserName.Text = "" : txtPassword.Text = "" '// Clear both TextBoxes.
            txtUserName.Select()
            Exit Sub '// skip remaining code.
        ElseIf txtUserName.Text = myCoolUserName AndAlso Not txtPassword.Text = myCoolPassword Then '// if User Name is Correct and Password is NOT.
            MsgBox("Incorrect Password.", MsgBoxStyle.Critical) '// Notify User.
            txtPassword.Text = "" '// Clear only the Password TextBox.
            txtPassword.Select()
            Exit Sub '// skip remaining code.
        Else
            MsgBox("Login Successful.", MsgBoxStyle.Information) '// Run Login code here.
        End If
    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.