I am importing .csv files into datatables
I want to display Message whenever there is empty values detected in the cell and also delete them and then continue the program
private static DataTable GetDataTabletFromCSVFile1(string csv_file_path1)
{
DataTable table2 = new DataTable("Real");
try
{
using (TextFieldParser csvReader1 = new TextFieldParser(csv_file_path1))
{
csvReader1.SetDelimiters(new string[] { "," });
csvReader1.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader1.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn2 = new DataColumn(column);
datecolumn2.AllowDBNull = true;
table2.Columns.Add(datecolumn2);
}
while (!csvReader1.EndOfData)
{
string[] fieldData1 = csvReader1.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData1.Length; i++)
{
if (fieldData1[i] == "")
{
fieldData1[i] = null;
}
}
table2.Rows.Add(fieldData1);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Select New File");
}
return table2;
}


VulpesRank #55 · 29.2K reputationPosted May 8, 2014, 5:48 AM EDT
private static DataTable GetDataTabletFromCSVFile1(string csv_file_path1)
{
DataTable table2 = new DataTable("Real");
try
{
using (TextFieldParser csvReader1 = new TextFieldParser(csv_file_path1))
{
csvReader1.SetDelimiters(new string[] { "," });
csvReader1.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader1.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn2 = new DataColumn(column);
datecolumn2.AllowDBNull = true;
table2.Columns.Add(datecolumn2);
}
while (!csvReader1.EndOfData)
{
string[] fieldData1 = csvReader1.ReadFields();
DataRow dr = table2.Rows.Add(fieldData1);
for (int i = 0; i < fieldData1.Length; i++)
{
if (String.IsNullOrEmpty(fieldData1[i])) dr[i] = DBNull.Value;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Select New File");
return null;
}
return table2;
}
VulpesRank #55 · 29.2K reputationPosted May 8, 2014, 2:36 AM EDT
Farhan ShariffRank #2,004 · 725 reputationPosted May 8, 2014, 3:47 AM EDT
VulpesRank #55 · 29.2K reputationPosted May 8, 2014, 4:52 AM EDT
Farhan ShariffRank #2,004 · 725 reputationPosted May 8, 2014, 5:13 AM EDT
Farhan ShariffRank #2,004 · 725 reputationPosted May 8, 2014, 10:10 AM EDT
now rejig
1.How does row count work
a. does it give the highest row value (when different column have different number of rows)
b.if different column have different number of rows does it affect the row count
2.can I count the number of rows, column after column
VulpesRank #55 · 29.2K reputationPosted May 8, 2014, 12:34 PM EDT
This is why I said earlier that you can't delete cells in isolation - only whole rows.
Farhan ShariffRank #2,004 · 725 reputationPosted May 12, 2014, 3:47 AM EDT
if there a better way to do tthis.
VulpesRank #55 · 29.2K reputationPosted May 12, 2014, 5:02 AM EDT
If it's the latter, to avoid popping up the MessageBox on each occasion, I'd save something to identify the null values - perhaps a tuple of the row and column numbers - to a list and then display the contents of the list in one go.
Farhan ShariffRank #2,004 · 725 reputationPosted May 12, 2014, 5:05 AM EDT
VulpesRank #55 · 29.2K reputationPosted May 12, 2014, 5:42 AM EDT
var nullColumns = new List
foreach (DataRow row in table2.Rows)
{
foreach (DataColumn col in table2.Columns)
{
//test for null here
if (row[col] == DBNull.Value)
{
if (!nullColumns.Contains(col.ColumnName))
{
nullColumns.Add(col.ColumnName);
}
}
}
}
File.WriteAllLines("nullcolumns.txt", nullColumns);
Farhan ShariffRank #2,004 · 725 reputationPosted May 12, 2014, 6:11 AM EDT
var nullColumns = new List
foreach (DataRow row in table2.Rows)
{
foreach (DataColumn col in table2.Columns)
{
//test for null here
if (row[col] == DBNull.Value)
{
if (!nullColumns.Contains(col.ColumnName))
{
nullColumns.Add(col.ColumnName);
MessageBox.Show("File has parameter with Empty values");
//MessageBox Pops multiple times and cannot use break or return null which stops only at one column
}
}
}
}
File.WriteAllLines(@"C:\Delta Sigma\Log.txt", nullColumns);
VulpesRank #55 · 29.2K reputationPosted May 12, 2014, 6:24 AM EDT
In fact, it's also dawned on me that you wouldn't want to create a file anyway if there are no null values:
var nullColumns = new List
foreach (DataRow row in table2.Rows)
{
foreach (DataColumn col in table2.Columns)
{
//test for null here
if (row[col] == DBNull.Value)
{
if (!nullColumns.Contains(col.ColumnName)
{
nullColumns.Add(col.ColumnName);
}
}
}
}
if (nullColumns.Count > 0)
{
MessageBox.Show("File has parameter with Empty values");
File.WriteAllLines(@"C:\Delta Sigma\Log.txt", nullColumns);
}
else
{
MessageBox.Show("File does not have parameter with Empty values");
}
Farhan ShariffRank #2,004 · 725 reputationPosted May 12, 2014, 12:29 PM EDT
I want date in the first line
File.WriteAllLines(@"C:\Delta Sigma\Log.txt", nullColumns);
VulpesRank #55 · 29.2K reputationPosted May 12, 2014, 1:23 PM EDT
sudipta sanyalRank #1,945 · 753 reputationPosted May 12, 2014, 9:43 PM EDT
DataTable dataTable = result.Tables[0].Rows.Cast
Farhan ShariffRank #2,004 · 725 reputationPosted May 13, 2014, 12:48 AM EDT