Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IErrorInfo.GetDescription failed with E_FAIL(0x80004005).System.Data while data adapter Fill()

I'm trying to get data from CSV file , by using fill() method i got a exception idk why it arise,please review code and suggest optimistic answer.note that if parameter "s" doesn't have any space means it works fine. if it have space means how to overcome that,don't suggest temporary rename and all.

/// <summary>
/// Import Function For CSV Delimeted File
/// </summary>
/// <param name="s">File Name</param>
private DataTable Import4csv(string s)
{
    string file = Path.GetFileName(s);
    string dir = Path.GetDirectoryName(s);
    string sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
                             + "Data Source=\"" + dir + "\\\";"
                             + "Extended Properties=\"text;HDR=YES;FMT=Delimited\"";
    try
    {
        var objConn = new OleDbConnection(sConnection);
        objConn.Open();
        var ds = new DataSet();
        var da = new OleDbDataAdapter("SELECT * FROM " + file, sConnection);
        da.Fill(ds);        // getting exception on this line.
        objConn.Close();
        return ds.Tables[0];
    }
    catch (Exception ex)
    {
        Trace.WriteLine(ex.Message + ex.Source);
        return null;
    }
}
like image 312
Arunkumar Chandrasekaran Avatar asked Sep 20 '25 01:09

Arunkumar Chandrasekaran


2 Answers

var da = new OleDbDataAdapter("SELECT * FROM `" + file + "`", sConnection);

near the tilde key..

like image 182
shobhonk Avatar answered Sep 21 '25 15:09

shobhonk


I have faced the same issue, but was able to overcome it by enclosing the suspect word in square brackets.

In my case, it was a field name which I used in "Where" condition i.e.;

select * from tblDetail where [Section] = 'Operations'
like image 24
Jerrin Joseph Avatar answered Sep 21 '25 15:09

Jerrin Joseph