show gridview from the csv file

0

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

    
asked by PieroDev 18.09.2018 в 21:46
source

1 answer

0

Good to solve I had to make another code that is as follows:

protected void Button1_Click(object sender, EventArgs e)
        {
            String name = "yeremy";
ar filename = @"E:\yermey\yeremy.csv";
            var constr = string.Format(
                @"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=YES;FMT=Delimited""",
                Path.GetDirectoryName(filename)
            );
            var ds = new DataSet("CSV File");
            using (var conn = new OleDbConnection(constr))
            {
                conn.Open();
                var query = "SELECT * FROM [" + Path.GetFileName(filename) + "] ";
                using (var adapter = new OleDbDataAdapter(query, conn))
                {
                    adapter.Fill(ds);
                }
            }
    
answered by 18.09.2018 в 22:33