简体   繁体   中英

C# OLEDB connection for EXCEL not working on loop

1-I am having a strange problem.

Requirement: read a list of excel files on a folder and store the content on a dataset.

What I have written: Button click:

protected void btnUploadExcelFiles_Click(object sender, EventArgs e)
{
            string[] strFiles = Directory.GetFiles(strPath);
            foreach (string strFile in strFiles)
            {
                ClsExcelUpload objExcelUpload = new ClsExcelUpload();
                string szConnectionString1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + strFile + "';Extended Properties=\"Excel 12.0;HDR=YES;\"";
                objExcelUpload.ReadExcel(szConnectionString1, strFile);

            }
}

function ReadExcel:

public string ReadExcel(string szConnectionString, string strFile)
    {
        OleDbConnection objConn = new OleDbConnection();
        objConn.ConnectionString = szConnectionString;
        objConn.Open();

        OleDbDataAdapter DAobjBasicInfo = new OleDbDataAdapter();
        DAobjBasicInfo = new OleDbDataAdapter("select * from [Sheet1$]", objConn);
        DataTable dt1 = new DataTable();
        DAobjBasicInfo.Fill(dt1);
        objConn.Close();
        objConn.Dispose();
        return "";
    }

Problem: The szConnectionString gets updated with the new filename set through the loop but when the OleDbDataAdapter opens the objConn, it looks for the Excel file which is already deleted. and the datatable everytime gets the first file data everytime.

This is the strangest thing I have ever seen as I am not able to identify the area of problem why the oledbconnection is not getting updated.


entire code:

using System.Data;

using System.Data.OleDb;

    protected void btnUploadExcelFiles_Click(object sender, EventArgs e)
    {
        HttpFileCollection files = Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            if (file.ContentLength > 0)
            {
                FileUploadExcel.SaveAs(Server.MapPath(@"~\UploadedFile\") + file.FileName);

            }
        }
    string strPath = Path.GetFullPath(Server.MapPath(@"~\UploadedFile\"));
    string[] strFiles = Directory.GetFiles(strPath);
    foreach (string strFile in strFiles)
    {
        ClsExcelUpload objExcelUpload = new ClsExcelUpload();
        string szConnectionString1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + strFile + "';Extended Properties=\"Excel 12.0;HDR=YES;\"";
      string result=  ReadExcel(szConnectionString1, strFile);

    }
}


    public string ReadExcel(string szConnectionString, string strFile)
    {
        OleDbConnection objConn = new OleDbConnection();
        objConn.ConnectionString = szConnectionString;
        objConn.Open();

        OleDbDataAdapter DAobjBasicInfo = new OleDbDataAdapter();
        DAobjBasicInfo = new OleDbDataAdapter("select * from [Sheet1$]", objConn);
        DataTable dt1 = new DataTable();
        DAobjBasicInfo.Fill(dt1);
        objConn.Close();
        objConn.Dispose();
        File.Delete(strFile);
        return "";
    }

Further analysis on this strange issue: I would try to explain in more detail. I have one FileUpload control which allows multiple files to select and upload. a command button which copies the posted file through FileUpload to a specified location. Upto here everything works as expected. Now I have the code written on the same command button to read the excel file which is copied. The first file reads correctly. while reading the second file it finds the same data.

Now to test it further. I have moved the file read code to a different command button. Now I did 2 tests. Test 1. Upload 2 files using file upload Click on command button 1 to copy the posted files to spefified location click on Command button 2 to loop through the files on copied folder and read the files. Result: Same issue, reading second file it gets the same data. Test 2: Run the application. Do not select and upload any file. Manually copy the same to files to the specified location. click the command button 2 to read the files. File reads correctly.

its very clear there is nothing wrong with the function to read files. When File Upload is working there must be some conflict in memory management. I wonder whats my alternative to the requirement.

The issue has been resolved. There's nothing wrong with the read excel. The problem is saving the posted file to specified location.

protected void btnUploadExcelFiles_Click(object sender, EventArgs e)
{
    HttpFileCollection files = Request.Files;
    for (int i = 0; i < files.Count; i++)
    {
        HttpPostedFile file = files[i];
        if (file.ContentLength > 0)
        {
            FileUploadExcel.SaveAs(Server.MapPath(@"~\UploadedFile\") + file.FileName);

        }
    }
string strPath = Path.GetFullPath(Server.MapPath(@"~\UploadedFile\"));
string[] strFiles = Directory.GetFiles(strPath);
foreach (string strFile in strFiles)
{
    ClsExcelUpload objExcelUpload = new ClsExcelUpload();
    string szConnectionString1 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + strFile + "';Extended Properties=\"Excel 12.0;HDR=YES;\"";
  string result=  ReadExcel(szConnectionString1, strFile);

}

}

        }
    }

it was reading and copying the same file with different name.So While reading from specified location it was reading same data from different files.

the corrected function as follows

        protected void btnUploadExcelFiles_Click(object sender, EventArgs e)
    {
        List<string> LstStrPriExcelUploadError = new List<string>();
        HttpFileCollection files = Request.Files;
        foreach (HttpPostedFile uploadedFile in FileUploadExcel.PostedFiles)
        {
            string fn = System.IO.Path.GetFileName(uploadedFile.FileName);
            string SaveLocation = Server.MapPath(@"~\UploadedFile\") + "\\" + fn;

            uploadedFile.SaveAs(SaveLocation);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM