Question Need help understanding constructors & data access

capella07

Member
Joined
Nov 1, 2006
Messages
18
Location
At work
Programming Experience
1-3
Hello, all
I'm pretty new to VB programming (or any kind of programming for that matter!) - only about a year. I'm working on a project for WinCE.

I'm trying to use the following block of code to save to an existing record in a database on the device:
VB.NET:
Dim i As Integer
m_cWOs = m_objDB.GetWorkOrders()

For i = 1 To m_cWOs.Count

	'Dim myreader As SqlServerCe.SqlCeDataReader
	'Dim existWO As New WorkOrder(myreader, m_objDB)

	Dim existWO As WorkOrder = m_cWOs.Item(i)
	Select Case existWO.ID
		Case iWOID
			With existWO
				.CompleteDate = Today
				.CompletePercent = 100
				.CompletedAcres = objField.Acres
				.Save()
			End With
	End Select
Next
"m_cWOs" is a collection of WorkOrders, and "m_objDB" is the Local Database connection.

My intention is to run through the loop and when iWOID matches the ID field of existWO, I want to update the three fields in the Save() function in the WorkOrder.vb class.

Here is the beginning of that function:
VB.NET:
Public Function Save() As Boolean
	Dim objAdapter As SqlCeDataAdapter
	Dim c As SqlCeCommand
	Dim bInserting As Boolean
	Dim objDS As DataSet = New DataSet
	Dim objRow As DataRow
	Dim i As Integer

	Save = False

	Try
		objAdapter = New SqlCeDataAdapter
		c = m_objDB.Connection.CreateCommand()

I'm getting a "NullReferenceException" error on the last line (c = m_objDB.Connection...) with the Troubleshooting Tip saying "Use the "New" keyword to create an object instance." Since m_objDB is the only object in that line I checked & at that point in execution it is, indeed "Nothing". So I thought I need to use one of the constructors in the WorkOrder class.

There are a few constructors already in the WorkOrder.vb class. Two of them appear to me to be what I may need if I'm right in assuming the problem is the m_objDB not being instantiated. The problem is, both of the constructors are created with the SqlCeDataReader as such:
VB.NET:
	Public Sub New(ByRef myReader As SqlServerCe.SqlCeDataReader)
...
and
VB.NET:
Public Sub New(ByRef myReader As SqlServerCe.SqlCeDataReader, ByRef objDB As LocalDatabase)
...

I'm assuming I need to use one of those, as they will give me an instance of WorkOrder with the data I need.

As you can see in the first block of code, I have commented what I did to try to use one of those constructors, but I'm just not sure how to go about this.

I'm not sure if this is enough information (or maybe it's too much information!) for someone to help me, but I'd really appreciate it if someone could give me a hand here!

Thanks in advance...
 
Back
Top