If you are new to Entity Framework, then read my earlier blog – Introduction to Entity Framework
This blog will help you about How to insert data into sql using Entity Framework.
I have used Database first model approach.
HTML File:
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title></title>
<script src=”js/jquery.min.js”></script>
<script>
function SubmitData() {
var name = ‘Ram’;
var gender = ‘Male’;
var age = ’30’;
$.ajax({
type: “POST”,
url: “ajaxcall.aspx/SaveData”,
data: ‘{“name”:”‘ + name + ‘”, “gender”:”‘ + gender + ‘”, “age”:”‘ + age + ‘”}’,
contentType: “application/json; charset=utf-8”,
dataType: “json”,
beforeSend: function () {
$(‘#loader’).show();
},
success: function (data) {
alert(data.d);
$(‘#loader’).hide();
},
error: function (msg) {
//alert(‘3’);
msg = “There is an error”;
alert(msg);
$(‘#loader’).hide();
}
});
}
</script>
</head>
<body>
<div id=”loader” style=”display: none;“>
<img src=”ajax-loader.gif” />
</div>
<a href=”#” onclick=”SubmitData();”>Submit</a>
</body>
</html>
ASPX page
[WebMethod]
public static string SaveData(string name, string gender, string age)
{
try
{
var test = new Test();
test.age = Convert.ToInt16(age);
test.gender = gender;
test.name = name;
using (var ctx = new TestEntities())
{
ctx.Test.Add(test);
ctx.SaveChanges();
}
return “OK”;
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
}
}
Other Blogs on Entity framework-
Other Blogs on Entity framework-
- Introduction to Entity Framework
- Create login module using Entity framework
- Error- Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported
- How to Insert data to SQL using Entity framework
Keep following – SharePointCafe.Net – A technical blog on SharePoint, ASP.Net, Cloud Computing