Wednesday, 5 October 2016

How to insert Data using Entity Framework

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="">

    <script src="js/jquery.min.js"></script>
        function SubmitData() {

            var name = 'Ram';
            var gender = 'Male';
            var age = '30';
                type: "POST",
                url: "ajaxcall.aspx/SaveData",
                data: '{"name":"' + name + '", "gender":"' + gender + '", "age":"' + age + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function () {
                success: function (data) {
                error: function (msg) {
                    msg = "There is an error";

    <div id="loader" style="display: none;">
        <img src="ajax-loader.gif" />
    <a href="#" onclick="SubmitData();">Submit</a>


ASPX page

    public static string SaveData(string name, string gender, string age)
            var test = new Test();
            test.age = Convert.ToInt16(age);
            test.gender = gender;
   = name;

            using (var ctx = new TestEntities())

            return “OK”;

        catch (Exception ex)

            return ex.Message;



No comments:

Post a Comment

Dear Readers, Please post your valuable feedback in the comment section if you like this blog or if you have any suggestions. I would love to hear the same from you. Thanks