Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my Controller to post data into database:-

C#
[HttpPost]
        public ActionResult UpdateGLCode(string Glcode, string ReportId, string HeadName)
        {
            SqlConnection con = null;
            SqlCommand cmd = null;

            con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["System.Data.SqlClient"].ToString());
            con.Open();
            cmd = new SqlCommand("AliUpdateGLCodeInRLMaster", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Criteria1", SqlDbType.VarChar).Value = Glcode;
            cmd.Parameters.Add("@Criteria2", SqlDbType.VarChar).Value = ReportId;
            cmd.Parameters.Add("@Criteria3", SqlDbType.VarChar).Value = HeadName;

            cmd.ExecuteNonQuery();
            con.Close();
            return RedirectToAction("Index", new { msg = "Record Saved Successfully." });
        }


and below is my Ajax:

HTML
$("#btnRLMasterSave").click(function () {
        alert("HI");
        $.ajax({
            url: "/RLMaster/UpdateGLCode",
            data: { Glcode: $('#Glcode').val(), ReportId: $('#ReportId').val(), HeadName: $('#HeadName').val() },
            type: "POST",
            dataType: "json",
            success: function (data) {
                alert("HI");
            },
            error: function () {
                alert("Failed");
            }
        });
    });


I am very sure that I am not correct.
But this is all I can write of my own.
Please correct or any suggestions would be highly appreciated.
Posted
Updated 1-Sep-15 3:27am
v6

1 solution

Javascript code:
1. Add jQuery reference in your view page.
JavaScript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>


2. Add below javascript code.
JavaScript
<script>
   $(function () {
        $("#btnRLMasterSave").click(function () {
	alert("HI");
	$.ajax({
		url: "/RLMaster/UpdateGLCode",
		data: JSON.stringify({ Glcode: $('#Glcode').val(), ReportId: $('#ReportId').val(), HeadName: $('#HeadName').val() }),
		type: "POST",
		contentType: 'application/json; charset=utf-8',
		dataType: "json",
		success: function (data) {
			alert(data);
		},
		error: function () {
			alert("Failed");
		}
	});
     });
});
</script>


Controller Code:
C#
[HttpPost]
public ActionResult UpdateGLCode(string Glcode, string ReportId, string HeadName)
{
	SqlConnection con = null;
	SqlCommand cmd = null;

	con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["System.Data.SqlClient"].ToString());
	con.Open();
	cmd = new SqlCommand("AliUpdateGLCodeInRLMaster", con);
	cmd.CommandType = CommandType.StoredProcedure;
	cmd.Parameters.Add("@Criteria1", SqlDbType.VarChar).Value = Glcode;
	cmd.Parameters.Add("@Criteria2", SqlDbType.VarChar).Value = ReportId;
	cmd.Parameters.Add("@Criteria3", SqlDbType.VarChar).Value = HeadName;

	cmd.ExecuteNonQuery();
	con.Close();
	
	return Json("Success", JsonRequestBehavior.AllowGet);
}
 
Share this answer
 
v2
Comments
Member 11579819 2-Sep-15 0:59am    
ain't working ManasKumarM
[no name] 2-Sep-15 1:42am    
What is the error you are getting or where you get stuck?
Member 11579819 2-Sep-15 2:25am    
It does not post data to database. However,when I had a debug,it has posted the data.
[no name] 2-Sep-15 2:26am    
Are you getting all data(you are getting AJAX data) within your action. If yes then please check your stored procedure and database operation details like connection string etc..

Secondly are you getting any kind of exception.
Member 11579819 2-Sep-15 2:35am    
Everything else works fine there.But still I can't figure out why it is able to post data only when I run the app in debug mode.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900