Click here to Skip to main content
15,883,796 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
C#
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();



C#
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
Updated 17-Jan-14 17:44pm
v2

For reading the excel using Oledb you do not require Microsoft Excel to be installed.

Instead of using DataReader
C#
OleDbDataReader odr = ocmd.ExecuteReader();

try using Datatable and check if data are getting filled in DataTable or not?
 
Share this answer
 
v2
Comments
Member 9492907 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 17-Jan-14 4:27am    
Then please check if "Microsoft.Jet.OLEDB.4.0" adapter is registered in the server or not.
Member 9492907 17-Jan-14 4:35am    
Can u plz tell me how to check it
Member 9492907 17-Jan-14 7:53am    
oconn.Open(); is not happening...How to solve it in server
Gopi Kishan Mariyala 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

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

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900