Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have two DataTable -- 1.table1
2.table2

THESE ARE MY TWO DATATABLES (visual c#)

table1

|Par Name| | Par #|-- |Units |--|LSL|--|USL |--|SKIP |
Diffusion |908513100 |- | 0 | 99.9| |0 |
Program |908514100 |- | 99.5 |999 | |0 |
(ADCQ2_P) |908533100 |- | 1000 | 0 | |0 |




table2

starttime | (ADCQ2_N) | Device | Diffusion | Program | (ADCQ2_P) |
11/7/2013 | SAF5100EL | 163 | -0.145712003 | -0.146583006 | 6.29
11/7/2013 | SAF5100EL | 84 | -0.137499005 | -0.137592003 | 6.21
11/7/2013 | SAF5100EL | 44 | -0.142690003 | -0.143250003 | 6.33
11/7/2013 | SAF5100EL | 164 | -0.139434993 | -0.140459001 | 6.23
11/7/2013 | SAF5100EL | 34 | -0.147183999 | -0.148519993 | 6.11


output should look like
table3

|Diffusion| | Program | (ADCQ2_P)
-0.145712003 - -0.146583006 - 6.29
-0.137499005 - -0.137592003 - 6.21
-0.142690003 - -0.143250003 - 6.33
-0.139434993 - -0.140459001 - 6.23
-0.147183999 - -0.148519993 - 6.11



here the columns in table2 which match the row names of table1(for instance here diffusion and program) have to be fetched from datatable2 and new DataTable3 has to be created and with sorted values of columns (Diffusion and Program)


I want to fetch only those column from table2 into table3 which match with table1 rows


The used the following code to build the tables

This is how I built the Datatable


using System;
using System.Data;
using Microsoft.VisualBasic.FileIO;

namespace ReadDataFromCSVFile
{
static class Program
{
static void Main()
{
string csv_file_path = @"C:\Matlab\Limits.csv";
DataTable table1 = GetDataTabletFromCSVFile(csv_file_path);
Console.WriteLine("Rows count:" + table1.Rows.Count);
Console.ReadLine();

}

private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
{
DataTable table1 = new DataTable("Limits");

using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
table1.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
table1.Rows.Add(fieldData);

}

}

return table1;
}











[edit]Code block added - OriginalGriff[/edit]
[edit]Code block added, again - OriginalGriff[/edit]







table2

starttime   |(ADCQ2_N)     | Device   | Diffusion       | Program | 	
11/7/2013    SAF5100EL       163       -0.145712003      -0.146583006   -0.146583006                              
11/7/2013    SAF5100EL        84       -0.137499005      -0.137592003
11/7/2013    SAF5100EL        44       -0.142690003      -0.143250003  
11/7/2013    SAF5100EL       164       -0.139434993      -0.140459001
11/7/2013    SAF5100EL        34       -0.147183999      -0.148519993



output should look like
table3 Product

 |Diffusion|       | Program |
-0.145712003      -0.146583006
-0.137499005      -0.137592003
-0.1  -0.143250003
-0.139434993      -0.140459001
-0.147183999      -0.148519993




here the columns in table2 which match the row names of table1(for instance here diffusion and program) have to be fetched from datatable2 and new DataTable3 has to be created and with sorted values of columns (Diffusion and Program)


I want to fetch only those column from table2 into table3 which match with table1 rows


The used the following code to build the tables

This is how I built the Datatable


using System;
using System.Data;
using Microsoft.VisualBasic.FileIO;

namespace ReadDataFromCSVFile
{
static class Program
{
static void Main()
{
string csv_file_path = @"C:\Matlab\Limits.csv";
DataTable table1 = GetDataTabletFromCSVFile(csv_file_path);
Console.WriteLine("Rows count:" + table1.Rows.Count);
Console.ReadLine();

}

private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
{
DataTable table1 = new DataTable("Limits");

using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
table1.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
table1.Rows.Add(fieldData);

}

}

return table1;
}











[edit]Code block added - OriginalGriff[/edit]
[edit]Code block added, again - OriginalGriff[/edit]
Posted
Updated 25-Feb-14 3:26am
v11
Comments
OriginalGriff 25-Feb-14 5:07am    
I can't really answer this, because there is no obvious connection between the two tables, and it really isn't clear what should go into the third database as a result.
Perhaps an example of the actual data, some idea of how the two tables are related, and a sample of the output would help?
Use the "Improve question" widget to edit your question and provide better information.
FarhanShariff 25-Feb-14 5:39am    
ok sure .Is the question clear now?
Maciej Los 25-Feb-14 6:47am    
No.
Show us how table1 and table2 are related.
Maciej Los 25-Feb-14 8:27am    
Instead posting code in comment, please, use "Improve question" widget.
_Asif_ 25-Feb-14 8:52am    
What is the matching critera?

1 solution

 
Share this answer
 
Comments
FarhanShariff 25-Feb-14 10:36am    
Thank you for the links "Asif Bhai".
Will try solving using LINQ

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