Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to insert data into datatable from the question?
Visual Studio 2015 | Console Application

I have 3 datatable named dt_A, dt_B, dt_C. I have made queries to SQL for counting table row count and fill SQLDataReader into DataTable. Involved DataTable is dt_A (grab from serverA) and dt_B (grab from serverB). While dt_C will contain a total different count from (dt_A minus dt_B).

Assuming both dt_A and dt_B should have this column:
dt_A
count_tbl1...........390 
count_tbl2.............9
count_tbl3...........500

dt_B
count_tbl1...........385 
count_tbl2.............0
count_tbl3...........480

Assume dt_C will hold differentiation from above data
dt_C
count_tbl1.............5
count_tbl2.............9
count_tbl3............20


dt_A and dt_B automatically fill on queries. dt_C will insert manually.

C#
Dim val1, val2 As Integer
Dim colname As String
For i = 0 To dt_A.Columns.Count - 1
    colname = dt_A.Columns(i).ToString
    dt_C.Columns.Add(colname)
Next
For i = 0 To dt_C.Columns.Count - 1
    Dim nrow As DataRow = dt_C.NewRow
    val1 = dt_A.Rows(0).Item(i)
    val2 = dt_B.Rows(0).Item(i)
    nrow(i) = val1 - val2
    dt_C.Rows.Add(nrow)
Next

The second For Next, is showing wrong display. It should be adding on single row. Instead, while lopping, it will add next value to next column but next row.

I want next value to be on next column and on row(0). No any other row should be made. I have try some other but shows empty

image view here[^]

What I have tried:

Dim val1, val2 As Integer
Dim colname As String
For i = 0 To dt_A.Columns.Count - 1
colname = dt_A.Columns(i).ToString
dt_C.Columns.Add(colname)
Next
For i = 0 To dt_C.Columns.Count - 1
Dim nrow As DataRow = dt_C.NewRow
val1 = dt_A.Rows(0).Item(i)
val2 = dt_B.Rows(0).Item(i)
nrow(i) = val1 - val2
dt_C.Rows.Add(nrow)
Next
Posted
Updated 18-Aug-16 21:26pm
v3

1 solution

Move the creation and adding of the dt_C row outside the second for-loop
C#
Dim nrow As DataRow = dt_diff.NewRow
For i = 0 To dt_diff.Columns.Count - 1
    val1 = dt_local.Rows(0).Item(i)
    val2 = dt_azure.Rows(0).Item(i)
    nrow(i) = val1 - val2
Next
dt_diff.Rows.Add(nrow)


Cheers
 
Share this answer
 
Comments
Luiey Ichigo 19-Aug-16 3:43am    
Yes.. I find it can be done. Lol.. I modified something too on the code.

Dim val1, val2 As Integer
Dim colname As String
For i = 0 To dt_A.Columns.Count - 1
colname = dt_A.Columns(i).ToString
dt_C.Columns.Add(colname)
Next
Dim int(dt_C.Columns.Count - 1) As Integer
For i = 0 To dt_C.Columns.Count - 1
val1 = dt_A.Rows(0).Item(i)
val2 = dt_B.Rows(0).Item(i)
int(i) = val1 - val2
Next
Dim ndr As DataRow = dt_C.NewRow
For i = 0 To int.Length - 1
ndr(dt_A.Columns(i).ToString) = int(i)
Next
dt_C.Rows.Add(ndr)

All shows single line

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900