Click here to Skip to main content
14,607,924 members
Rate this:
Please Sign up or sign in to vote.
See more:
I followed this tutorial, but I must have missed something, because I am unable to upload the excel file to the folder in my project, that is acting as a local server.
https://www.youtube.com/watch?v=fP6RLBWf894

Please let me know what can I do to fix this problem.
I am not getting any errors, but the file wont display in grid, and it is not being uploaded to folder.
I also need to be able to select among all the sheets in the workbook, instead of just opening the first sheet.
Thanks.

What I have tried:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;
using System.IO;
using System.Data;

namespace Interfaz
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string filePath = ConfigurationManager.AppSettings["FilePath"].ToString();
            string filename = string.Empty;
            if (FileUpload1.HasFile)
            {
                try
                {
                    string[] allowFile = {".xls",".xlsx" };
                    string FileExt = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
                    bool isValidFile = allowFile.Contains(FileExt);
                    if (!isValidFile)
                    {
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Porfavor solo suba archivos de Excel";
                    }
                    else
                    {
                        //Upload only less or equal to 1 mb
                        /*int FileSize = FileUpload1.PostedFile.ContentLength;
                        if (FileSize<=1048576)
                        {

                        }*/
                        filename = Path.GetFileName(Server.MapPath(FileUpload1.FileName));
                        FileUpload1.SaveAs(Server.MapPath(filePath) + filename);
                        OleDbConnection con = null;
                        if (FileExt==".xls")
                        {
                            grid.Visible = true;
                            con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filePath+";Extended Properties=Excel 8.0;");
                        }
                        if (FileExt == ".xlsx")
                        {
                            grid.Visible = true;
                            con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;");
                        }
                        con.Open();
                        DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
                        String[] sheetNames = new String[dt.Rows.Count];
                        /*int i = 0;
                        foreach (DataRow row in dt.Rows)
                        {
                            cb.Items.Add(row["TABLE_NAME"].ToString());
                            i++;
                        }*/
                        OleDbCommand ExcelCommand = new OleDbCommand(@"SELECT * FROM ["+sheetNames+@"]",con);
                        OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
                        DataSet ExcelDataSet = new DataSet();
                        con.Close();
                        grid.DataSource = ExcelDataSet;
                        grid.DataBind();
                    }
                }
                catch (Exception ex) { }
            }
        }
    }
}
Posted
Updated 15-Jun-20 9:36am
Comments
CHill60 16-Nov-17 19:02pm
   
"Please let me know what can I do to fix this problem." ... learn to debug
Karthik_Mahalingam 16-Nov-17 22:56pm
   
are you getting any error ?
add the below code
 catch (Exception ex) { throw ex; } 
F-ES Sitecore 17-Nov-17 5:56am
   
We don't have access to the html markup or the file you are using to upload or the config. The code looks ok but it depends on many things we have no access to. As above, learn to debug your code. Step through it to see what is happening line by line, and get rid of the catch block. If the code is throwing an error you won't know what it is.
Sinisa Hajnal 17-Nov-17 11:26am
   
1. Put something into catch block - the way you did it you just swallow the exception and that is why you don't get the error. It would be better to NOT have try catch than to have empty catch block
2. use finally and dispose of any connection, transaction and other disposables.
3. learn to use breakpoints and debug
   
First save your file in the server using server.mappath

second check whether the driver is registered or not for oldb

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

Wow, this is an old question. The thing that I'd want to know is what do you have in ConfigurationManager.AppSettings["FilePath"] ? You're doing a Server.MapPath(...) to save the file (with the original filename and extension) but in your connection string you're just using the (relative?) filepath value. This *might* work but probably won't depending on your AppSettings value (the OLEDB driver needs the full absolute path). But more importantly, the driver needs the filename, not just the folder, and you're not giving it that - so the connection open will fail, an exception will be thrown, and as you don't handle it you'll be none the wiser. Read and action the comments from November, use the correct path and file in your connection string, then step through and see what's happening if there's still a problem. Oh, and think about whether you need FileUpload1.PostedFile.FileName or FileUpload.FileName - then use it consistently! (Or better still just save it in a local variable for performance).
   

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




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