C# 使用asp.net mvc 2将数据插入数据库
嗨,我正在尝试将数据插入到由用户填写的数据库中。我正在使用visual studio 2010 Asp.net mvc 2。但我犯了一个错误 The resource cannot be found./Fillcustomer/customerdetail } customerdetailcontroller.cs fillcustomer.aspxC# 使用asp.net mvc 2将数据插入数据库,c#,asp.net,database,asp.net-mvc-2,C#,Asp.net,Database,Asp.net Mvc 2,嗨,我正在尝试将数据插入到由用户填写的数据库中。我正在使用visual studio 2010 Asp.net mvc 2。但我犯了一个错误 The resource cannot be found./Fillcustomer/customerdetail } customerdetailcontroller.cs fillcustomer.aspx 客户Id 客户代码 数量 web.config 我想问题出在连接字符串上。 我使用了这种格式。也许这对你有帮助 <add name=
客户Id
客户代码
数量
web.config
我想问题出在连接字符串上。 我使用了这种格式。也许这对你有帮助
<add name="ProductEntities" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFileName=|DataDirectory|\Sam.mdf;Password=12345;User Id=ABC;Integrated Security=SSPI" />
不是
改变这个
public ActionResult insert()
{
customer cus = new customer();
int id = Convert.ToInt16(Request.Form["customerid"]);
string code = Request.Form["customercode"].ToString();
int amt = Convert.ToInt16(Request.Form["amount"]);
int _records = cus.Insert(id, code, amt);
if (_records > 0)
{
return RedirectToAction("Fillcustomer", "customerdetail");//change this
}
else
{
ModelState.AddModelError("", "Can Not Insert");
}
return View(cus);
}
RedirectToAction方法将第一个参数作为操作名称,将第二个参数作为控制器名称错误?您得到的是error,这意味着您与sql的连接出现错误。。conn字符串可能是错误的…如果您不使用sql参数,您的代码可能有一天会被sql注入:(我得到了我上面提到的新错误,请帮助我恢复
<form method="post" action="insert">
<table>
<tr>
<td>Customer Id</td>
<td><input type="text" name="customerid" /></td>
</tr>
<tr>
<td>Customer Code</td>
<td><input type="text" name="customercode" /></td>
</tr>
<tr>
<td>Amount</td>
<td><input type="text" name="amount" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>
</tr>
</table>
</form>
<connectionStrings>
<add name="ConnString" connectionString="server=servername; Initial Catalog=sample; UID=user; pwd=123; Integrated Security=True;" providerName="System.Data.SqlClient" />
<add name="ProductEntities" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFileName=|DataDirectory|\Sam.mdf;Password=12345;User Id=ABC;Integrated Security=SSPI" />
/customerdetail/FillCustomer
/Fillcustomer/customerdetail
public ActionResult insert()
{
customer cus = new customer();
int id = Convert.ToInt16(Request.Form["customerid"]);
string code = Request.Form["customercode"].ToString();
int amt = Convert.ToInt16(Request.Form["amount"]);
int _records = cus.Insert(id, code, amt);
if (_records > 0)
{
return RedirectToAction("Fillcustomer", "customerdetail");//change this
}
else
{
ModelState.AddModelError("", "Can Not Insert");
}
return View(cus);
}