Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on a tool that basically allow users select an excel file from their computer, preview it in a datagrid before submitting to the database. Currently this is what the code that opens the file looks like


C#
private void btn_browse_file_Click(object sender, EventArgs e)
     {
         OpenFileDialog openFileDialog1 = new OpenFileDialog();
         openFileDialog1.CheckFileExists = true;
         openFileDialog1.AddExtension = true;
         openFileDialog1.Multiselect = false;
         openFileDialog1.Filter = "Excel files (*.xls) | *.xlsx";
         openFileDialog1.Title = "Select a spreadsheet file";


         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
             openFileDialog1.OpenFile();

          //  MessageBox.Show(openFileDialog1.FileName.ToString());
             label_display_file_name.Text = openFileDialog1.FileName.ToString();
         }

     }


and this is the code that is meant to display the excel file in the datagridview
private void btn_preview_Click(object sender, EventArgs e)
{
    OleDbConnection ConnectToExcel = new OleDbConnection();
    DataSet excelds = new DataSet();
    OleDbDataAdapter excelda = new OleDbDataAdapter();
    OleDbCommand excelcmd = new OleDbCommand();
    ConnectToExcel = new OleDbConnection (String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;""", openFileDialog1.FileName.ToString()));
    excelcmd = new OleDbCommand (String.Format("SELECT * FROM [{0}$]", ConnectToExcel, openFileDialog1.FileName));
    excelda = new OleDbDataAdapter(excelcmd);
    excelda.Fill(excelds);
    dataGridView1.DataSource = excelds.Tables[0];


}


For some reasons I don't yet know, the code to display the excel file on the datagrid gives me the following error "Fill:SelectCommand.Connection property has not been initialized".

Any help or ideas on how to get this work will be much appreciated.
Posted

This code fails because you have used the String.Format syntax incorrectly. Which in return is causing the Command to not have a connection assigned to it. Change your code to this below:

excelcmd = new OleDbCommand(String.Format("SELECT * FROM [{0}]$", openFileDialog1.FileName), ConnectToExcel);
 
Share this answer
 
Comments
Uzoma Umekwe 21-Nov-13 10:49am    
Hi
Thanks for your help. I have changed my code like you said but now I get the following error
"C:\Users\admin\Desktop\Updatedsearchterm.xlsx$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long."

Not sure what I am not getting right. Any help is appreciated. Thanks
Richard C Bishop 21-Nov-13 10:50am    
That is because you need to Select from the SheetName of the excel file, not the file name.

The default sheet name is usually "Sheet1".
Uzoma Umekwe 21-Nov-13 11:04am    
Thanks buddy! All sorted.
Richard C Bishop 21-Nov-13 13:48pm    
You are welcome. Come back anytime.
You need to properly set connectionstring[^].

OledbCommand should looks like:
"SELECT * FROM [Sheet1]$"


Please, visit below link. There you'll find details ;)
How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET[^]
 
Share this answer
 
Comments
Uzoma Umekwe 22-Nov-13 8:42am    
thanks Maciej
Maciej Los 22-Nov-13 8:43am    
You're welcome ;)

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