Null Reference Exception
bparekh
Posts: 2
I am using Vb.Net and testing the SQL DataCompare API but it gives null reference exception for the object of time tabledifference. please help . Here is the code. I have bolded the line where i am getting the error:
Dim VW_Dev As New Database
Dim VW_Stage As New Database
VW_Dev.RegisterForDataCompare(New ConnectionProperties("SQL-31LI\Development", "widget_dev", "sqldba", "abc"))
VW_Stage.RegisterForDataCompare(New ConnectionProperties(".", "widget"))
Dim mappings As New SchemaMappings
mappings.CreateMappings(VW_Dev, VW_Stage)
Dim session As New ComparisonSession()
session.CompareDatabases(VW_Dev, VW_Stage, mappings)
Dim mapping As TableMapping
For Each mapping In mappings.TableMappings
Dim table As ViewTableSuperClass = mapping.Obj1
Dim difference As TableDifference = session.TableDifferences(table.FullyQualifiedName)
Dim row As Row
For Each row In difference.ResultsStore 'loop through all the rows
If (row.Type <> row.RowType.Same) Then 'go through the non same records
Dim field As FieldPair
Dim i As Int32 = 0
Console.WriteLine("{0} Row {1} type {2}", table.FullyQualifiedName, row.Index, row.Type.ToString())
For Each field In difference.ResultsStore.Fields
'work out where about in the results the field data is stored
'if we were comparing identical records, or records present in one
'database but not the other then we would not need to
'use the OrdinalInResults1 and OrdinalInResults2 properties
'but just OrdinalInResults
Dim field1 As Int32 = field.OrdinalInResults1
Dim field2 As Int32 = field.OrdinalInResults2
If (field1 <> field2) Then
'get the values
Dim value1 As Object = row.Values(field1)
Dim value2 As Object = row.Values(field2)
If (value1 Is Nothing) Then
value1 = "NULL"
End If
If (value2 Is Nothing) Then
value2 = "NULL"
End If
If row.FieldDifferent(i) Then
Console.WriteLine("{0}:{1} <> {2}", field.Field(False).Name, value1.ToString(), value2.ToString())
Else
Console.WriteLine("{0}:{1} == {2}", field.Field(False).Name, value1.ToString(), value2.ToString())
End If
Else
'this is part of the unique index we are comparing on
Dim value As Object = row.Values(field1)
Console.WriteLine("*{0}:{1}", field.Field(False).Name, value.ToString())
End If
i += 1
Next
End If
Next
Next
'Get the synchronization SQL
Dim provider As New SqlProvider
'we want to run the SQL on Vitaminworld so pass in true as the second parameter
session.Dispose()
VW_Dev.Dispose()
VW_Stage.Dispose()
End Sub
Dim VW_Dev As New Database
Dim VW_Stage As New Database
VW_Dev.RegisterForDataCompare(New ConnectionProperties("SQL-31LI\Development", "widget_dev", "sqldba", "abc"))
VW_Stage.RegisterForDataCompare(New ConnectionProperties(".", "widget"))
Dim mappings As New SchemaMappings
mappings.CreateMappings(VW_Dev, VW_Stage)
Dim session As New ComparisonSession()
session.CompareDatabases(VW_Dev, VW_Stage, mappings)
Dim mapping As TableMapping
For Each mapping In mappings.TableMappings
Dim table As ViewTableSuperClass = mapping.Obj1
Dim difference As TableDifference = session.TableDifferences(table.FullyQualifiedName)
Dim row As Row
For Each row In difference.ResultsStore 'loop through all the rows
If (row.Type <> row.RowType.Same) Then 'go through the non same records
Dim field As FieldPair
Dim i As Int32 = 0
Console.WriteLine("{0} Row {1} type {2}", table.FullyQualifiedName, row.Index, row.Type.ToString())
For Each field In difference.ResultsStore.Fields
'work out where about in the results the field data is stored
'if we were comparing identical records, or records present in one
'database but not the other then we would not need to
'use the OrdinalInResults1 and OrdinalInResults2 properties
'but just OrdinalInResults
Dim field1 As Int32 = field.OrdinalInResults1
Dim field2 As Int32 = field.OrdinalInResults2
If (field1 <> field2) Then
'get the values
Dim value1 As Object = row.Values(field1)
Dim value2 As Object = row.Values(field2)
If (value1 Is Nothing) Then
value1 = "NULL"
End If
If (value2 Is Nothing) Then
value2 = "NULL"
End If
If row.FieldDifferent(i) Then
Console.WriteLine("{0}:{1} <> {2}", field.Field(False).Name, value1.ToString(), value2.ToString())
Else
Console.WriteLine("{0}:{1} == {2}", field.Field(False).Name, value1.ToString(), value2.ToString())
End If
Else
'this is part of the unique index we are comparing on
Dim value As Object = row.Values(field1)
Console.WriteLine("*{0}:{1}", field.Field(False).Name, value.ToString())
End If
i += 1
Next
End If
Next
Next
'Get the synchronization SQL
Dim provider As New SqlProvider
'we want to run the SQL on Vitaminworld so pass in true as the second parameter
session.Dispose()
VW_Dev.Dispose()
VW_Stage.Dispose()
End Sub
Comments
I think it is possible for a mapping to be partial -- that is the mapping may not have a matching table or view in either database, so maybe checking each mapping's .include property first would be a good way to prevent this situation. If the mapping is included, chances are the object does exist in both databases and can be compared.