locked
JSON data not inserting in sql RRS feed

  • Question

  • User219039814 posted

    Connection con = new Connection();
    SqlConnection conn = new SqlConnection();

    SqlCommand cmd = null;

    conn = con.getConnection();

    // query = "INSERT INTO dummy1 VALUES(@Name, @Course)";
    //cmd.CommandType = CommandType.Text;
    //cmd.Parameters.AddWithValue("@Name", c.Name);
    //cmd.Parameters.AddWithValue("@Course", c.Course);
    query = "insert into dummy1 values('XYX','ABC')";     // I am trying to insert static data.. becoz the above insert failed

    cmd = new SqlCommand(query, conn);
    cmd.ExecuteNonQuery();
    conn.Close();

    My web method

    function AddData()
    {
    var employee = {};
    employee.Name= "ABCDE";
    employee.Course="BTECH";


    $.ajax({
    type: 'POST',
    url: "dummy.aspx/TestOnWebService",

    data: '{employee: ' + JSON.stringify(employee) + '}',
    contentType: "application/json; charset=utf-8",

    datatype: "json",
    success: function (response) {

    alert('Data added Successfully');

    },
    error: function (response) {
    alert(employee.Name);    // printing ABCDE
    alert('not Added');
    }
    });
    alert("inside the add data");  //printing
    }

    Can anyone tell me what is going wrong?

    I keep getting "not added" message

    Saturday, July 7, 2018 8:56 AM

Answers

  • User753101303 posted

    Hi,

    So have a look at http://api.jquery.com/jQuery.ajax/ to see how to better use the error function to understand first what happens exactly. My guess is that you have a server side connection related to how your db connection is handled (you may find error details in the Windows event log).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 7, 2018 9:07 AM
  • User-369506445 posted

    hi

    please put a breakpoint in TestOnWebService method and sure you catch it with ajax

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 7, 2018 9:44 AM
  • User-369506445 posted

    please go to App_start folder and find RouteConfig class then change

    settings.AutoRedirectMode = RedirectMode.Permanent;

    to

    settings.AutoRedirectMode = RedirectMode.Off;

    compelet code

    public static class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                var settings = new FriendlyUrlSettings();
                settings.AutoRedirectMode = RedirectMode.Off;
                routes.EnableFriendlyUrls(settings);
            }
        }

    and your dummy.aspx

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
        <script>
            function AddData() {
                var employee = {};
                employee.Name = "ABCDE";
                employee.Course = "BTECH";
                $.ajax({
                    type: 'POST',
                    url: "dummy.aspx/TestOnWebService",
                    data: '{employee: ' + JSON.stringify(employee) + '}',
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    success: function (response) {
                        alert('Data added Successfully');
                    },
                    error: function (response) {
                        alert(employee.Name);    // printing ABCDE
                        alert('not Added');
                    }
                });
                alert("inside the add data");  //printing
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <input type="button" onclick="AddData();" value="send" />
    
        </form>
    </body>
    </html>

    and code behind

     protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            [System.Web.Services.WebMethod]
            public static string TestOnWebService(check employee)
            {
                //here put your code
                return "test";
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 7, 2018 10:07 AM
  • User-1171043462 posted

    Please share your WebMethod and please paste Code properly using Code Button in editor.

    Your method may not be getting called due to incorrect signatures.

    Refer my example: Insert (Update) Data to Database with jQuery AJAX and WebMethod in ...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 7, 2018 11:14 AM

All replies

  • User219039814 posted

    sorry,

    My cs file

    public class check
    {

    public String Name { get; set; }

    public String Course { get; set; }

    }

    Saturday, July 7, 2018 8:57 AM
  • User753101303 posted

    Hi,

    So have a look at http://api.jquery.com/jQuery.ajax/ to see how to better use the error function to understand first what happens exactly. My guess is that you have a server side connection related to how your db connection is handled (you may find error details in the Windows event log).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 7, 2018 9:07 AM
  • User219039814 posted

    I checked everything sir,

    but in still the insert query is not working.

    How can i check my insert query

    Saturday, July 7, 2018 9:33 AM
  • User-369506445 posted

    hi

    please put a breakpoint in TestOnWebService method and sure you catch it with ajax

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 7, 2018 9:44 AM
  • User219039814 posted

    sir, 

    i put a breakpoint.. but i am not able to catch it..

    can you help me please

    Saturday, July 7, 2018 9:51 AM
  • User-369506445 posted

    please go to App_start folder and find RouteConfig class then change

    settings.AutoRedirectMode = RedirectMode.Permanent;

    to

    settings.AutoRedirectMode = RedirectMode.Off;

    compelet code

    public static class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                var settings = new FriendlyUrlSettings();
                settings.AutoRedirectMode = RedirectMode.Off;
                routes.EnableFriendlyUrls(settings);
            }
        }

    and your dummy.aspx

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
        <script>
            function AddData() {
                var employee = {};
                employee.Name = "ABCDE";
                employee.Course = "BTECH";
                $.ajax({
                    type: 'POST',
                    url: "dummy.aspx/TestOnWebService",
                    data: '{employee: ' + JSON.stringify(employee) + '}',
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    success: function (response) {
                        alert('Data added Successfully');
                    },
                    error: function (response) {
                        alert(employee.Name);    // printing ABCDE
                        alert('not Added');
                    }
                });
                alert("inside the add data");  //printing
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <input type="button" onclick="AddData();" value="send" />
    
        </form>
    </body>
    </html>

    and code behind

     protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            [System.Web.Services.WebMethod]
            public static string TestOnWebService(check employee)
            {
                //here put your code
                return "test";
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 7, 2018 10:07 AM
  • User-1171043462 posted

    Please share your WebMethod and please paste Code properly using Code Button in editor.

    Your method may not be getting called due to incorrect signatures.

    Refer my example: Insert (Update) Data to Database with jQuery AJAX and WebMethod in ...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 7, 2018 11:14 AM
  • User219039814 posted
    I figured it out.. the signatures mismatched..
    Thank u for all ur patience
    Saturday, July 7, 2018 12:33 PM