My Ajax data function has data but I can't figure out how to get that data out and insert it into a textbox with the ID of FirstName. I know the data is there because I can debug and see "d" contains all of the data from my query but how do I extract it from the success function?
$(document).ready(function () {
$("#btnGetData").click(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetFormData", //Default.aspx is page and GetFormData is the WebMethod
data: {},
dataType: "json",
success: function (data) {
data: { ("#FirstName").val(d.FirstName) }
},
error: function () {
alert("Error while Showing update data");
}
});
});
});
WebMethod:
public static List<MembersClass> GetFormData()
{
List<MembersClass> infoObjs = new List<MembersClass>();
try
{
// Initialization.
string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("dbo.spGetMemberbyMemberID", con);
cmd.Parameters.AddWithValue("@MemberID", "123");
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
// Read file.
while (rdr.Read())
{
MembersClass infoObj = new MembersClass();
infoObj.FirstName = rdr["first_name"].ToString();
infoObj.LastName = rdr["last_name"].ToString();
// Adding.
infoObjs.Add(infoObj);
}
}
}
catch (Exception ex)
{
Console.Write(ex);
}
// info.
return infoObjs;
}