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.
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