I have a simple code to update the database with simple Ajax call using WebMethod but its not updating in the database.
HTML Markup
<a href="#" class="newProject" id ="<%= item2 %>"><i class="fa fa-file"></i> <%= item2 %></a>
Client Side Method:
<script type="text/javascript">
$(".newProject").on("click", function () {
$.ajax({
type: "POST",
url: "index.aspx/UpdateCode",
data: 'engCode=' + this.id,
success: function (response) {
// window.location.reload();
alert(response);
}
});
return false;
});
</script>
Server side method:
[WebMethod]
public static int UpdateCode(string Code)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Codes"))
{
int intresult = 0;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Mode", SqlDbType.VarChar, 50).Value = "UpdateCodes";
cmd.Parameters.Add("@Code", SqlDbType.VarChar, 50).Value = Code;
cmd.Connection = con;
try
{
con.Open();
intresult = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
ex.ToString();
}
finally
{
cmd.Dispose();
if (con != null)
{
con.Close();
}
}
return intresult;
}
}
}
Its posting the right value on click event of hyperlink but not updating the database.