使用asp.net mvc 2将数据插入数据库

时间:2013-11-09 05:29:30

标签: c# asp.net database asp.net-mvc-2

您好我正在尝试将数据插入到由用户填充的数据库中。我正在使用visual studio 2010 Asp.net mvc 2.但我收到错误

The resource cannot be found./Fillcustomer/customerdetail
。但服务器名称,用户ID和密码是正确的。请更正我的代码。

customer.cs


 namespace displaycustomer.Models
{
    public class customer
    {
        public int id { set; get; }
        public string customercode { set; get; }
        public int amount { set; get; }   

    public int Insert(int cid,string ccode, int amount)
    {

        string sqlconn = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        SqlConnection cn = new SqlConnection(sqlconn);
        cn.Open();           
        SqlCommand cmd = new SqlCommand("INSERT INTO tblcus(C_Id, C_Code, Amount) " +
            " VALUES('" + cid + "','" + ccode + "', '" + amount + "')", cn);
            return cmd.ExecuteNonQuery();           

    }
}

}

customerdetailcontroller.cs

    namespace displaycustomer.Controllers
{
    public class customerdetailController : Controller
    {
        //
        // GET: /customerdetail/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Fillcustomer()
        {
            return View();
        }

        [HttpPost]
        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("customerdetail", "Fillcustomer");
            }
            else
            {
                ModelState.AddModelError("", "Can Not Insert");
            }
            return View(cus);
        }

    }
}

fillcustomer.aspx

<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>

的web.config

<connectionStrings>
<add name="ConnString" connectionString="server=servername; Initial Catalog=sample; UID=user; pwd=123; Integrated Security=True;" providerName="System.Data.SqlClient" />

1 个答案:

答案 0 :(得分:0)

我猜问题出在连接字符串中。 我用过这种格式。可能这可以帮到你

<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" />

文件Sam.mdf位于App_Data文件夹中。

好了,现在你的网址不正确。

它应该是格式为/控制器名称/方法名称

应该是

 /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);
    }

RedirectToAction方法将第一个参数作为操作名称,将第二个参数作为控制器名称