[2008] n Tier, Classes and Data Access
I am working on my first n tier project and having a lot of trouble getting my head around wirining it all up. I have a customer class
VB Code:
Public Class Customer
Inherits BOBase
Private Const CN_CustomerCode As String = "CustomerCode"
Private Const CN_CompanyName As String = "CompanyName"
Private _CustomerCode As String
Public Property CustomerCode() As String
Get
Return _CustomerCode
End Get
Private Set(ByVal value As String)
_CustomerCode = value
End Set
End Property
Private _CompanyName As String
Public Property CompanyName() As String
Get
Return _CompanyName
End Get
Set(ByVal value As String)
If _CompanyName <> value Then
Dim propertyName As String = "CompanyName"
Me.DataStateChanged(EntityStateEnum.Modified)
_CompanyName = value
End If
End Set
End Property
Private Sub New()
End Sub
Public Shared Function Create(ByVal custCode As String) As Customer
Dim cust As Customer
Dim dt As DataTable
dt = DAC.ExecuteDataTable("GetCustomerByCode_SP", _
DAC.parameter(CN_CustomerCode, custCode))
cust = New Customer()
With dt.Rows(0)
cust.CustomerCode = .Item(CN_CustomerCode).ToString
cust.CompanyName = .Item(CN_CompanyName).ToString
End With
cust.DataStateChanged(EntityStateEnum.Unchanged)
Return cust
End Function
End Class
Just to test it out I put this is the Form Load Event
Dim myCode As String = "ABC"
Me.CustomerBindingSource1.DataSource = Customer.Create(myCode)
Nothing comes up. I don't get an error or anything. The form just loads with out anything in the fields (I have bound them all). When I set a Break Point on the Me.CustomerBindingSource it doesn't stop there either. Is there something that I am missing?
Re: [2008] n Tier, Classes and Data Access
Re: [2008] n Tier, Classes and Data Access
Your code looks alright, how have you bound the form fields? Have you set DataMember properties on the bound fields on the form?
Re: [2008] n Tier, Classes and Data Access
I have bound it like this.
http://www.liberty1st.org/images/bindings.JPG
Where do I set the Datamember properties?
Re: [2008] n Tier, Classes and Data Access
I found this here but there were no options to set.
http://www.liberty1st.org/images/binding.jpg
The CustomerBinding has different names in these two screen shots but they are the same in the project.
Re: [2008] n Tier, Classes and Data Access
I think I got it now. I had two issues. For some reason the form load event was not firing off when then form was loaded. Once I took care of that I started getting errors, which enabled me to track down the problem.
Next I removed the VS Designer Code for the control binding and did my own with this.
Dim aCust As Customer
aCust = Customer.Create("ABC")
txtCompany.Text = aCust.CompanyName
txtContact.Text = aCust.CustomerCode
Re: [2008] n Tier, Classes and Data Access
Ok the loading of the controls is working now. So the next thing I want to do is save any changes back to the database. Since I am not using the CustomerBindingSource I need to do it in code but I am unsure of the syntax.
Re: [2008] n Tier, Classes and Data Access
This is what I have so far.
VB Code:
Public Function ProcessSave() _
As Boolean Implements MDIInterface.ProcessSave
Dim success As Boolean
Dim aCust As Customer = Nothing
Try
aCust.ContactName = txtContact.Text
aCust.Notes = txtNotes.Text
aCust.Save()
Return success
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace)
End Try
End Function
I get this error Object reference not set to an instance of an object.
at PTWin.frmCustomer.ProcessSave() When I try to do a save.
Re: [2008] n Tier, Classes and Data Access