Here i am getting error on calling data to edit controls from database using ajax json
No specific error message is shown. when i click edit control i am getting pop up error as undefined.
i.e.... localhost:23343 says: 'undefined'
[System.Web.Services.WebMethod]
public Employee[] getemployee()
{
List<Employee> emp = new List<Employee>();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con11"].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from employee", con);
DataTable dt = new DataTable();
SqlDataAdapter ada = new SqlDataAdapter();
ada.SelectCommand = cmd;
cmd.Connection = con;
ada.Fill(dt);
foreach (DataRow dtrow in dt.Rows)
{
Employee em = new Employee();
em.id = Convert.ToInt32(dtrow["id"].ToString());
em.name = dtrow["name"].ToString();
em.email = dtrow["email"].ToString();
em.address = dtrow["address"].ToString();
emp.Add(em);
}
return emp.ToArray();
}
[System.Web.Services.WebMethod]
public Employee[] edit(string id)
{
// string message;
List<Employee> emp = new List<Employee>();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con11"].ConnectionString);
SqlCommand cmd = new SqlCommand("select id,name,address from employee where id=@id", con);
cmd.Parameters.AddWithValue("@id", id);
SqlDataAdapter ada = new SqlDataAdapter();
DataTable dt = new DataTable();
ada.SelectCommand = cmd;
cmd.Connection = con;
ada.Fill(dt);
foreach (DataRow dtrow in dt.Rows)
{
Employee em = new Employee();
em.id = Convert.ToInt32(dtrow["id"].ToString());
em.name = dtrow["name"].ToString();
em.email = dtrow["email"].ToString();
em.address = dtrow["address"].ToString();
emp.Add(em);
}
return emp.ToArray();
}
$(document).ready(function () {
getdata();
});
function getdata(){
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/WebService.asmx/getemployee",
data: {},
dataType: "json",
//traditional: true,
async: false,
success: function (data) {
var table=$("#table1");
var rows = "";
for (var i = 0; i < data.d.length; i++) {
rows += "<tr><td>" + data.d[i].name+"</td>"
+ "<td>" + data.d[i].email + "</td>"
+ "<td>" + data.d[i].address + "</td>"
// + "<td>" + '<img class=width=100px height=100px src="data:image/jpg;base64,' + data.d[i].byteImage + '"/>' + "</td>"
+ "<td>" + '<a href=# id=edit1 onclick=edit("' + data.d[i].id + '")>edit</a>' + "</td>"
+"</tr>"
}
table.append(rows);
},
error: function ()
{
alert("error");
}
})
}
function edit(id) {
// var id = 177;
//alert(id);
$.ajax({
type: "post",
contentType: "application/json; charset=utf-8",
url: "/WebService.asmx/edit",
data: '{id:"'+id+'"}',
dataType: "json",
//traditional: true,
// async: false,
success: function (data) {
$("#name1").val(data.d.name);
$("#address1").val(data.d.address);
$("#email1").val(data.d.email);
},
error: function () {
alert("error");
}
})
}