I read an Excel file from ASP: NET MVC to localhost without problems. But when you publish the site on error. On the server is not installed the office or any Excel library. Could this be the problem?? Or could it be??. Thanks for your answers
*Do not use any library, read the Excel file through a connection OleDbConnection. My code is is the following:
//the error occurs here, but only published on the website:
strConex = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
/**strConex = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:copia.xlsx;
Extended Properties='Excel 12.0;HDR=YES;'"**/
OleDbConnection connection = new OleDbConnection(strConex);
OleDbCommand command = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
command.Connection = connection;
connection.Open();
DataTable dtschema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String nameSheet = dtschema.Rows[0]["TABLE_NAME"].ToString();
connection.Close();
connection.Open();
command.CommandText = "SELECT * From [" + nameSheet + "]";
adapter.SelectCommand = command;
adapter.Fill(dt);
connection.Close();
The result in the following exception:
System.NullReferenceException: Object reference not set to an instance of an object. at BusinessServices.RetentionAgentService.CreateRetentionsByAgentExcel(String path, String idcard, String user, DataTable dtError, DateTime initialDate, DateTime endDate, Guid& masterId) in C:\Corp\PublicAriManagua\BusinessServices\RetentionAgent\RetentionAgentService.cs:line 342 at PublicARI.Controllers.AgentRetentionController.SaveRetentions(HttpPostedFileBase file, RetentionAgentDeclaration retAgent) in C:\Corp\PublicAriManagua\PublicARI\Controllers\AgentRetentionController.cs:line 496 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)