Link to home
Start Free TrialLog in
Avatar of searchsanjaysharma
searchsanjaysharma

asked on

How to rectify error in Vb.Net Windows form for click on button for db connectivity issues

There is a textbox txtauin and button btnuin.
The users enters the uin and click on uin and get the details.
But if txtauin is blank and user clicks on btnuin, then the error comes Object reference not set to instance of an object.

Code is here.

    Private Sub btnuin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnuin.Click
        Try
            If (txtauin.Text.Trim() <> "") Then
                con = New SqlConnection("Data Source=172.16.3.17;Initial Catalog=testadms;User Id=sa;Password=admin#cgc;")
                s = "SELECT isnull(ahstatus,'') from mststudents where uin='" & txtauin.Text.Trim() & "'"

                If (con.State = ConnectionState.Closed) Then
                    con.Open()
                End If
                da = New SqlDataAdapter(s, con)
                dt = New DataTable

                da.Fill(dt)

                If (dt.Rows.Count = 0) Then
                    MessageBox.Show("No Record Found")
                    txtauin.Text = ""
                Else
                    'Timer1_Tick()
                    txtahstatus.Text = dt.Rows(0)(0).ToString()
                                End If
            end if

            Else
                MsgBox("Invalid UIN")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString())
        Finally
            con.Close()
        End Try
Avatar of smilitaru
smilitaru
Flag of Romania image

the problem is with the code from finally

Finally
            con.Close()
End Try

The con is Nothing if there is no UIN
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of searchsanjaysharma
searchsanjaysharma

ASKER

great