How do i go about creating an exception handler when i want to login to my form to access the rest of my forms. The username and password are linked to server.What if it doesnt connect to server? here is coding for me to login:

 Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    If (txtUName.Text = "-") Or (txtUName.Text = "0") Or (txtUName.Text = "") Or (txtPass.Text = "0") Or (txtPass.Text = "-") Or (txtPass.Text = "") Then
        MessageBox.Show("Invalid Entry", "ErrorMessageBox", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtUName.Text = ""
        txtPass.Text = ""
        Return
    End If
    empeid = txtUName.Text
    DatasetLogin1.Clear()
    sqldLoginadapter.SelectCommand.Parameters("@EID").Value = txtUName.Text
    sqldLoginadapter.SelectCommand.Parameters("@EPass").Value = txtPass.Text
    sqldLoginadapter.Fill(DatasetLogin1, "empdetails")
    If (DatasetLogin1.Tables("empdetails").Rows.Count > 0) Then
        MessageBox.Show("Login Successfull! Welcome Employee: " & empeid, "MessageBox", MessageBoxButtons.OK, MessageBoxIcon.Information)
        txtUName.Text = ""
        txtPass.Text = ""
        Me.Hide()
        PDL_MainMenu_Form.Show()
    Else
        MessageBox.Show("Not Registered Employee Details!. Please Try Again", "ErrorMessageBox", MessageBoxButtons.OK, MessageBoxIcon.Error)
        txtUName.Text = ""
        txtPass.Text = ""
        Return
    End If
End Sub

Thanks

You should use a Try...Catch statement on your sqldLoginadapter.Fill(DatasetLogin1, "empdetails").
Read here for more info in try and catch: http://msdn.microsoft.com/en-us/library/fk6t46tz

Whenever u write a coding for Database connection use the try catch block

Try

' *ur complete code goes here * 

Catch ex as Exception
MsgBox(ex.Message) ' if u get an error in above coding it will be passed to catch block and will display the error
End try

And also use the finally bloack to close ur connection to database or to dispose any objects...

Thanks very much guys for the assist

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.