0
votes

I am doing this to read :

private bool writetoven(string xlspath)
{
    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString)


    try
    {
        OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", excelConnection);
        excelConnection.Open();
        OleDbDataReader odr = ocmd.ExecuteReader();
        string vcode = "";
        string pswd = "";
        string vname = "";

        while (odr.Read())
        {
            vcode = valid(odr, 0);
            pswd = valid(odr, 1);
            vname = valid(odr, 2);

            insertdataintosql(vcode,pswd,vname);
        }
        excelConnection.Close();
        return true;
    }
    catch (DataException)
    {
        return false;
    }
    finally
    {
        lblmsg4.Text = "Data Inserted Sucessfully";
    }
}

and my connection string is like this:

excelConnectionString = "provider=Microsoft.jet.oledb.4.0;data source=" + 
                        filepath1 + 
                        ";extended properties='Excel 8.0;HDR=YES;'";

but I am getting an error as

The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data.

Line 1574: OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
Line 1575:
Line 1576: excelConnection.Open();
Line 1577:
Line 1578:

It seems like the file is still open but its not and I have checked the running process and its not there

Now what should I do? ...My Excel sheet is closed but I am getting this error

i dont have microsoft access on my com is that can be an issue

is this problem is something to do with my fileupload control that i am using??

3
Is there any special reason why you are calling excelConnection.Open(); twice? Furthermore I would check TaskManager if there is any instance of Excel running in background (not visible) with your file open! - Pilgerstorfer Franz
yes that was my fault sorry ... but i removed that and still not able to work - Drone
is this problem is something to do with my fileupload control that i am using?? - Drone
Try using LINQ-To-Excel and see if that works for you. - Enigmativity
do you have any excel process running in your task manager? - Karthik

3 Answers

2
votes

Well, the error says :

cannot open the file ''.

So it almost seems as if your Excel connection string isn't valid - it doesn't have a valid Excel file name in there!

When and where are you setting the Excel connection string??

Is the filepath1 you use valid in that moment?

Does it contain the valid Excel file name ??

Update: why I don't understand is: you're passing in xlspath as a parameter to your method - yet, you don't seem to use that xlspath anywhere - most certainly not to concatenate together your excelConnectionString ....

private bool writetoven(string xlspath)
{
    OleDbConnection excelConnection = new OleDbConnection(excelConnectionString)

Can you put a breakpoint on this line - what does your excelConnectionString look like - just before your create the connection? Could it be that xlspath contains vendor.xls - but your Excel connection string just never even gets set to that value??

1
votes

Your excel file is open in the Microsoft Excel. Close the Excel window and everything will be fine.

1
votes

Make sure the account your web app is running under has permissions on the file.

Also, since you are not using a path, and just a filename, it might not be finding the file where it is at. Where is the file located relative to your web application?

Does System.IO.File.ReadAllBytes(filepath1) succeeed or fail?

Since you are dealing with a file upload:

The user is uploading from their computer through the browser, and the file is transferred to your server in the post response. See: http://blog.divergencehosting.com/2009/03/12/upload-read-parse-file-aspnet/

Even if you had the full path, you wouldn't be able to access the file on the user's computer from your server. The browser sends you a copy of the file in uploadControl.PostedFile.InputStream; where uploadControl is the name of your asp:FileUpload control.

You would then need to save this stream to a file location on the server, and provide this full path to the connection string.

If you used something like EPPlus to read the file instead of the OleDbConnection, you could use the in-memory stream instead of saving the file first.