Avatar of searchsanjaysharma
searchsanjaysharma
 asked on

How to correct this error in Vb.Net.

I have a put a time on Vb.NEt windows form, which fethches the data every 15 seconds. but the following error in starting.
Object reference not set to an instance of an object.

Timer1_Tick
Interval - 15000

Try
            If (txtauin.Text.Trim() <> "") Then
                con = New SqlConnection("Data Source=172.16.3.17;Initial Catalog=testadms;User Id=sa;Password=admin#cgc;")
                s1 = "select isnull(flagah,0),isnull(flagf,0),isnull(flagr,0) from mststudents where uin ='" & txtauin.Text.Trim() & "' and sname is not null"
                If (con.State = ConnectionState.Closed) Then
                    con.Open()
                End If
                da1 = New SqlDataAdapter(s1, con)
                dt1 = New DataTable
                da1.Fill(dt1)
                If (dt1.Rows.Count = 0) Then
                    MessageBox.Show("No Record Found")
                    txtauin.Text = ""
                Else
                    If (Convert.ToInt32(dt1.Rows(0)(0).ToString()) = 1) Then
                        btnah.BackColor = Color.Green
                        btnah.Visible = True

                    Else
                        btnah.BackColor = Color.Red
                        btnah.Visible = True

                    End If
                    If (Convert.ToInt32(dt1.Rows(0)(1).ToString()) = 1) Then
                        btnf.BackColor = Color.Green
                        btnf.Visible = True
                    Else
                        btnf.BackColor = Color.Red
                        btnf.Visible = True
                    End If
                    If (Convert.ToInt32(dt1.Rows(0)(2).ToString()) = 1) Then
                        btnr.BackColor = Color.Green
                        btnr.Visible = True
                    Else
                        btnr.BackColor = Color.Red
                        btnr.Visible = True
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("Error Occurred " + ex.ToString())
        Finally
            If ConnectionState.Open Then
                con.Close()
            End If
        End Try
Visual Basic.NET

Avatar of undefined
Last Comment
searchsanjaysharma

8/22/2022 - Mon
Dirk Haest

Where do you get that error ? Are you sure that you declared all the variables (and initiated them with a value ) ?
searchsanjaysharma

ASKER
There the variables, but initialize how,? as it is getting at run time only
    Dim con As SqlConnection
    Dim da As SqlDataAdapter
    Dim da1 As SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As DataSet
    Dim dt As DataTable
    Dim dt1 As DataTable
    Dim s As String
    Dim s1 As String
Dirk Haest

Where do you get that error ?
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
searchsanjaysharma

ASKER
I think, please read the question properly.
The error comes when the screen is idle.
asfahani

at finally there is no sense for the condition because it is always true

instead write:

  If con.State = ConnectionState.Open Then
                con.Close()
   End If


Everything is supposed to be ok
searchsanjaysharma

ASKER
Thats what i earlier put con.close() then also error is coming
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
searchsanjaysharma

ASKER
I think every 15 second this query is fired, so probably, some other event may be fired and then the the con.close() If i change the connection object name then it wont cause i suppose/
ASKER CERTIFIED SOLUTION
Dirk Haest

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
systan

try to comment temporary to trace the error, and one by one to run uncomment it.
If (dt1.Rows.Count = 0) Then
                    MessageBox.Show("No Record Found")
                    ''//txtauin.Text = ""
Else
                   
                    'If (dt1.Rows.Count = 0) Then
                    '    MessageBox.Show("No Record Found")
                    '    ''//txtauin.Text = ""
                    'Else
                    '    If (Convert.ToInt32(dt1.Rows(0)(0).ToString()) = 1) Then
                    '        btnah.BackColor = Color.Green
                    '        btnah.Visible = True

                    '    Else
                    '        btnah.BackColor = Color.Red
                    '        btnah.Visible = True

                    '    End If
                    '    If (Convert.ToInt32(dt1.Rows(0)(1).ToString()) = 1) Then
                    '        btnf.BackColor = Color.Green
                    '        btnf.Visible = True
                    '    Else
                    '        btnf.BackColor = Color.Red
                    '        btnf.Visible = True
                    '    End If
                    '    If (Convert.ToInt32(dt1.Rows(0)(2).ToString()) = 1) Then
                    '        btnr.BackColor = Color.Green
                    '        btnr.Visible = True
                    '    Else
                    '        btnr.BackColor = Color.Red
                    '        btnr.Visible = True
                    '    End If
End If

Try to check also if you have set more than a row or column.
try to use column count and row count before setting it to final.
searchsanjaysharma

ASKER
I didnt get rowcount and columncount concept
Your help has saved me hundreds of hours of internet surfing.
fblack61
asfahani

can you replace the .ToString by System.convert.tostring

for example

dt1.rows(0)(2).tostring Is Replace by System.Conver.ToString(dt1.rows(0)(2))
systan

ahm, you have it,
If (dt1.Rows.Count = 0) Then
 MessageBox.Show("No Record Found")
  ''//txtauin.Text = ""comment test
Return
Endif

but try to use,
for statement
ie.,
for x as integer = 0 to dt1.rows.count
for y as integer = 0 to dt1.columns.count
If (Convert.ToInt32(dt1.Rows(x)(y).ToString()) = 1) Then

Endif
next y
next x
searchsanjaysharma

ASKER
ok
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.