jQuery Asp.net ajax JSON Example, C# WebMethod with sql database


Overview:  Here in this article will see asp.net Ajax JSON example, i.e. calling jQuery ajax WebMethod and get JSON format result (JSON data) in Asp.net C# Webform. Simple jQuery Ajax JSON example in Asp.net c# with MS SQL Server database.

If you are looking for how to get JSON formatted result from jquery ajax call in asp.net C#, then this post will explain step by step process. You can also check 3 way to convert DataTable into JSON String in Asp.net C#.

Am assuming you are aware of Asp.net C# Webservice, i.e., (ASMX file) Webservice.ASMX, WebMethods and familiar with jQuery Ajax syntax.

And it’s very useful if you want to save or insert data into database without page postback or page refresh .i.e Insert data into database using jQuery Ajax

In this tutorial, we make a jQuery Ajax call and in response will get the JSON data, i.e., here we get a list of all cars and other detail information from our database ( MS SQL server) via WebMethod (ASMX file).

Steps to create jQuery Ajax JSON example in Asp.net C#.

  1. Download jQuery library and add Html Markup.
  2. Calling jQuery Ajax method.
  3. C#: Create Class and WebMethod, which returns JSON Object.

Fig 1:

Asp.net c# jQuery Ajax Json example with MS SQL sever database

# Download the jQuery library and add Html Markup.

First, we open Visual Studio IDE, create a new project and then we added new Asp.net WebPage and named as myJsonTuts.aspx. On our Asp.net WebPage, we add a button control and import jQuery library.

You can download jQuery files from jQuery.com, or you can use Google hosted files. So now our HTML markup looks like as shown below.

# Client-Side: Calling jQuery Ajax method.

Now we write client-side code .i.e.  on button click we make a jQuery Ajax call with returns JSON data, check this for dynamic button click event in jQuery.

Here in our case, we fetch the list of cars along with other information from the database MS SQL server via C# WebMethod, which we create later on server-side.

Let’s first check how our Client side  code looks as shown below

Here in above code on our button click we make an ajax call.

You may notice we have set dataType=”json” that is because we want our ajax response in JSON format.

# Creating a class and WebMethod, which returns list collection as JSON data.

Now we add a Webservice file (ASMX file) in our project, here we added a file and named as myfunction.asmx.  In our Webservice (.asmx file) we also created a class .i.e Cars which has property as carName,carRating, carYear.

Now we write a WebMethod as we want to get the list of all cars,  here getListofCars is our WebMethod, which fire select query to the database, pull the record from the database and add it to our class object Car.

In response, we return this Car class object.

Now we write our WebMethod, which returns List of Cars details.

 In response, we will get data in JSON format,  check this in your browser console, JSON format list of cars is available as shown in Fig 1.

Note: Always use response.d to get the jquery ajax response in Asp.net C#.

Fig 1:

Asp.net c# jQuery Ajax Json example with MS SQL sever database

Now we write some more code on our client-side .i.e how to display JSON data list of the car into our Webpage.

In short, we have to write some jQuery code to handle the response JSON Data. So now our success function looks like as shown below.

Here we stored the response in variable item and then  using jQuery $.each() method we make a loop over data, get and set value to ‘<li>’ tag and append to parent <UL>

# OutPut: Asp.net ajax JSON example:

Finally, our Output which displays the list of car and other information.

Output of jQuery Ajax JSON Example in Asp.net C# with MS SQL server database

Conclusion:  In this article, we learn how to make a jquery ajax call in Asp.net C# .ie using WebMethod ( .asmx file) and returns JSON format data. Also, we display this JSON data into our HTML .i.e onSuccess method we create a List of  Cars with other information ( Li tag) using JSON data.

You must also check these articles:

Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!


  1. I need some help, please!
    I do not understand something..
    HTML markup and jQuery Ajax method should be in the same file: Default.aspx
    Defining a class Cars and [WebMethod] sholud be in the same file: myfunction.asmx (or maybe myfunction.asmx.cs?!)
    And then, the “function OnSuccess(response)” should be where?

    I’m sorry, I know it is a stupid question, but I’m very new to this and I really need some help

    • Hi EvaTomlison,
      Here we use two files .i.e (Default.aspx and myFunction.asmx)

      1. Default.aspx : Here we put out HTML markup and jquery ajax code with success and error method.
      2. myFunction.asmx.cs : Here we write the WebMethod and the Class.

      Hope now you got a clear idea about using jquery ajax with JSON data in asp.net

  2. need help ,how write a jquery function to display the name list from a customer database table as a help list as soon as the textbox get focus in the web form (asp.net with c#) , and second thing is the selected data from the help list should be displayed on the textbox also

  3. Hi,

    I tried creating a new database with database fields, carname, carrating, and caryear. But I cant seem to query the fields in the codes above. May in know more about your connection string to query from the database and also your stored procedure, ‘spGetCars’.


    • Though it’s basic, but sure will update this in the post.
      Ok first you need to add your connection string into your Web.config

      <add name="constr"
        connectionString= "server=sys009;database=myDatabaseName;uid=sa;pwd=myPassword" />

      Secondly create a connection into your webpage so you can use in your Webmethod/functions

      SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["constr"].ToString());

      And about query it’s simple SELECT * FROM CAR
      so Storedprocedure would be like

      CREATE PROC spGetCars(
      	@makeYear int
      BEGIN AS
      SELECT * FROM CAR WHERE [email protected]
  4. Hi, i’m having this error:
    Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user’s experience. For more help, check http://xhr.spec.whatwg.org/.
    browserLink:37 Setting ‘XMLHttpRequest.withCredentials’ for synchronous requests is deprecated.

    what am i doing wrong?.


    • To avoid this warning, do not use `async: true` in any of your `$.ajax()` calls. This is the only feature of XMLHttpRequest that’s deprecated.
      You can resolve this by adding `async: true,` in your ‘$.ajax()` call