Pages

Saturday, 24 February 2018

Record Update ajax MVC


Field
Data Type
Roll
Bigint
Name
Varchar(20)
City
Varchar(20
Table Structure 1



Store Procedure

USE [Girfa_Studb]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[UpdateProduct]
(
@roll bigint,
@name varchar(100),
@city varchar(100)

)

AS

BEGIN
       SET NOCOUNT ON;

              update dbo.Student set Name=@name,City=@city where Roll=@roll

       SET NOCOUNT OFF;
END


CSHTML



@{
ViewBag.Title = "tmp";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Save Data</h2>
<form>
    Roll : <inputtype="text"id="txtRoll"/><br/>
    Name : <inputtype="text"id="txtName"/><br/>
    City : <inputtype="text"id="txtCity"/><br/>
<inputtype="button"value="Save"onclick="UpdateStu()"/>
</form>

Model


publicclassStudentModel
{
publicint Roll { getset; }
publicstring Name { getset; }

publicstring City { getset; }
}

JS

function UpdateStu() {
 
 
    

        var Userobj =
            {
                Roll: $j("#txtRoll").val(),
                Name: $j("#txtName").val(),
                City: $j("#txtCity").val(),
              
            }

        $.ajax({
            url: '/Master/UpdateStuData/',
            dataType: "json",
            type: "POST",
            async: false,
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(Userobj),
            success: function (data) {
            
                    if (data.sMessage == "1") {
                        alert("Record Updated successfully");
                    }
              
                else {
                        alert(data.sMessage);
                }
               

            }
        });
   
}

Controller

public JsonResult UpdateStuData(StudentModel data)
{
    string ErrMsg = "";

    StudentRepository ob = new StudentRepository();

    if (ob.UpdateStuData(data, ref ErrMsg) == true)
    {
        return Json(new { sMessage = "1", JsonRequestBehavior.AllowGet });
    }
    else
    {
        return Json(new { sMessage = ErrMsg, JsonRequestBehavior.AllowGet });
    }
}

Repository

  public bool UpdateStuData(StudentModel ob,ref string ErrMsg)
        {
            String result = "";
            try
            {
                using (SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString()))
                {
                    using (SqlCommand cmd = new SqlCommand("[dbo].[UpdateStu]", dbcon))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;

                        cmd.Parameters.AddWithValue("@roll",Convert.ToInt16(ob.Roll));
                        cmd.Parameters.AddWithValue("@name", ob.productname);
                        cmd.Parameters.AddWithValue("@city", ob.city);
                       

                        if (dbcon.State == ConnectionState.Closed)
                            dbcon.Open();
                        cmd.ExecuteNonQuery();
                        result = "1";
                    }
                }
            }
            catch (Exception ex)
            {
                ErrMsg = ex.Message;
            }
            if (result == "1")
                return true;
            else
                return false;
        }

No comments:

Post a Comment