JSON is used to make a key value or collection pair of values while performing transfer of data. So i am inserting values into database using ajax call in which i first have created a user class for defining entities
.Example:
public int id;
public string Username;
public string Password;
public string Email;
public int PrefernceID;
public string Contact;
public string Address;
public string status;
public string errormsg;
public string Preference;
public string acctype;
This is my User class and its associated entities.
After doing that i am making an AJAX call from the registration page into a method called Insert Details. For parsing values into JSON i have used JSON.stringify(user) and passed a user array object into it, see the example below:
function InsertDetails() {
var user = {};
user.Username = $("[id*=txtUserName]").val();
user.Password = $("[id*=txtPassword]").val();
user.Email = $("[id*=txtEmail]").val();
user.Contact = $("[id*=txtContact]").val();
user.Address = $("[id*=txtAddress]").val();
user.Preference = $("[id*=txtPreference]").val();
$.ajax({
type: "POST",
url: "RegisterUser.aspx/InsertDetails",
data: '{user: ' + JSON.stringify(user) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("User has been added successfully.");
window.location.reload();
}
});
return false;
}
After doing this i will collect the values passed from user object and perform insertion operations into my Insert Details method, see the below example:
[WebMethod]
[ScriptMethod]
public static void InsertDetails(User user)
{
try
{
UserOperation repobj = new UserOperation();
user = repobj.GetPrefernceId(user);
user = repobj.InsertData(user);
}
catch(Exception ex)
{
}
}
catch (Exception ex)
{
throw ex;
}
}
Here in above code i have used Script Method which is used to receive object coming from user .
0 Comment(s)