object reference not set to an instance of an object error
-
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
-
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
Where is the exception being thrown? Mike Lasseter
-
Where is the exception being thrown? Mike Lasseter
-
I don't see where dsData is getting Initialized. Is your database code working?
Mike Lasseter
-
I don't see where dsData is getting Initialized. Is your database code working?
Mike Lasseter
-
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
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 CodeMike 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 CodeMike Lasseter
-
can i e-mail you my program, so you can see what is happening. let me know. thanks for your help so far
Just post the code for 'Database code to return dataset to dsData in the forum. It shouldn't be that long.
Mike Lasseter
-
Just post the code for 'Database code to return dataset to dsData in the forum. It shouldn't be that long.
Mike Lasseter
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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