Null Reference Exception

Forum for users of SQL Toolkit 3,4,and 5

Null Reference Exception

Postby bparekh » Wed Dec 05, 2007 9:33 pm

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())
Console.WriteLine("{0}:{1} == {2}", field.Field(False).Name, value1.ToString(), value2.ToString())
End If
'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
End If

'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


End Sub
Posts: 1
Joined: Wed Dec 05, 2007 9:26 pm

Postby Brian Donahue » Thu Dec 06, 2007 9:36 am

Thanks for your post. I can't say what the problem is, but here is a tip: try setting some breakpoints and using the immediate window to figure out which object is NULL. For instance, mapping.obj1 could be null if the object does not exist in database 1. Or there could be some wierdness causing table.FullyQualifiedName to be null. Either of these could have the effect of returning a NULL resultsstore object.

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.
Brian Donahue
Posts: 6590
Joined: Mon Aug 23, 2004 9:48 am

Return to SQL Toolkit Previous Versions

Who is online

Users browsing this forum: No registered users and 0 guests