original and proposed are different in datarow with date when same date!

S

Smokey Grindel

Try this code, why are the sames that are the same showing up as different?
when I run this it says they are different values!, but do it with strings
and they say they are the same... I am trying to write code to check if a
date changed in a data row and it is getting no where
Module Module1

Sub Main()

CheckVersionBeforeAccept()

Console.Read()

End Sub

Private Sub CheckVersionBeforeAccept()

'Run a function to create a DataTable with one column.

Dim dataTable As DataTable = MakeTable()

Dim dataRow As DataRow = dataTable.NewRow()

dataRow("FirstName") = Now.Date.Date

dataTable.Rows.Add(dataRow)

dataRow.BeginEdit()

' Edit data but keep the same value.

dataRow(0) = Now.Date.Date

' Uncomment the following line to add a new value.

dataRow(0) = Now.Date.Date

' dataRow(0) = Now.Date.Date

Console.WriteLine(String.Format("FirstName {0}", dataRow(0)))

' Compare the proposed version with the current.

If dataRow.HasVersion(DataRowVersion.Proposed) Then

If dataRow(0, DataRowVersion.Current) Is dataRow(0, DataRowVersion.Proposed)
Then

Console.WriteLine("The original and the proposed are the same.")

dataRow.CancelEdit()

Else

dataRow.AcceptChanges()

Console.WriteLine("The original and the proposed are different.")

End If

End If

End Sub

Private Function MakeTable() As DataTable

' Make a simple table with one column.

Dim dt As New DataTable("dataTable")

Dim firstName As New DataColumn("FirstName", _

Type.GetType("System.DateTime"))

dt.Columns.Add(firstName)

Return dt

End Function



End Module
 
S

Smokey Grindel

just hit me one is a reference type and one is a value type... so I made a
quick compare function that if its a reference type and it failes the IS
check then checks it using the equals method of the object... anything
better then doing this?
 
G

Gregg Walker

Smokey,
anything better then doing this?

Try casting the current value to IComparable and then using the CompareTo
method to compare to the proposed value.

Something like...

dim compareValue as IComparable

compareValue = dataRow(0, DataRowVersion.Current) as IComparable

if not compareValue is nothing then

Try

if compareValue.CompareTo(dataRow(0, DataRowVersion.Proposed))
== 0) then

Console.WriteLine("The original and the proposed are the
same.")

dataRow.CancelEdit()

else

dataRow.AcceptChanges()

Console.WriteLine("The original and the proposed are
different.")

end if

Catch ex As Exception

if ex is ArgumentException then 'CompareTo will throw
ArgumentException when the 2 values are not of the same type...i.e. DBNull
and int

dataRow.AcceptChanges()

Console.WriteLine("The original and the proposed are
different.")

else

'-- Handle Other Exception Type or Let it bubble up...

end if

End Try

end if

....

Sorry I'm not proficient with VB so the snippet may not be syntactically
correct.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top