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
Visual Basic.NET

Avatar of undefined
Last Comment
searchsanjaysharma

8/22/2022 - Mon
smilitaru

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
Nasir Razzaq

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
searchsanjaysharma

ASKER
great
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23