I m Student and I working with edit records using ajax.
my update functionality is working fine when I press the update button and then record(name, address) going to the text box and some changes of record(name, address change) and then press the update button then record update that is working fine
Problem is:
but when I did not press the second update button and page refresh then store the empty value in the database as well as a page that is my problem??
See Below Image:
Current I have 2 records in the database:

and then see my browser output:
when I press the update button:

problem is here when cc is going to the textbox and then I m refresh the URL then store the empty value in database and page also that is my problem?

sp:
ALTER PROCEDURE [dbo].[updatestud]
@studid int,
@studname varchar(50),
@studaddress varchar(50)
AS
BEGIN
update tblstud set studname=@studname,studaddress=@studaddress where studid=@studid;
ENDEmploee.aspx.cs
[WebMethod] //for update the data
public static string EditData(int studid, string studname, string studaddress)
{
cn.Open();
string data = "";
SqlCommand cmd = new SqlCommand("updatestud", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@studid", studid);
cmd.Parameters.AddWithValue("@studname", studname);
cmd.Parameters.AddWithValue("@studaddress", studaddress);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Console.WriteLine("Update Successfully");
}
else
{
Console.WriteLine("Not Update Successfully");
}
cn.Close();
data = "true";
return data;
}Emploee.aspx
<tr><td><input type="button" id="btnsave" value="Submit" onclick="saverecord()" /></td><asp:HiddenField ID="HiddenField1" runat="server" Value="" /></tr><div class="row"><div><table id="tbl" border="1"><thead><tr><th>StudentName</th><th>StudentAddress</th><th>Edit</th></tr></thead></table><span id="result"></span></div></div><script type="text/javascript">$(document).ready(function () {
GetData();
});
//for insert data
function saverecord() {
studid = $("#HiddenField1").val();
if ($("#btnsave").val() == "Submit") {$.ajax({
url: 'Emploee.aspx/insert',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{name:'" + $("#txtname").val() + "',address:'" + $("#txtaddress").val() + "'}",
success: function (data) {
if (data.d == 'true') {
window.location.reload();
}
},
error: function () {
alert('Insert Error');
},
});
}
else {
$.ajax({
url: 'Emploee.aspx/EditData',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
aync: true,
data: "{studid: '" + studid + "',studname:'" + $("#txtname").val() + "',studaddress:'" + $("#txtaddress").val() + "'}",
success: function (data) {
window.location.reload();
}
},
error: function () {
alert('Update Error');
},
});
}
}
//when I press update button record going to the textbox
function Update(studid, studname, studaddress) {
$.ajax({
url: 'Emploee.aspx/EditData',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
data: "{studid: '" + studid + "',studname:'" + $("#txtname").val() + "',studaddress:'" + $("#txtaddress").val() + "'}",
success: function (data) {
data = JSON.parse(data.d);
$("#txtname").val(studname);$("#txtaddress").val(studaddress);$("#HiddenField1").val(studid);
//if id is available then run the function saverecord() else condition
if (studid != null) { $("#btnsave").val("Update");
}
},
error: function () {
alert('Update Error');
},
});
}
//for getdatalist
function GetData() {
$.ajax({
url: 'Emploee.aspx/GetData',
type: 'post',
contentType: 'application/json;charset=utf-8',
datatype: 'json',
success: function (data) {
data = JSON.parse(data.d);
$.each(data, function (index, element) {$("#tbl").append('<td>' + element.studname + '</td>');$("#tbl").append('<td>' + element.studaddress + '</td>');$("#tbl").append('<td><input type="button" id="btnupdate" value="Update" onclick="Update(' + element.studid + ', \'' + element.studname + '\' ,\'' + element.studaddress + '\')" /></td>');
});
},
error: function (error) {
alert('Not Get Data')
},
});
}</script></form>what logic should be remaining above program my mind is not working??
an update is working fine but the issue is when I press update then record(name, address) going to textbox and I refresh the page then store the empty value in the database and current page also?what should do I here? which logic is missing above the program?
help?