如何解决ArgumentException [英] How to resolve ArgumentException
问题描述
大家好我正在做一个Asp.Net MVC应用程序。我是新手,因为我得到以下例外
初始化字符串的格式不符合从索引0开始的规范。
有人可以帮帮我。谢谢。
这是我在Controller中的代码: -
--------------------------------- -------
Hi all I am doing one Asp.Net MVC Application. I am very new to this in that I am getting the following Exception
Format of the initialization string does not conform to specification starting at index 0.
Can anybody please help me .Thanks in advance
This is my code inside Controller:-
----------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAppDataAccess.Models;
namespace MvcAppDataAccess.Controllers
{
public class EmployeeController : Controller
{
//
// GET: /Employee/
public ActionResult Details(int Id)
{
EmployeeContext empobj = new EmployeeContext();
Employee obj = empobj.Employees.Single(emp => emp.EmpId == Id);// This is the line of Error.
return View(obj);
}
}
}
这里面的模型
---- -----------------------
This Goes inside Model
---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
namespace MvcAppDataAccess.Models
{
[Table("Employee")]
public class Employee
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public string EmpJob { get; set; }
public double EmpSal { get; set; }
public int DeptId { get; set; }
}
}
这是我的上下文类
----------- ----------------
this is my Context Class
---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcAppDataAccess.Models
{
public class EmployeeContext:DbContext
{
public DbSet<Employee> Employees { get; set; }
}
}
这个是详情视图
- ----------------------------
this one is the details view
------------------------------
@model MvcAppDataAccess.Models.Employee
@{
ViewBag.Title = "Employee Details";
}
<h2>Employee Details</h2>
<table>
<tr>
<td>
Employee ID:-
</td>
<td>
@Model.EmpId
</td>
</tr>
<tr>
<td>
Employee Name:-
</td>
<td>
@Model.EmpName
</td>
</tr>
<tr>
<td>
Employee Job:-
</td>
<td>
@Model.EmpJob
</td>
</tr>
<tr>
<td>
Employee Salary
</td>
<td>
@Model.EmpSal
</td>
</tr>
<tr>
<td>
Department ID:-
</td>
<td>
@Model.DeptId
</td>
</tr>
</table>
以下是Web.Config文件的内容
-------------- -------------------------
Below is the Content of Web.Config File
---------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<connectionStrings>
<add name="EmployeeContext" connectionString="Server:MAVENSOFT\SQLEXPRESS; id:workflow;password:workflow;Database:Sample" providerName="System.Data.SqlClient" />
</connectionStrings>//this block of code for ConnectionString
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
推荐答案
这是一个错误的连接字符串问题。请检查你的web.config文件,看看connectionstring是否正确。
It's a problem of wrong connectionstring. Please check your web.config file see the connectionstring is correct or not.
检查以下链接:
初始化字符串错误MVC的格式
希望有所帮助:)
Check the below links:
format of the initialization string exception MVC
format of the initialization string error MVC
Hope it helps :)
这篇关于如何解决ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!