Ad Unit (Iklan) BIG

simple insert data using store procedure in asp.net mvc

simple insert data using store procedure in asp.net mvc
in this article, i will show you how to insert data using stored procedure in c# asp.net mvc.

---table script---
create table Employee
(
   id int identity(1,1),
   name varchar(50),
   salary decimal(18,2)
)

---procedure script---
create proc insert_emp_rec
   @name varchar(50),
   @salary decimal(18,2)
as
    insert into Employee (name,salary) values(@name,@salary)

---model class---
public class Employee
{
    public int id { get; set; }
    public string name { get; set; }
    public decimal salary { get; set; }
}

---insert  method---
[HttpPost]
public void Create(Employee Emp)
{
   SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TESTDB;Integrated Security=True");
   string spname = "insert_emp_rec";
   SqlCommand cmd = new SqlCommand(spname, con);
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.AddWithValue("@name", Emp.name);
   cmd.Parameters.AddWithValue("@salary", Emp.salary);
   con.Open();
   cmd.ExecuteNonQuery();
   con.Close();
}

---cshtml view---
@model InsertRec.Models.Employee
@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{
<p>@Html.TextBoxFor(m => m.name, new {placeholder = "enter name"})</p>
<p>@Html.TextBoxFor(m => m.salary, new {placeholder = "enter salary"})</p>
<p><input type="submit" value="Create"/></p>
}

Post a Comment

0 Comments