Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel OleDb
I am using the following code to import data using Excel sheet. It is working fine in localhost, but not in server. I d't know the reason. The file is storing in a folder, but the data fetching and saving in database is not happening. Is the server need to install the MS Excel ?.the oconn.Open() function is not happening,from their only the error is comming..Plz gve me a solution.
 
string extension = Path.GetExtension(FileUpload1.FileName);
                filename = DateTime.Now.ToString("dd-MM-yyyy") + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Millisecond + extension;
                FileUpload1.SaveAs(Server.MapPath("~/Account/PublishedRanklist/") + filename);
                string path = Server.MapPath("~/Account/PublishedRanklist/" + filename);
                OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0");
               
                    OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", oconn);
                    oconn.Open();
                    OleDbDataReader odr = ocmd.ExecuteReader();
                    while (odr.Read())
                   {       int Regno=valid(odr, 0);
 
                   }
 
                  oconn.Close();
 

protected string valid(OleDbDataReader myreader, int stval)//if any columns are found null then they are replaced by zero
        {
            object val = myreader[stval];
            if (val != DBNull.Value)
                return val.ToString();
            else
                return Convert.ToString(0);
        }
Posted 16-Jan-14 21:11pm
Edited 17-Jan-14 17:44pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

For reading the excel using Oledb you do not require Microsoft Excel to be installed.
 
Instead of using DataReader
OleDbDataReader odr = ocmd.ExecuteReader();
try using Datatable and check if data are getting filled in DataTable or not?
  Permalink  
v2
Comments
Member 9492907 at 17-Jan-14 4:08am
   
I am using the OleDbDataReader only. you can see it in my code. It is working fine in localhost, only in the server not working.
Deviprasad Das at 17-Jan-14 4:27am
   
Then please check if "Microsoft.Jet.OLEDB.4.0" adapter is registered in the server or not.
Member 9492907 at 17-Jan-14 4:35am
   
Can u plz tell me how to check it
Member 9492907 at 17-Jan-14 7:53am
   
oconn.Open(); is not happening...How to solve it in server
Gopi Kishan Mariyala at 17-Jan-14 23:46pm
   
Check if the server has folder access permissions. Is the file located on server. Then check if the IIS has access to that folder
Member 9492907 at 18-Jan-14 2:31am
   
The IIS has the full permission to access the folder. anything need to install or something in the server
Gopi Kishan Mariyala at 18-Jan-14 4:32am
   
What is the exact error message you are getting when you do Oconn.Open()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 200
1 Kornfeld Eliyahu Peter 175
2 BillWoodruff 160
3 George Jonsson 140
4 OriginalGriff 126


Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 18 Jan 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100