Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to export my data in .csv file to dataGridView in C#.

After this line: oda.SelectCommand.ExecuteNonQuery();
I have an error:
The Microsoft Access database engine could not find the object 'asasas'. Make sure the object exists and that you spell its name and the path name correctly.


How can i fix that?



There is my code:


C#
public DataSet readCSV(string file)
{
if (!File.Exists(file))
return null;
string pathOnly = Path.GetDirectoryName(file);
DataSet ds = new DataSet();
OleDbConnection conn = null;
//string fileConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathOnly + "\\;Extended Properties=\"Text;HDR=Yes;FORMAT=Delimited\"";
 
string fileConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ pathOnly +";Extended Properties=Excel 8.0";
OleDbConnection ocn = new OleDbConnection(fileConnStr);
try
{
try
{
conn = new OleDbConnection();
conn.ConnectionString = fileConnStr;
conn.Open();
}
catch
{
MessageBox.Show("Veritabanı bağlantı kuramadı.");
return null;
}
string sql = String.Format("SELECT * FROM {0}", Path.GetFileName(file));
OleDbDataAdapter oda = new OleDbDataAdapter(sql, conn);
oda.SelectCommand.ExecuteNonQuery();
oda.Fill(ds, "TableName");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
finally
{
if (conn != null)
conn.Dispose();
}
return ds;
}}}






C#
 private void btnExcelReader_Click(object sender, EventArgs e)
        {
            string dosya;
            //string cevap;
            openFileDialog1.ShowDialog();
            dosya = openFileDialog1.FileName.ToString();

            ClsExcelReader er = new ClsExcelReader();
            DataSet ds = er.readCSV(dosya);

            dataGridViewScanner.DataSource = ds;
}
Posted
Updated 4-Sep-12 21:11pm
v3
Comments
Peter_in_2780 5-Sep-12 2:55am    
Do not ask your question more than once. It is called cross-posting, and it annoys people who otherwise might help you.
cell-in 5-Sep-12 3:12am    
Sorry, I couldn't notice to ask my question in true topic before
manognya kota 5-Sep-12 2:56am    
What are you sending in the file parameter to this method?
cell-in 5-Sep-12 3:12am    
I get my parameter by openFileDialog. I added that code in my question.
manognya kota 5-Sep-12 3:31am    
When you are selecting your file using a filedialog, why are you again using this code.
if (!File.Exists(file))
return null;
string pathOnly = Path.GetDirectoryName(file);
I guess this gives you only the folder and not the filename when building your connection string.

I guess, this link should help you out.

http://www.gridview.net/read-excel-file-in-c/[^]
 
Share this answer
 
I solved my problem.
The problem is about my file name.
My file name include a lot of "." character.

When i choose the file, i change the file name.
fileName.Replace(".", "_")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900