0

While uploading excel it is giving error, At first its work fine but today it is giving error, I had implemented the save thing in another project and there it is working properly, I tried to give the exact name as sheet name as suggested in articles but nothing worked.

HttpFileCollection uploads = HttpContext.Current.Request.Files;
        for (int i = 0; i < uploads.Count; i++)
        {
            HttpPostedFile upload = uploads[i];
            if (upload.ContentLength == 0)
                continue;
            string c = System.IO.Path.GetFileName(upload.FileName);
            try
            {
                upload.SaveAs(Server.MapPath("~/Files\\") + c);
            }
            catch (Exception Exp)
            {
                throw (Exp);
            }
        }
        if (fileCustSite.PostedFile != null)
        {
            HttpPostedFile attFile = fileCustSite.PostedFile;
            int attachFileLength = attFile.ContentLength;
            if (attachFileLength > 0)
            {
                if (fileCustSite.PostedFile.ContentLength > 0)
                {
                    string Extension = Path.GetExtension(fileCustSite.PostedFile.FileName);
                    string inFileName = Path.GetFileName(fileCustSite.PostedFile.FileName);
                    string pathDataSource = Server.MapPath("~/Files\\") + inFileName;
                    string conStr = "";
                    if (Extension == ".xls" || Extension == ".xlsx")
                    {
                        switch (Extension)
                        {
                            case ".xls": //Excel 1997-2003   Provider=Microsoft.Jet.OLEDB.4.0;
                                conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;" + "Data Source='" + pathDataSource.ToString() + "';" + "Extended Properties=Excel 8.0;";
                                break;

                            case ".xlsx": //Excel 2007  Provider=Microsoft.ACE.OLEDB.12.0;
                                conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;" + "Data Source='" + pathDataSource.ToString() + "';" + "Extended Properties=Excel 8.0;";
                                break;

                            default:
                                break;
                        }
                        try
                        {
                            OleDbConnection connExcel = new OleDbConnection(conStr.ToString());
                            OleDbCommand cmdExcel = new OleDbCommand();
                            OleDbDataAdapter oda = new OleDbDataAdapter();
                            connExcel.Open();
                            DataSet ds = new DataSet();

                            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + "POP_Upload_Template" + "$]", conStr.ToString());
                            da.Fill(ds);
                            gdvUpExcel.DataSource = ds;
                            gdvUpExcel.DataBind();
                            Session["Table1"] = ds.Tables[0];
                            if (gdvUpExcel.Columns.Count > 0)
                            {
                            }
                            connExcel.Close();
                            if (File.Exists(pathDataSource))
                            {
                                File.Delete(pathDataSource);
                            }
                        }

This is my excel upload format

  • Where in the code are you getting that error? What _exactly_ is the purpose of this code? And what is `fileCustSite`? – Nyerguds Jun 21 '18 at 08:09
  • btw, the excel 1997-2003 provider string in your code is identical to the 2007 one. And there's not much use in calling .ToString() on that string... – Nyerguds Jun 21 '18 at 08:16
  • @Nyerguds fileCustSite is id of my fileupload control. I am getting error in this line:- da.Fill(ds); – Bhupendra Jun 21 '18 at 08:57

0 Answers0