Reading CSV and Bind to GridView


public DataSet readCsv()
{
try
{
string strPath = @"Path of the file";
string ConnectionString =
string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};
Extended Properties=""text;HDR=Yes;FMT=Delimited"";"
, strPath);
string CommandText = string.Format(@"SELECT * FROM Nameofthefile.csv", ConnectionString);
DataSet CSVDataSet = new DataSet();
OleDbConnection CSVConnection = new OleDbConnection(ConnectionString);
OleDbDataAdapter CSVAdapter = new OleDbDataAdapter(CommandText, CSVConnection);
CSVConnection.Open();
CSVAdapter.Fill(CSVDataSet, "FileName.csv");
CSVConnection.Close();
GVcsv.DataSource = CSVDataSet;
GVcsv.DataBind();
return CSVDataSet;
}
catch (Exception ex)
{
lblerror.Text = ex.Message;
throw ex;
}

}

No comments: