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;
}

VulpesPosted May 8, 2014, 9:48 AM
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;
}
Farhan ShariffPosted May 13, 2014, 4:48 AM
sudipta sanyalPosted May 13, 2014, 1:43 AM
DataTable dataTable = result.Tables[0].Rows.Cast
VulpesPosted May 12, 2014, 5:23 PM
Farhan ShariffPosted May 12, 2014, 4:29 PM
I want date in the first line
File.WriteAllLines(@"C:\Delta Sigma\Log.txt", nullColumns);
VulpesPosted May 12, 2014, 10:24 AM
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 ShariffPosted May 12, 2014, 10:11 AM
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);
VulpesPosted May 12, 2014, 9:42 AM
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 ShariffPosted May 12, 2014, 9:05 AM
VulpesPosted May 12, 2014, 9:02 AM
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 ShariffPosted May 12, 2014, 7:47 AM
if there a better way to do tthis.
VulpesPosted May 8, 2014, 4:34 PM
This is why I said earlier that you can't delete cells in isolation - only whole rows.
Farhan ShariffPosted May 8, 2014, 2:10 PM
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
Farhan ShariffPosted May 8, 2014, 9:13 AM
VulpesPosted May 8, 2014, 8:52 AM
Farhan ShariffPosted May 8, 2014, 7:47 AM
VulpesPosted May 8, 2014, 6:36 AM