Advertisement

05.20.2008 at 10:54AM PDT, ID: 23418176
[x]
Attachment Details

Reading in from Excel into Dataset in .NET

Asked by LuckyLucks in .Net Editors & IDEs, C# Programming Language, Microsoft Excel Spreadsheet Software

Hi:
   I was trying to read data from an Excel file into a dataset. The file is retreived from a FileUpload. The below is my code and the error received. Please advice why it is occuring and how to fix it.

The Microsoft Jet database engine could not find the object 'fileName'.  Make sure the object exists and that you spell its name and the path name correctly.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine could not find the object 'fileName'.  Make sure the object exists and that you spell its name and the path name correctly.

Source Error:


Line 11657:                  DataSet ds = new DataSet();
Line 11658:
Line 11659:                  da.Fill(ds, "Employees");
Line 11660:                        
Line 11661:                                    // Validate each column in this line.If error, reject the transaction. Else continue
 


[OleDbException (0x80040e37): The Microsoft Jet database engine could not find the object 'fileName'.  Make sure the object exists and that you spell its name and the path name correctly.]
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +267
   System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +192
   System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +48
   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +106
   System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) +111
   System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +4
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +141
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +137
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +83
   genumd.TableEdit3.UploadFile(String fileName, String fileExtension, String filePath) in c:\Inetpub\wwwroot\genumd\TableEdit3.aspx.cs:11659
   genumd.TableEdit3.btnDBUpload_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\genumd\TableEdit3.aspx.cs:11612
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

Code:

protected void btnDBUpload_Click(object sender, System.EventArgs e)
{
                  
            string saveServDir = @"Uploads\";      // Server dir for storing uploaded csvs
            string appPath = Request.PhysicalApplicationPath;

      

            if (brwUploadFile.HasFile)
            {
                  string savePath = appPath + saveServDir + Server.HtmlEncode (brwUploadFile.FileName);

                        
                  // Save file on server            
                  brwUploadFile.SaveAs (savePath);

                  // Upload to database
                  UploadFile (brwUploadFile.PostedFile.FileName, extension, savePath);

                  // Success...
                  lblDBUpload.Text = "Your file was uploaded successfully";
                        

            }
            else
            {
                  lblDBUpload.Text = "No file selected to upload";
            }
                                    
}
//btnDBUpload_Click

/// Reads the file and uploads to the database if successfully validated.
/// Otherwise, write error to output file
protected void UploadFile(string fileName, string fileExtension, string filePath)
{
      string connectionString;
      string commandText;
      System.Data.OleDb.OleDbConnection conn;
      System.Data.OleDb.OleDbCommand command;
      int count;
      string strMsg;

      connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath +  ";Extended Properties='Excel 8.0;HDR=Yes'";
      commandText = "select * from fileName";
 
      conn = new System.Data.OleDb.OleDbConnection(connectionString);
      command = new System.Data.OleDb.OleDbCommand(commandText, conn);
 
      conn.Open();
 
      System.Data.OleDb.OleDbDataAdapter da= new                         System.Data.OleDb.OleDbDataAdapter(commandText, conn);
      DataSet ds = new DataSet();

      da.Fill(ds, "MyData");                              

}
// UploadFile
Start Free Trial
[+][-]05.20.2008 at 10:59AM PDT, ID: 21608580

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.20.2008 at 11:23AM PDT, ID: 21608779

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.20.2008 at 11:41AM PDT, ID: 21608952

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .Net Editors & IDEs, C# Programming Language, Microsoft Excel Spreadsheet Software
Sign Up Now!
Solution Provided By: junges
Participating Experts: 3
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628