Loading & saving data from a class - design question

  • Thread starter Thread starter Jeff Bunting
  • Start date Start date
J

Jeff Bunting

I have a class that pulls data from a stored procedure into a datatable and
is using the DataRowModified property to determine whether to update the
data when the class is saved or disposed. So I have this:

Public Property LastName() As String
Get
LastName = ThisRecord("lastname")
End Get
Set(ByVal Value As String)
If Me.LastName <> Value Then ThisRecord("lastname") = Value
End Set
End Property

Public Sub Save()
If ThisRecord.RowState = DataRowState.Modified Then
WS.UpdateContact(DS1)
End Sub

ThisRecord is a DataRow,WS is a web service, DS1 is a DataSet

Works fine with existing data. When I create a new instance of the class
that doesn't have any pre-existing data in it then ThisRecord is undefined.
I was trying to decide the best way to handle this. Can I create a table
and row based on the class with empty values and continue to use the
DataRowState.Modified, or is there a better way? I don't want to do submit
any changes back to the database unless a value has changed and this seemed
to be the way to do it.

Jeff
 
Hi Jeff,

DataTable has a GetChanges() method which will return a DataTable
consisting of all the changed (new, edited or deleted) rows.

Craig VB .NET Team
 
Thanks, good to know. the table only consists of one row though (I'm pulling
a single record). I was just trying to decide if this was a good way to do
it.

I figured I could use System.Reflection to get the property names and create
a table with those named columns at runtime for newly created objects. Just
need to set initial values based on the data type. I was going to do
something like this:

Dim ObjectMembers As MemberInfo() = t.GetMembers
Dim Iterator As MemberInfo

For Each Iterator In ObjectMembers
If Iterator.MemberType = MemberTypes.Property Then
ColName = Iterator.Name.ToString
ColType = Iterator.GetType
myNewCol = New DataColumn(ColName, ColType)
DT.Columns.Add(myNewCol)
End If
Next

DS.Tables.Add(DT)

NumRows = DT.Columns.Count

Dim rowVals(NumRows) As Object

myNewRow = DS.Tables("CActivity").NewRow

Dim tt As Type

For X = 0 To NumRows - 1

tt = DT.Columns(X).GetType
If tt Is GetType(Integer) Then
myNewRow.Item(X) = 0
Else
If tt Is GetType(String) Then myNewRow.Item(X) = ""
End If

Next


Jeff
 
realized after I sent the post,

NumRows should be called NumColumns to avoid confusion.

and I left out

DT.Rows.Add(MyNewRow)

at the very end.
 
Back
Top