Wednesday, July 5, 2017

connect or login

=============================  connect or login
 Private Sub ConnectToSQL()
        Dim Password As String
        Dim userName As String
        Try
            If (con.State = ConnectionState.Open) Then

                con.Close()
            End If
            con.Open()
            cmd.Connection = con
            cmd.CommandText = " SELECT  Username, Password FROM   tbUser where Username = '" & txtUsername.Text & "' and Password = '" & txtPassword.Text & "'  "
            Dim lrd As SqlDataReader = cmd.ExecuteReader()
            If lrd.Read() Then
                userName = lrd("Username").ToString()
                Password = lrd("Password").ToString()
                If Password = txtPassword.Text And userName = txtUsername.Text Then
                    MessageBox.Show("Logged in successfully as " & userName, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Form2.Show()
                    Me.Hide()
                    txtPassword.Text = ""
                    txtUsername.Text = ""
                End If
            Else
                MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtPassword.Text = ""
                txtUsername.Text = ""
            End If

        Catch ex As Exception
            MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
        Finally
            con.Close() 'Whether there is error or not. Close the connection.
        End Try
    End Sub

=====================================

No comments:

Post a Comment