Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. object reference not set to an instance of an object error

object reference not set to an instance of an object error

Scheduled Pinned Locked Moved Visual Basic
helpdatabasetutorialannouncement
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    str50
    wrote on last edited by
    #1

    I am getting this error when I run the program. I am not sure how to fix this error, if anybody can help me I will appreciate it. The program has two forms. Form1 has the search textbox and form2 takes the information from form1 and populates form2. I have attached the code for both forms, so you can see what I am talking about. Any help will be appreciated. Thanks Form1 Code: Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Dim oForm2Data As New Form2.Form2Data(Me.txtbarcodeid.Text) If oForm2Data.HasData Then Form2.Initialize(Me, oForm2Data) Else ' The Data does not exists for the item in the textbox End If End Sub Form2 Code: Dim dt As New DataTable Dim sqlStr As String = "SELECT * FROM testbackup" Dim sqlstr1 As String = "SELECT * FROM history" Dim connstr As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _ "Data Source = backup.mdb" Dim dataadapter As New OleDb.OleDbDataAdapter(sqlStr, connstr) Public Shared Sub Initialize(ByVal Owner As IWin32Window, ByRef oForm2Data As Form2Data) Dim result As DialogResult Dim frm2 As New Form2 With frm2 'Using TheData Populate each Control on Form2 End With Result = frm2.ShowDialog(Owner) If Result = System.Windows.Forms.DialogResult.OK Then With frm2 'Now update the values in TheData with its value in its Form2 Control End With 'Save Changes oForm2Data.SaveData() End If End Sub Public Class Form2Data Public HasData As Boolean Public TheData As DataRow Public Sub New(ByVal Value As String) Dim dsData As DataSet Dim stSQL As String stSQL = "Select * from testbackup Where barcodeid = '" & Value & "'" 'Database code to return dataset to dsData Select Case dsData.Tables(0).Rows.Count Case 1 'One Row Returned HasData = True TheData = dsData.Tables(0).Rows(0) Case 0 'No Rows Found HasData = False TheData = Nothing Case Else 'More then one row returned 'Decided how to handle HasData = True TheData = dsData.Tables(0

    M X 2 Replies Last reply
    0
    • S str50

      I am getting this error when I run the program. I am not sure how to fix this error, if anybody can help me I will appreciate it. The program has two forms. Form1 has the search textbox and form2 takes the information from form1 and populates form2. I have attached the code for both forms, so you can see what I am talking about. Any help will be appreciated. Thanks Form1 Code: Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Dim oForm2Data As New Form2.Form2Data(Me.txtbarcodeid.Text) If oForm2Data.HasData Then Form2.Initialize(Me, oForm2Data) Else ' The Data does not exists for the item in the textbox End If End Sub Form2 Code: Dim dt As New DataTable Dim sqlStr As String = "SELECT * FROM testbackup" Dim sqlstr1 As String = "SELECT * FROM history" Dim connstr As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _ "Data Source = backup.mdb" Dim dataadapter As New OleDb.OleDbDataAdapter(sqlStr, connstr) Public Shared Sub Initialize(ByVal Owner As IWin32Window, ByRef oForm2Data As Form2Data) Dim result As DialogResult Dim frm2 As New Form2 With frm2 'Using TheData Populate each Control on Form2 End With Result = frm2.ShowDialog(Owner) If Result = System.Windows.Forms.DialogResult.OK Then With frm2 'Now update the values in TheData with its value in its Form2 Control End With 'Save Changes oForm2Data.SaveData() End If End Sub Public Class Form2Data Public HasData As Boolean Public TheData As DataRow Public Sub New(ByVal Value As String) Dim dsData As DataSet Dim stSQL As String stSQL = "Select * from testbackup Where barcodeid = '" & Value & "'" 'Database code to return dataset to dsData Select Case dsData.Tables(0).Rows.Count Case 1 'One Row Returned HasData = True TheData = dsData.Tables(0).Rows(0) Case 0 'No Rows Found HasData = False TheData = Nothing Case Else 'More then one row returned 'Decided how to handle HasData = True TheData = dsData.Tables(0

      M Offline
      M Offline
      mr_lasseter
      wrote on last edited by
      #2

      Where is the exception being thrown? Mike Lasseter

      S 1 Reply Last reply
      0
      • M mr_lasseter

        Where is the exception being thrown? Mike Lasseter

        S Offline
        S Offline
        str50
        wrote on last edited by
        #3

        at this statement Select Case dsData.Tables(0).Rows.Count

        M 1 Reply Last reply
        0
        • S str50

          at this statement Select Case dsData.Tables(0).Rows.Count

          M Offline
          M Offline
          mr_lasseter
          wrote on last edited by
          #4

          I don't see where dsData is getting Initialized. Is your database code working?

          Mike Lasseter

          S 1 Reply Last reply
          0
          • M mr_lasseter

            I don't see where dsData is getting Initialized. Is your database code working?

            Mike Lasseter

            S Offline
            S Offline
            str50
            wrote on last edited by
            #5

            which database code. I am not sure which one. I wanted to post my program here, but I don't know how to do that. Can you tell me where to initialize my dsdata

            M 1 Reply Last reply
            0
            • S str50

              which database code. I am not sure which one. I wanted to post my program here, but I don't know how to do that. Can you tell me where to initialize my dsdata

              M Offline
              M Offline
              mr_lasseter
              wrote on last edited by
              #6

              Dim dsData As DataSet '--THIS VARIABLE IS NOT GETTING INITIALIZED/SET Dim stSQL As String stSQL = "Select * from testbackup Where barcodeid = '" & Value & "'" 'THIS IS DB CODE I AM TALKING ABOUT 'Database code to return dataset to dsData Without seeing the code I can't say exactly where your problem is, but here are some things to check. 1. Is the dsData nothing when you step through your code? 2. If not does it contain any tables? My guess is the dsData is not getting populated correctly in the Database Code

              Mike Lasseter

              S 1 Reply Last reply
              0
              • M mr_lasseter

                Dim dsData As DataSet '--THIS VARIABLE IS NOT GETTING INITIALIZED/SET Dim stSQL As String stSQL = "Select * from testbackup Where barcodeid = '" & Value & "'" 'THIS IS DB CODE I AM TALKING ABOUT 'Database code to return dataset to dsData Without seeing the code I can't say exactly where your problem is, but here are some things to check. 1. Is the dsData nothing when you step through your code? 2. If not does it contain any tables? My guess is the dsData is not getting populated correctly in the Database Code

                Mike Lasseter

                S Offline
                S Offline
                str50
                wrote on last edited by
                #7

                can i e-mail you my program, so you can see what is happening. let me know. thanks for your help so far

                M 1 Reply Last reply
                0
                • S str50

                  can i e-mail you my program, so you can see what is happening. let me know. thanks for your help so far

                  M Offline
                  M Offline
                  mr_lasseter
                  wrote on last edited by
                  #8

                  Just post the code for 'Database code to return dataset to dsData in the forum. It shouldn't be that long.

                  Mike Lasseter

                  S 1 Reply Last reply
                  0
                  • M mr_lasseter

                    Just post the code for 'Database code to return dataset to dsData in the forum. It shouldn't be that long.

                    Mike Lasseter

                    S Offline
                    S Offline
                    str50
                    wrote on last edited by
                    #9

                    I am not sure what to put in that area. Would you be able to help me with that if you don't mind. thanks

                    M 1 Reply Last reply
                    0
                    • S str50

                      I am not sure what to put in that area. Would you be able to help me with that if you don't mind. thanks

                      M Offline
                      M Offline
                      mr_lasseter
                      wrote on last edited by
                      #10

                      That is what is giving you your problem. Try looking at the following link. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcreatingusingdatasets.asp[^]

                      Mike Lasseter

                      S 1 Reply Last reply
                      0
                      • M mr_lasseter

                        That is what is giving you your problem. Try looking at the following link. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcreatingusingdatasets.asp[^]

                        Mike Lasseter

                        S Offline
                        S Offline
                        str50
                        wrote on last edited by
                        #11

                        Alright I looked at the website and this is what I got out of it. This is what I have so far, but when I run the program now I don't get anything. It does not go to form2. It just stays at form1 even when I hit the search button. I must have done something wrong and I can't figure out. I am trying my best to figure out. Any help will be appreciated. Thanks again Public Sub New(ByVal Value As String) Dim dsData As DataSet = New DataSet("BackupInfo") Dim stSQL As String Dim backupTable As DataTable = dsData.Tables.Add("testbackup") stSQL = "Select * from testbackup Where barcodeid = '" & Value & "'" 'Database code to return dataset to dsData backupTable.Columns.Add("Barcodeid", Type.GetType("System.Int32")) backupTable.Columns.Add("location", Type.GetType("System.Int32")) backupTable.Columns.Add("datein", Type.GetType("System.String")) backupTable.Columns.Add("dateout", Type.GetType("System.String")) backupTable.Columns.Add("timein", Type.GetType("System.String")) backupTable.Columns.Add("timeout", Type.GetType("System.String")) backupTable.Columns.Add("comment", Type.GetType("System.String")) Select Case dsData.Tables(0).Rows.Count Case 1 'One Row Returned HasData = True TheData = dsData.Tables(0).Rows(0) Case 0 'No Rows Found HasData = False TheData = Nothing Case Else 'More then one row returned 'Decided how to handle HasData = True TheData = dsData.Tables(0).Rows(0) End Select End Sub

                        -- modified at 16:57 Saturday 22nd July, 2006

                        M 1 Reply Last reply
                        0
                        • S str50

                          Alright I looked at the website and this is what I got out of it. This is what I have so far, but when I run the program now I don't get anything. It does not go to form2. It just stays at form1 even when I hit the search button. I must have done something wrong and I can't figure out. I am trying my best to figure out. Any help will be appreciated. Thanks again Public Sub New(ByVal Value As String) Dim dsData As DataSet = New DataSet("BackupInfo") Dim stSQL As String Dim backupTable As DataTable = dsData.Tables.Add("testbackup") stSQL = "Select * from testbackup Where barcodeid = '" & Value & "'" 'Database code to return dataset to dsData backupTable.Columns.Add("Barcodeid", Type.GetType("System.Int32")) backupTable.Columns.Add("location", Type.GetType("System.Int32")) backupTable.Columns.Add("datein", Type.GetType("System.String")) backupTable.Columns.Add("dateout", Type.GetType("System.String")) backupTable.Columns.Add("timein", Type.GetType("System.String")) backupTable.Columns.Add("timeout", Type.GetType("System.String")) backupTable.Columns.Add("comment", Type.GetType("System.String")) Select Case dsData.Tables(0).Rows.Count Case 1 'One Row Returned HasData = True TheData = dsData.Tables(0).Rows(0) Case 0 'No Rows Found HasData = False TheData = Nothing Case Else 'More then one row returned 'Decided how to handle HasData = True TheData = dsData.Tables(0).Rows(0) End Select End Sub

                          -- modified at 16:57 Saturday 22nd July, 2006

                          M Offline
                          M Offline
                          mr_lasseter
                          wrote on last edited by
                          #12

                          You need to fill the dataset with the data from the database. Look at the link below. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconpopulatingdatasetfromdataadapter.asp[^]

                          Mike Lasseter

                          S 1 Reply Last reply
                          0
                          • M mr_lasseter

                            You need to fill the dataset with the data from the database. Look at the link below. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconpopulatingdatasetfromdataadapter.asp[^]

                            Mike Lasseter

                            S Offline
                            S Offline
                            str50
                            wrote on last edited by
                            #13

                            Alright. I finally made progress with your help. thanks a lot. But now when I run the program I get the message that I put in "not valid". It is not picking up the record. This is the code I have so far. Any help will be appreciated. Thanks Public Sub New(ByVal Value As String) Dim dsData As DataSet = New DataSet1 Dim stSQL As String Dim backupTable As DataTable = dsData.Tables.Add("testbackup") stSQL = "Select * from testbackup Where barcodeid = '" & Value & "'" 'Database code to return dataset to dsData Dim orderConn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=backup.mdb;") Dim orderDA As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM testbackup", orderConn) orderConn.Open() Dim custDS As DataSet = New DataSet orderDA.Fill(custDS, "testbackup") orderConn.Close() backupTable.Columns.Add("Barcodeid", Type.GetType("System.Int32")) backupTable.Columns.Add("location", Type.GetType("System.String")) backupTable.Columns.Add("datein", Type.GetType("System.String")) backupTable.Columns.Add("dateout", Type.GetType("System.String")) backupTable.Columns.Add("timein", Type.GetType("System.String")) backupTable.Columns.Add("timeout", Type.GetType("System.String")) backupTable.Columns.Add("comment", Type.GetType("System.String")) Select Case dsData.Tables(0).Rows.Count Case 1 'One Row Returned HasData = True TheData = dsData.Tables(0).Rows(0) Case 0 'No Rows Found HasData = False TheData = Nothing Case Else 'More then one row returned 'Decided how to handle HasData = True TheData = dsData.Tables(0).Rows(0) End Select End Sub This is the code where it says not value. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Dim oForm2Data As New Form2.Form2Data(Me.txtbarcodeid.Text) If oForm2Data.HasData Then Form2.Initialize(Me, oForm2Data) Else

                            1 Reply Last reply
                            0
                            • S str50

                              I am getting this error when I run the program. I am not sure how to fix this error, if anybody can help me I will appreciate it. The program has two forms. Form1 has the search textbox and form2 takes the information from form1 and populates form2. I have attached the code for both forms, so you can see what I am talking about. Any help will be appreciated. Thanks Form1 Code: Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Dim oForm2Data As New Form2.Form2Data(Me.txtbarcodeid.Text) If oForm2Data.HasData Then Form2.Initialize(Me, oForm2Data) Else ' The Data does not exists for the item in the textbox End If End Sub Form2 Code: Dim dt As New DataTable Dim sqlStr As String = "SELECT * FROM testbackup" Dim sqlstr1 As String = "SELECT * FROM history" Dim connstr As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & _ "Data Source = backup.mdb" Dim dataadapter As New OleDb.OleDbDataAdapter(sqlStr, connstr) Public Shared Sub Initialize(ByVal Owner As IWin32Window, ByRef oForm2Data As Form2Data) Dim result As DialogResult Dim frm2 As New Form2 With frm2 'Using TheData Populate each Control on Form2 End With Result = frm2.ShowDialog(Owner) If Result = System.Windows.Forms.DialogResult.OK Then With frm2 'Now update the values in TheData with its value in its Form2 Control End With 'Save Changes oForm2Data.SaveData() End If End Sub Public Class Form2Data Public HasData As Boolean Public TheData As DataRow Public Sub New(ByVal Value As String) Dim dsData As DataSet Dim stSQL As String stSQL = "Select * from testbackup Where barcodeid = '" & Value & "'" 'Database code to return dataset to dsData Select Case dsData.Tables(0).Rows.Count Case 1 'One Row Returned HasData = True TheData = dsData.Tables(0).Rows(0) Case 0 'No Rows Found HasData = False TheData = Nothing Case Else 'More then one row returned 'Decided how to handle HasData = True TheData = dsData.Tables(0

                              X Offline
                              X Offline
                              xivovivovix
                              wrote on last edited by
                              #14

                              i have experienced like this one too. the problem was the data type. How come??? let see your code below : stSQL = "Select * from testbackup Where barcodeid = '" & Value & "'" if we see at barcodeid = '" & Value & "'" , we can say that barcodeid is string data type. But it's possible in your datasource, the barcodeid is integer.. so the solution is remove ' charachters, and it will be like this: stSQL = "Select * from testbackup Where barcodeid = " & Value -- modified at 7:43 Friday 4th August, 2006

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups