Click here to Skip to main content
13,145,068 members (54,084 online)
Rate this:
Please Sign up or sign in to vote.
See more:

i am new to JSON n jquery. i make a simple application for insert of data but it not work. i any have experiece in this please help.

currently data is not inserted to database


public class WebService : System.Web.Services.WebService {
    public WebService ()
    string conStr = WebConfigurationManager.ConnectionStrings["testconn"].ConnectionString;
    int rowsInserted = 0;
    public string InsertWebservice(int id, string name, string address)
        using (SqlConnection conn = new SqlConnection(conStr))
            string sql = string.Format(@"INSERT INTO [TestDB].[dbo].[emp] ([id],[name],[Address]) VALUES ('{0}','{1}','{2}')", id, name, address);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            rowsInserted = cmd.ExecuteNonQuery();
            cmd = null;
        return string.Format("Thank you, {0} number of rows inserted!", rowsInserted);

at aspx page
   <tr><td>  Id:</td><td><asp:TextBox ID="_id" runat="server"></asp:TextBox></td></tr>
    <tr><td>Name:</td><td><asp:TextBox ID="_txtname" runat="server"></asp:TextBox></td></tr>
    <tr><td>Address:</td><td><asp:TextBox ID="_addr" runat="server"></asp:TextBox></td></tr>
  <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="CallService();"
 <script type="text/javascript">
       function insert() {
       function CallService() {
           var id = $('#_id').val();
           var name = $('#_txtname').val();
           var addr = $('#_addr').val();
               type: "POST",
       url: "WebService.asmx/InsertWebservice",   
               contentType: "application/json;charset=utf-8",
               dataType: "json",                         
               //  data: "{ 'id': '" + id + "','name': '" + name + "', 'address':'" + addr + "'}",
               data: JSON.stringify({ id: 'id', name: 'name', address: 'address' }),
               async: false,
               success: function (data) {
Posted 10-Jan-13 1:37am
Updated 24-Feb-16 1:35am
ryanb31 10-Jan-13 8:04am
If you put breakpoints in your code, what IS happening?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here's what we need to know:

1 - does the success message get shown ?
2 - does the back end method get called ?
3 - is there a reason you need to use a webservice and not just an AJAX call ?
4 - have you debugged this in Chrome to see what happens when it runs ? Are there any javascript errors ?

I never use stringify, I just use an array of name/value pairs. Well, I've used stringify to serialise an entire form, that's about it. In fact, while I break it out, the line you commented out looks more like what I usually do. Here is an example of how I'd make an AJAX call:

    var postData = {
        urlIntegrated: urlIntegrated,
        id: id,
        password: $.trim($("#Password").val()),
        secretQuestion: $("#SecretQuestionId").val(),
        secretAnswer: $.trim($("#SecretAnswer").val())
    $.post("/Home/StoreProfile", postData, function (data) 
        if (data.Saved) 
            jAlert("Profile Saved", "Success", function () {
                window.location = "/";
            jAlert("Profile save failed, please try again or contact our support.", "Error");

    }, 'json');

I am posting to an ASP.NET MVC website. I am not sure if a web service will populate the values in the same way, but the way you wrote your method, is how it works fine for me in my site.

Just to add, building your SQL the way you are, anyone who wants to erase your DB, can. Read up on SQL injection attacks. You need to create paramaterized queries, or call stored procs.
Member 9013369 14-Aug-13 2:29am
How to insert data into sql server datatable using jquery
Christian Graus 14-Aug-13 20:09pm
You can't. jquery itself runs on the client. You can use it to make an AJAX call that does that, though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 24 Feb 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100