Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:05:59+00:00 2026-06-17T21:05:59+00:00

On my local machine everything works well but.. After publishing my MVC4 web project

  • 0

On my local machine everything works well but..

After publishing my MVC4 web project there is a problem with an uploaded Excel file.
I load an HttpPostedFileBase and send the path to my BL. There I load it to dataTable and on my second call I get it to a list.

Here is the code..

Controller:

  [HttpPost]
    public ActionResult UploadCards(HttpPostedFileBase file, string sheetName, int ProductID)
    {
        try
        {
            if (file == null || file.ContentLength == 0)
                throw new Exception("The user not selected a file..");

            var fileName = Path.GetFileName(file.FileName);
            var path = Server.MapPath("/bin");

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            path = Path.Combine(path, fileName);
            file.SaveAs(path);

            DataTable cardsDataTable = logic.LoadXLS(path, sheetName);
            cardsToUpdate = logic.getUpdateCards(cardsDataTable, ProductID);

            foreach (var item in cardsToUpdate)
            {
                if (db.Cards.ToList().Exists(x => x.SerialNumber == item.SerialNumber))
                    cardsToUpdate.Remove(item);
            }
            Session["InfoMsg"] = "click update to finish";
        }
        catch (Exception ex)
        {
            Session["ErrorMsg"] = ex.Message;
        }
        return View("viewUploadCards", cardsToUpdate);
    }

BL:

     public DataTable LoadXLS(string strFile, String sheetName)
    {
        DataTable dtXLS = new DataTable(sheetName);

        try
        {
            string strConnectionString = "";

            if (strFile.Trim().EndsWith(".xlsx"))
                strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", strFile);
            else if (strFile.Trim().EndsWith(".xls"))
                strConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", strFile);

            OleDbConnection SQLConn = new OleDbConnection(strConnectionString);

            SQLConn.Open();

            OleDbDataAdapter SQLAdapter = new OleDbDataAdapter();

            string sql = "SELECT * FROM [" + sheetName + "$]";

            OleDbCommand selectCMD = new OleDbCommand(sql, SQLConn);

            SQLAdapter.SelectCommand = selectCMD;

            SQLAdapter.Fill(dtXLS);

            SQLConn.Close();

        }

        catch (Exception ex)
        {
            string res = ex.Message;
            return null;
        }

        return dtXLS;
    }

and:

    public List<Card> getUpdateCards(DataTable dt, int prodId)
    {
        List<Card> cards = new List<Card>();
        try
        {
            Product product = db.Products.Single(p => p.ProductID == prodId);
            foreach (DataRow row in dt.Rows)
            {
                cards.Add(new Card
                {
                    SerialNumber = row[0].ToString(),
                    UserName = row[1].ToString(),
                    Password = row[2].ToString(),

                    Activated = false,

                    Month = product.Months,
                    Bandwidth = product.Bandwidth,
                    ProductID = product.ProductID,
                    // Product = product
                });
            }
        }
        catch (Exception ex)
        {
            db.Log.Add(new Log { LogDate = DateTime.Now, LogMsg = "Error : " + ex.Message });

        }
        return cards;
    }

Now I think Windows Azure doesn’t let me save this file because on the middle view when I supposed to see the data – I don’t see it.

I thought of some ways…
one – not saving the file, but I don’t see how to complete the ConnectionString…
second maybe there is a way to save the file there.

I’d love to get suggestions for solving this problem…

10x and sorry for my bad English =)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:06:00+00:00Added an answer on June 17, 2026 at 9:06 pm

    I’m embarrassed but I found a similar question here.. Not exactly but it gave me a good direction.

    Hare the finally result:

    [HttpPost]
        public ActionResult UploadCards(HttpPostedFileBase file, string sheetName, int ProductID)
        {
            IExcelDataReader excelReader = null;
            try
            {
                if (file == null || file.ContentLength == 0)
                    throw new Exception("The user not selected a file..");
    
                if (file.FileName.Trim().EndsWith(".xlsx"))
                    excelReader = ExcelReaderFactory.CreateOpenXmlReader(file.InputStream);
                else if (file.FileName.Trim().EndsWith(".xls"))
                    excelReader = ExcelReaderFactory.CreateBinaryReader(file.InputStream);
                else
                    throw new Exception("Not a excel file");
    
                cardsToUpdate = logic.getUpdateCards(excelReader.AsDataSet().Tables[sheetName], ProductID);
    
                foreach (var item in cardsToUpdate)
                {
                    if (db.Cards.ToList().Exists(x => x.SerialNumber == item.SerialNumber))
                        cardsToUpdate.Remove(item);
                }
                Session["InfoMsg"] = "Click Update to finish";
            }
            catch (Exception ex)
            {
                Session["ErrorMsg"] = ex.Message;
            }
            finally
            {
                excelReader.Close();
            }
            return View("viewUploadCards", cardsToUpdate);
        }  
    

    10q all.

    EDIT: download, reference and using

    the dll is avalibale hare
    i add the reference to the Excel.dll and i add the using Excel;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i created a flipbook with help of turn.js. on my local machine everything works
We want to use Microsoft.Office.Interop.Excel in our web application. Everything works fine on our
Bootstrap's popover works perfectly on my local machine but doesn't seem to work on
I am having a problem on my local machine when I am doing testing.
In web projects on my local machine, I'm using a fairly simple Sass setup.
I'm having a problem. I'm using jquery.lightbox-0.5 to display images and everything works fine
I have a ASP.NET web app that utilizes ReportViewer to show local reports. Everything
I have problem with connecting to Sql Server from my local machine. Seems like
I currently have a web project developed with Codeigniter. My production environment works as
I have a problem creating an index with Zend_Search_Lucene. Now, everything works fine on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.