Farhan Shariff

Farhan Shariff

  • NA
  • 933
  • 241.6k

Delete empty cells in CSV files

May 8 2014 5:53 AM
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;
}
 

Answers (17)