Good I'm doing extracting the data from a csv file to my application. basically the file will be in a directory in a specific route and the information that has to be shown in a gridview I'm doing with the language c # asp.net
This is my code:
protected void Button1_Click(object sender, EventArgs e)
{
String name = "yeremy";
string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\yermey\yeremy.csv;Extended Properties=""Excel 8.0;HDR=Yes"";";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
GridView1.DataSource = data;
}
But there is an error that comes to me that indicates that the route I do not specify does not exist.
but if it is on the indicated route.
/ ********************************************* **************************** /
It also makes this code.
protected void Page_Load(object sender, EventArgs e)
{
PopulateGrid();
}
public class DataLoader
{
public static DataTable GetDataTableFromCSV(string strFileName)
{
try
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");
conn.Open();
string strQuery = "Select * from [" + System.IO.Path.GetFileName(strFileName) + "]";
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
da.Fill(ds);
return ds.Tables[0];
}
catch (Exception ex) { }
return new DataTable();
}
}
private void PopulateGrid()
{
DataTable table = DataLoader.GetDataTableFromCSV(@"E:\yermey\yeremy.cvs");
if (table.Columns.Count == 0)
{ }
else
GridView1.DataSource = table.DefaultView;
}