0
votes

Here users browse to the Excel file from there system and then data is uploaded to sql database table.

I get this Error in Following Code : The Microsoft Jet database engine could not find the object 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Student Registration1.xls'. Make sure the object exists and that you spell its name and the path name correctly.

I have no idea why it is asking for this Path ? C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\

protected void import_xls_Click(object sender, EventArgs e)
    {
        try
        {
            if (xmlupload.HasFile)
            {
                string path = xmlupload.PostedFile.FileName;


                string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";

                OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                excelConnection.Open();

                OleDbCommand cmd = new OleDbCommand("select * from [Sheet2$]", excelConnection);                    
                OleDbDataReader dReader = cmd.ExecuteReader();

                SqlBulkCopy sqlBulk = new SqlBulkCopy(SqlConnectionstring.mainConnectionString, SqlBulkCopyOptions.KeepIdentity);

                sqlBulk.DestinationTableName = "dbo.studentA";                    
                sqlBulk.ColumnMappings.Add("UserName", "UserName");
                sqlBulk.ColumnMappings.Add("Password", "Password");
                sqlBulk.ColumnMappings.Add("Name", "Name");
                sqlBulk.ColumnMappings.Add("Standard", "Standard");
                sqlBulk.ColumnMappings.Add("Division", "Division");
                sqlBulk.ColumnMappings.Add("SchoolName", "SchoolName");
                sqlBulk.ColumnMappings.Add("Language", "Language");
                sqlBulk.ColumnMappings.Add("ExamStatus", "ExamStatus");
                sqlBulk.ColumnMappings.Add("Result", "Result");                   

                sqlBulk.WriteToServer(dReader);

                lblmsg.Visible = true;
                lblmsg.Text = "Your data uploaded successfully";
                excelConnection.Close();
            }
        }
        catch (Exception ex)
        {
            lblmsg.Visible = true;
            lblmsg.Text = ex.Message.ToString();

        }

SqlConnectionstring.cs :

public class SqlConnectionstring
{
public SqlConnectionstring()
{
    //
    // TODO: Add constructor logic here
    //
}


public static readonly string mainConnectionString = ConfigurationManager.ConnectionStrings["constring"].ToString();
}
1
Well, what's in the path var you're using in the OleDBCommand call? Pretty much guaranteed that if it's not an absolute path, C# will use the current working directory of your program, which is almost certainly wherever the .exe for your compiled app is going. - Marc B
What is the value of 'path' and where do you have stored the excel file? (You have saved the file before right?) - Steve
@MarcB Path is getting the value of client side excel sheet. It can be at any drive and of any name. - Rushikesh Korgaonkar
@Steve Path is getting the value of client side excel sheet using FileUpload tool. It can be at any drive and of any name. - Rushikesh Korgaonkar
According to MSDN The file name that the FileName property returns does not include the path of the file on the client. - Steve

1 Answers

3
votes

I will try to save the file in a working directory and then try to open from there.
If the fileName has no path then the connection could find the file only if it is in the
current directory (Usually where your program start)

string savePath = @"c:\temp\uploads";

if (xmlupload.HasFile)
{
  string fileName = xmlupload.FileName;
  savePath = Path.Combine(savePath, fileName);
  xmlupload.SaveAs(savePath);
  string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
                      savePath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";   

  ....

On a web server, to avoid permission problems, it is better to keep the file in a subfolder of the root folder of your site.

In this case the folder could be fully qualified using the HttpServerUtility.MapMath method.