all groups > dotnet datatools > july 2004 >
You're in the

dotnet datatools

group:

How to merge a Dataset while ignoring null column values


How to merge a Dataset while ignoring null column values yachea2002 NO[at]SPAM hotmail.com
7/30/2004 2:13:21 PM
dotnet datatools:
Hi, I am trying to merge a Dataset with author one that contains some
null column values.

Ex:

Table A (Main Table)

Id: C2: C3
01 null 30
02 null 30

Table B (Source Table)

Id: C2: C3
01 50 null
02 50 null


When I try A.merge(B) this is what I get:

Table A (Main Table)

Id: C2: C3
01 50 null
02 50 null

Could any one tell me how to do to get this result? :

Table A (In the main Table)

Id: C2: C3
01 50 30
02 50 30

Re: How to merge a Dataset while ignoring null column values Jay B. Harlow [MVP - Outlook]
7/31/2004 5:37:46 PM
You will need to manually do any custom merge such as your example.

What should the follow result be?

[quoted text, click to view]

Assuming that Id is the primary key of both tables, I would use a loop
similar to (untested):

Dim mainTable As New DataTable("Main Table")
mainTable.Columns.Add("Id", GetType(Integer))
mainTable.Columns.Add("C2", GetType(Integer))
mainTable.Columns.Add("C3", GetType(Integer))
mainTable.PrimaryKey = New DataColumn() {mainTable.Columns("Id")}
mainTable.Rows.Add(New Object() {1, DBNull.Value, 30})
mainTable.Rows.Add(New Object() {2, DBNull.Value, 30})

Dim sourceTable As New DataTable("Source Table")
sourceTable.Columns.Add("Id", GetType(Integer))
sourceTable.Columns.Add("C2", GetType(Integer))
sourceTable.Columns.Add("C3", GetType(Integer))
sourceTable.PrimaryKey = New DataColumn()
{sourceTable.Columns("Id")}
sourceTable.Rows.Add(New Object() {1, 50, DBNull.Value})
sourceTable.Rows.Add(New Object() {2, 50, DBNull.Value})

For Each mainRow As DataRow In mainTable.Rows
Dim sourceRow As DataRow = sourceTable.Rows.Find(mainRow!Id)
For Each mainColumn As DataColumn In mainTable.Columns
Dim sourceColumn As DataColumn =
sourceTable.Columns(mainColumn.ColumnName)
If mainRow.IsNull(mainColumn) Then
mainRow(mainColumn) = sourceRow(sourceColumn)
' Next two lines are optional if you want the source
table updated
'ElseIf sourceRow.IsNull(sourceColumn) Then
' sourceRow(sourceColumn) = mainRow(mainColumn)
End If
Next
Next

Hope this helps
Jay

[quoted text, click to view]

AddThis Social Bookmark Button