How to correct this error in Vb.Net.

searchsanjaysharma
searchsanjaysharma used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dirk HaestProject manager

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

Author

Commented:
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 HaestProject manager

Commented:
Where do you get that error ?
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

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

Commented:
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

Author

Commented:
Thats what i earlier put con.close() then also error is coming

Author

Commented:
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/
Project manager
Commented:
Is it a general connection-object that you are using in your project ? If so, try to use a local variable


Try
            If (txtauin.Text.Trim() <> "") Then
                dim myConnectionas SqlConnection= 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 (myConnection.State = ConnectionState.Closed) Then
                    myConnection.Open()
                End If
                da1 = New SqlDataAdapter(s1, myConnection)
                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 myConnection.State = ConnectionState.Open Then
                   myConnection.Close()
              End If 

        End Try 

Open in new window

Commented:
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.

Author

Commented:
I didnt get rowcount and columncount concept

Commented:
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))

Commented:
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

Author

Commented:
ok

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial