Problem object reference not set to an instance of an object

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 2851 Views - Last Post: 19 February 2012 - 06:40 PM Rate Topic: -----

#1 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Problem object reference not set to an instance of an object

Posted 18 February 2012 - 09:20 PM

This code is for TIME IN BUTTON
Private Sub cmdIn_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdIn.Click
        lblDlog.Text = lbltime.Text
        MsgBox("Log in successful")


This code is for TIME OUT BUTTON
Private Sub cmdOut_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOut.Click
        Dim ssql As String

        If lblDlog.Text = "" Then
            MsgBox("Failed to save Log in first")
            Exit Sub
        End If
        ssql = "INSERT INTO tbl_user (Date,Empname,TimeIN1,TimeOUT1) " & _
            "values(" & _
             "'" & lblDlog.Text & "'," & _
             "'" & lblEmpname.Text & "'," & _
             "'" & lblout.Text & "'," & _
             "'" & lblin.Text & "')"
        Dim com As New SqlCeCommand
        lblEmpname.Text = lbltime.Text
        Using con As SqlCeConnection = New SqlCeConnection("Data Source=" & My.Settings.ASDdb)
            Try
                With con
                    con.Open()
                    dr.Read()
                    com.CommandType = CommandType.Text
                    com.CommandText = ssql
                    com.Connection = con
                    com.Parameters.AddWithValue("@lblDlog", lblDlog.Text)
                    com.Parameters.AddWithValue("@lblEmpname", lblEmpname.Text)
                    com.Parameters.AddWithValue("@lblout", lblout.Text)
                    com.Parameters.AddWithValue("@lblin", lblin.Text)
                End With
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error!")
            Finally
                dr = com.ExecuteReader
                con.Close()
                Dim lv As ListViewItem = ListView1.Items.Add(lblEmpname.Text)
                lv.SubItems.Add(lblin.Text)
                lv.SubItems.Add(lblout.Text)
                lv.SubItems.Add(lblDlog.Text)
                MsgBox("Successfully Logged Out!", vbInformation, "Success!")
            End Try
        End Using

    End Sub


:helpsmilie:
my error is:
object reference not set to an instance of an object
and this..

Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: Problem object reference not set to an instance of an object

#2 Cherish Life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-February 12

Re: Problem object reference not set to an instance of an object

Posted 18 February 2012 - 09:38 PM

This simply means that there is not a value or placeholder of whatever you are trying to access, here is an example:

Listbox.items(6)

When there would only be 5 items.


I am not going to analyze your code for you, but based on the information I just gave you, I hope you can fix it yourself. Look for an index out of the bounds of the array.
Was This Post Helpful? 0
  • +
  • -

#3 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Problem object reference not set to an instance of an object

Posted 18 February 2012 - 10:01 PM

i solve it but..now the problem is the inserted value can't insert into the database. :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#4 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Problem object reference not set to an instance of an object

Posted 18 February 2012 - 10:44 PM

this is my code now..

Private Sub cmdOut_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOut.Click
        Dim com As New SqlCeCommand
        Using con As SqlCeConnection = New SqlCeConnection("Data Source=" & My.Settings.ASDdb)
            ssql = "INSERT INTO tbl_timrec (timeIN,timeOUT) " & _
            "values(" & _
             "'" & lblin.Text & "'," & _
             "'" & lblout.Text & "')"
            Try
                lblout.Text = lbltime.Text
                With con
                    con.Open()
                    com.CommandType = CommandType.Text
                    com.CommandText = ssql
                    com.Connection = con
                    com.Parameters.AddWithValue("@lblin", lblin.Text)
                    com.Parameters.AddWithValue("@lblout", lblout.Text)
                    dr = com.ExecuteReader
                    dr.Read()
                End With
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error!")
            Finally
                con.Close()
                Dim lv As ListViewItem = ListView1.Items.Add(lblin.Text)
                lv.SubItems.Add(lblout.Text)
                MsgBox("Successfully Logged Out!", vbInformation, "Success!")

            End Try
        End Using
    End Sub
End Class



i can't find the problem why the value can't put in the database. :no:

This post has been edited by bry090911: 18 February 2012 - 10:47 PM

Was This Post Helpful? 0
  • +
  • -

#5 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Problem object reference not set to an instance of an object

Posted 18 February 2012 - 11:41 PM

Please help guys.. i don't know what should i do.
Was This Post Helpful? 0
  • +
  • -

#6 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Problem object reference not set to an instance of an object

Posted 19 February 2012 - 12:40 AM

if its not throwing any errors, debug and see where its going wrong.

And I can see a problem with your SQL query. Since you're using parameterized queries, shouldn't it be something like this?

ssql = "INSERT INTO tbl_timrec (timeIN,timeOUT) VALUES (timeIN=@lblin, timeOUT=@lblout)"

Was This Post Helpful? 0
  • +
  • -

#7 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Problem object reference not set to an instance of an object

Posted 19 February 2012 - 01:17 AM

there was a message box says
i tried your:
ssql = "INSERT INTO tbl_timrec (timeIN,timeOUT) VALUES (timeIN=@lblin, timeOUT=@lblout)"
but this is the error..

There was an error parsing the query [Token line number=1, token line offset = 55, token in error ==]

and the the msgbox will pop up successfully logged out!


but still the same problem it didn't insert to the database..

THIS IS MY CODE NOW..
 Private Sub cmdOut_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOut.Click
        Dim com As New SqlCeCommand
        Using con As SqlCeConnection = New SqlCeConnection("Data Source=" & My.Settings.PATS)
            ssql = "INSERT INTO tbl_timrec (timeIN,timeOUT) VALUES (timeIN=@lblin, timeOUT=@lblout)"
            Try
                lblout.Text = lbltime.Text
                With con
                    con.Open()
                    com.CommandType = CommandType.Text
                    com.CommandText = ssql
                    com.Connection = con
                    com.Parameters.AddWithValue("@lblin", lblin.Text)
                    com.Parameters.AddWithValue("@lblout", lblout.Text)
                    dr = com.ExecuteReader
                    dr.Read()
                End With
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error!")
            Finally
                con.Close()
                Dim lv As ListViewItem = ListView1.Items.Add(lblin.Text)
                lv.SubItems.Add(lblout.Text)
                MsgBox("Successfully Logged Out!", vbInformation, "Success!")

            End Try
        End Using
    End Sub

This post has been edited by bry090911: 19 February 2012 - 01:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Problem object reference not set to an instance of an object

Posted 19 February 2012 - 01:49 AM

i don't know whats the steps on how to debug.
Was This Post Helpful? 0
  • +
  • -

#9 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 204
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Problem object reference not set to an instance of an object

Posted 19 February 2012 - 01:53 AM

http://www.dreaminco...4249-debugging/
Was This Post Helpful? 0
  • +
  • -

#10 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Problem object reference not set to an instance of an object

Posted 19 February 2012 - 02:12 AM

can't find the error..

Please help me. :( this is the code again..

 Private Sub cmdOut_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOut.Click
        Dim com As New SqlCeCommand
        Using con As SqlCeConnection = New SqlCeConnection("Data Source=" & My.Settings.PATS)
            ssql = "INSERT INTO tbl_timrec (timeIN,timeOUT) " & _
            "values(" & _
             "'" & lblin.Text & "'," & _
             "'" & lblout.Text & "')"
            Try
                lblout.Text = lbltime.Text
                With com
                    con.Open()
                    com.CommandType = CommandType.Text
                    com.CommandText = ssql
                    com.Connection = con
                    com.Parameters.AddWithValue("@lblin", lblin.Text)
                    com.Parameters.AddWithValue("@lblout", lblout.Text)

                End With
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error!")
            Finally
                dr = com.ExecuteReader
                dr.Read()
                con.Close()
                Dim lv As ListViewItem = ListView1.Items.Add(lblin.Text)
                lv.SubItems.Add(lblout.Text)
                MsgBox("Successfully Logged Out!", vbInformation, "Success!")

            End Try
        End Using
    End Sub

Was This Post Helpful? 0
  • +
  • -

#11 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Problem object reference not set to an instance of an object

Posted 19 February 2012 - 03:22 AM

Please help.. :(
Was This Post Helpful? 0
  • +
  • -

#12 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Problem object reference not set to an instance of an object

Posted 19 February 2012 - 03:53 AM

up this topic please... :/
Was This Post Helpful? 0
  • +
  • -

#13 kingsonprisonic  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 74
  • Joined: 09-July 09

Re: Problem object reference not set to an instance of an object

Posted 19 February 2012 - 04:27 AM

View Postbry090911, on 19 February 2012 - 03:53 AM, said:

up this topic please... :/


I modify little bit of your code...

Private Sub cmdOut_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOut.Click
        
        Using con As SqlCeConnection = New SqlCeConnection("Data Source=" & My.Settings.ASDdb)
          

Dim com As New SqlCeCommand("INSERT INTO tbl_timrec (timeIN,timeOUT) values('"+lblin.Text+"','"+lblout.Text+"')",con)
            Try
                lblout.Text = lbltime.Text
                With con
                    con.Open()
                    dr = com.ExecuteReader
                    dr.Read()
                End With
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error!")
            Finally
                con.Close()
                Dim lv As ListViewItem = ListView1.Items.Add(lblin.Text)
                lv.SubItems.Add(lblout.Text)
                MsgBox("Successfully Logged Out!", vbInformation, "Success!")

            End Try
        End Using
    End Sub
End Class



Now try this.. And reply further ....
Was This Post Helpful? 1
  • +
  • -

#14 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Problem object reference not set to an instance of an object

Posted 19 February 2012 - 04:39 AM

wait i`ll gonna try it

Nothings changed. :( again, there is no record. didn't work.
Was This Post Helpful? 0
  • +
  • -

#15 kingsonprisonic  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 74
  • Joined: 09-July 09

Re: Problem object reference not set to an instance of an object

Posted 19 February 2012 - 04:41 AM

First tell me what datatype u used for timeIN,timeOUT in the Table?????
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2