Click Here For MCQ

Thursday, June 25, 2020

Insert Data with the help of DapperORM with store procedure in ASP.Net MVC C#

install dapper orm version 1.42 nuget pakage only with mvc it is must 


you can also perform entityframe work with dapperorm it is tested


public class DapperOrm
    {
        private static string connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB; Initial Catalog=mystore; Integrated Security=True;";

      public static void Executewithoutreturn(string procedureName ,DynamicParameters param =null)
        {
            using (SqlConnection sqlcon =new SqlConnection(connectionstring))
            {
                sqlcon.Open();
                sqlcon.Execute(procedureName, param, commandType: CommandType.StoredProcedure);

            }
        }
        //DapperOrm.ExecutewithreturnScaler<int>("","");
        public static T ExecutewithreturnScaler<T>(string procedureName, DynamicParameters param=null)
        {
            using (SqlConnection sqlcon = new SqlConnection(connectionstring))
            {
                sqlcon.Open();
                return (T)Convert.ChangeType(sqlcon.Execute(procedureName, param, commandType: CommandType.StoredProcedure),typeof(T));

            }
        }
        ///DapperOrm.ReturnList<Employeemodel>("","");
        public static IEnumerable<T> ReturnList<T>(string procedureName, DynamicParameters param =null)
        {
            using (SqlConnection sqlcon = new SqlConnection(connectionstring))
            {
                sqlcon.Open();
                return sqlcon.Query<T>(procedureName, param, commandType: CommandType.StoredProcedure);

            }
        }
    }
}



mypracticemodel


public class mypracticemodel
    {
        public int id { get; set; }
        public string firstname { get; set; }
        public string sirname { get; set; }
    }



controller



 public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]

        public ActionResult Index(mypracticemodel emp)
        {
            DynamicParameters param = new DynamicParameters();
            param.Add("@id", emp.id);
            param.Add("@firstname", emp.firstname);
            param.Add("@sirname", emp.sirname);
            DapperOrm.Executewithoutreturn("sp_newinsert", param);

            return View();
        }

        public ActionResult viewlist()
        {
            return View(DapperOrm.ReturnList<Employeemodel>("sp_selectlisttable"));
        }



view 


@model DapperProj.Models.mypracticemodel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>mypracticemodel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.firstname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.firstname, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.firstname, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.sirname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.sirname, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.sirname, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>




database
store procedure


USE [mystore]
GO
/****** Object:  StoredProcedure [dbo].[sp_newinsert]    Script Date: 6/25/2020 9:44:04 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_newinsert]
@id int,
@firstname nvarchar(50),
@sirname nvarchar(50)
as
insert into mypractice (firstname,sirname)
values (@firstname,@sirname)

No comments:

Post a Comment