Click here to Skip to main content
16,018,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have been trying to upload an xls file and save them in a path and receive a view on success showing the xls file up this throws up

[HttpPost]
       public ActionResult Import(HttpPostedFileBase excelfile)
       {
           if (excelfile == null || excelfile.ContentLength == 0)
           {
               ViewBag.Error = "Please select the updated file<br>";
               return View("About");
           }
           else
           {
               if(excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith
                   ("xlsx"))
               {
                   string path = Server.MapPath("~/Excel/" + excelfile.FileName);
                   if (System.IO.File.Exists(path))
                       System.IO.File.Delete(path);
                   excelfile.SaveAs(path);
                   //Read data from excel file
                   Excel.Application application = new Excel.Application();
                   Excel.Workbook workbook = application.Workbooks.Open(path);
                   Excel.Worksheet worksheet = workbook.ActiveSheet;
                   Excel.Range range = worksheet.UsedRange;
                   List<Details> listExcels = new List<Details>();
                   for(int row = 3; row <= range.Rows.Count; row++)
                   {
                       Details d = new Details();
                       d.UserID = ((Excel.Range)range.Cells[row, 1]).Text;
                       //where error comes
                       d.Name = ((Excel.Range)range.Cells[row, 2]).Text;
                       d.Enroll_ID = ((Excel.Range)range.Cells[row, 3]).Text;
                       d.Place = ((Excel.Range)range.Cells[row, 4]).Text;
                       d.Date = ((Excel.Range)range.Cells[row, 5]).Text;
                       d.Time = ((Excel.Range)range.Cells[row, 6]).Text;
                       d.Att_Type = ((Excel.Range)range.Cells[row, 7]).Text;
                       listExcels.Add(d);
                   }
                   ViewBag.ListExcels = listExcels;
                   return View("Success");
               }
               else
               {
                   ViewBag.Error = "File type is incorrect<br>";
                   return View("About");
               }
           }
       }


What I have tried:

its says i m trying to change a string to int which i m not doing kinda newbie to coding so do help
this is the exact error on chrome

Server Error in '/' Application.
Cannot implicitly convert type 'string' to 'int'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'string' to 'int'

Source Error: 


Line 53:                     {
Line 54:                         Details d = new Details();
Line 55:                         d.UserID = ((Excel.Range)range.Cells[row, 1]).Text;
Line 56:                         d.Name = ((Excel.Range)range.Cells[row, 2]).Text;
Line 57:                         d.Enroll_ID = ((Excel.Range)range.Cells[row, 3]).Text;
Posted
Updated 27-Dec-17 19:32pm
v2

use Int32.TryParse Method [^] to convert string values to integer
 int userId;
 bool isValid = int.TryParse( ((Excel.Range)range.Cells[row, 1]).Text , out userId);
if(isValid){
 d.UserID = userId
}
else
{
  // invalid Numeric Text present in the cell, notify it to the user.
}
 
Share this answer
 
Comments
Athfan 28-Dec-17 2:00am    
thanks a lot guys as u said tryparse method helped
Athfan 28-Dec-17 2:03am    
will that work the same for enroll_id
Karthik_Mahalingam 28-Dec-17 2:31am    
if enroll_id is an integer type then it will work
it applies for all integer types.
The following looks suspicious (although I'm not a web development expert):
return View("About")

The return type of the method is an ActionResult.

Are you sure that the View function accepts a string (i.e., "About"), and that it yields an ActionResult?


Alternatively, cosider the following:
d.Enroll_ID = ((Excel.Range)range.Cells[row, 3]).Text;


The Text property will return a string. Is the Enroll_ID an int? Perhaps that is what is causing the problem. And are you sure that that particular Excel cell can't contain anything other than a numeric string?

The same is true for other properties on the 'd' object, like Date and Time.
 
Share this answer
 
Comments
Athfan 28-Dec-17 1:29am    
well obviously date time enroll id and user id are all int values on excel sheet and in the code and yeah about does accept a string
Member 13587529 28-Dec-17 2:04am    
So why aren't you casting the cell value?

It may be an int on the sheet itself, and it may be being assigned to an int property on the "d" object, but it's being retrieved via the *string-based* .Text property of the cell.

The same, like I say, is true of Date and Time. They need to be casted when retrieved via the .Text property.

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



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