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.

    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


                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
                End If

            End If

        Catch ex As Exception

        End Try

Edited 5 Years Ago by __avd: Added [code] tags.

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.
            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.
            Exit Sub '// skip remaining code.
            MsgBox("Login Successful.", MsgBoxStyle.Information) '// Run Login code here.
        End If
    End Sub
End Class

Edited 5 Years Ago by codeorder: n/a

This article has been dead for over six months. Start a new discussion instead.