I have this function which compares two DataTables (table1 and table2 )and makes a a new table3( sorted in ascending order)
1.Here table2 should have only numeric(double) values but whenever there is a non numeric value I want to display the column name(col).
private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
{
DataTable returnValue = null;
try
{
DataTable table3 = new DataTable();
DataRow dr = null;
string filterExp = string.Empty;
for (int i = 0; i < table1.Rows.Count; i++)
{
string col = table1.Rows[i]["Par Name"].ToString(); // column names of table2
if (table2.Columns.Contains(col) )
{
if (!table3.Columns.Contains(col))
{
table3.Columns.Add(col, typeof(double));
filterExp = filterExp + col + " asc ,";
}
for (int j = 0; j < table2.Rows.Count; j++)
{
if (table3.Rows.Count != table2.Rows.Count)
{
dr = table3.NewRow();
table3.Rows.Add(dr);
}
/*Here I want to check if the value begin copied is numeric(double) else I want to display the col (ie column name in table2)whenever there is a non numeric value control is transferred to catch how to pass col to catch */
table3.Rows[j][col] = table2.Rows[j][col];
}
}
}
returnValue = resultDt;
}
catch
{
MessageBox.Show("Critical Data error caused by "+ "\n" 1.String Value Present in the Parametric Data", "- Exit Application ", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.show("Column with non numeric data :" +_______ );
Environment.Exit(0);
}
return returnValue;
}
Loading

VulpesPosted Aug 5, 2014, 7:45 PM
Farhan ShariffPosted Aug 6, 2014, 9:28 AM
VulpesPosted Aug 6, 2014, 9:09 AM
The nested try statement is wholly within the for loop. So when a mis-match exception occurs and is caught and handled by the catch block, control simply returns to the next iteration of the for loop.
The list and the 'col' variable are available throughout the for statement because we've declared them outside the outer try statement which includes the for statement and hence the inner catch block.
Farhan ShariffPosted Aug 6, 2014, 9:03 AM
table3.Rows[j][col] = table2.Rows[j][col];
control is transfered to
catch
{
notDoubles.Add(col);
} copying col to List
then control shifts back to
for (int i = 0; i < table1.Rows.Count; i++) and loop is continued as col is declared outside this loop and also outside try statement
VulpesPosted Aug 6, 2014, 5:25 AM
If you declare a variable in the try block then it's scoped to that block and so isn't available in the catch and finally blocks. This surprises a lot of people but it's the way it works in C#.
To ensure that they're available everywhere what I did here was to declare both the list and 'col' completely outside the nested try statements.
I also put the code which checks whether there were any mismatches and prints them out in the finally block of the outer try block.
This means that, even if there's some exception other than a type mismatch, the user will still see the mismatches which have been identified to date before the application exits. That's because you're guaranteed that the finally block will always run whether there's an exception or not.
Farhan ShariffPosted Aug 6, 2014, 4:52 AM
When col is declared as null and used how is the control transfered back
Farhan ShariffPosted Aug 5, 2014, 4:53 PM
In my scenario is it very strict that no numeric or null values are to be present as the input files are machine generated and this non numeric value may very rarely arise only due to user interference, I have included the code only to check user error.
VulpesPosted Aug 5, 2014, 12:30 PM
If you wanted to report all of them, you could add them to a list rather than calling Environment.Exit in the inner catch block. After all the code has run, you could then see if there are any names in the list and, if there are, print them out and exit the application at that point.
The code to do this could be put in a finally block for the outer try.
Farhan ShariffPosted Aug 5, 2014, 12:00 PM
try
{
DataTable table3 = new DataTable();
DataRow dr = null;
string filterExp = string.Empty;
for (int i = 0; i < table1.Rows.Count; i++)
{
string col = table1.Rows[i]["Par Name"].ToString();
if (table2.Columns.Contains(col))
{
try
{
if (!table3.Columns.Contains(col))
{
table3.Columns.Add(col, typeof(double));
filterExp = filterExp + col + " asc ,";
}
for (int j = 0; j < table2.Rows.Count; j++)
{
if (table3.Rows.Count != table2.Rows.Count)
{
dr = table3.NewRow();
table3.Rows.Add(dr);
}
table3.Rows[j][col] = table2.Rows[j][col];
}
}
catch
{
MessageBox.Show("Parameter Has non numeric Values: " +col);
Environment.Exit(0);
}
}
}
returnValue = resultDt;
}
catch
{
MessageBox.Show("Critical Data error caused by either "+ "\n" + .String Value Present in the Parametric Data", "- Exit Application ", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
return returnValue;
}
Farhan ShariffPosted Aug 5, 2014, 11:59 AM
VulpesPosted Aug 5, 2014, 11:50 AM
Farhan ShariffPosted Aug 5, 2014, 11:43 AM
VulpesPosted Aug 5, 2014, 11:41 AM
Farhan ShariffPosted Aug 5, 2014, 11:33 AM
here only those columns names which are under "Par Name" column of table1 have to be considered while copying to table3.
basically all column names(in Par Name) are of double
table1
table2
here Par2 has MARS i want to handle these kind of situations and indicate to user under which column it is happening (which rarely happen)
VulpesPosted Aug 5, 2014, 11:19 AM
private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
{
var notDoubles = new List
foreach(DataColumn dc in table2.Columns)
{
if (dc.ColumnType != typeof(double)) notDoubles.Add(dc.ColumnName);
}
if (notDoubles.Count > 0)
{
string message = "The following columns are not doubles\r\n";
message += String.Join("\r\n", notDoubles.ToArray());
MessageBox.Show(message);
return null;
}
// rest of code
}