Link to home
Start Free TrialLog in
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
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Where do you get that error ? Are you sure that you declared all the variables (and initiated them with a value ) ?
Avatar of searchsanjaysharma
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
Where do you get that error ?
I think, please read the question properly.
The error comes when the screen is idle.
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
Thats what i earlier put con.close() then also error is coming
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
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium 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
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.
I didnt get rowcount and columncount concept
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))
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