aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Ajax JSON Example in

Dec 10, 2013

Here I will explain simple jQuery ajax json example in or submit a form without page refresh using jQuery ajax json with example in using c#,

In previous articles I explained
integrate Facebook login authentication, integrate twitter login authentication, jQuery randomly change color of div and many articles relating to jQuery,, c#, Now I will explain simple jQuery ajax json example in
To implement this first design table in your database like below to save values in database.

Column Name
Data Type
Allow Nulls
Once table creation completed then write code like as shown below in your aspx page

<html xmlns="">
<head id="Head1">
<title>jQuery Submit a form without postback</title>
<script type="text/javascript" src=""></script>
<form id="form1" runat="server">
<td><input type="text" id="txtname" /></td>
<td> <input type="text" id="txtsubject" /></td>
<td> <textarea id="txtbody"></textarea></td>
<input type="button" id="btnSubmit" value="Submit" />
<label id="lblmsg" style="font-weight:bold; color:Red"/>
<script type="text/javascript">
$(function () {
$('#btnSubmit').click(function () {
var name = $('#txtname').val();
var subject = $('#txtsubject').val();
var body = $('#txtbody').val();
if (name != '' && subject != '' && body) {
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/InsertData",
data: "{'username':'" + name + "','subj':'" + subject + "','desc':'" + body + "'}",
dataType: "json",
success: function (data) {
var obj = data.d;
if (obj == 'true') {
$('#lblmsg').html("Details Submitted Successfully");
error: function (result) {
else {
alert('Please enter all the fields')
return false;
After completion of aspx page design add the following namespaces in code behind

C# Code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
After that write the following code in code behind

protected void Page_Load(object sender, EventArgs e)
public static string InsertData(string username, string subj, string desc)
string msg = string.Empty;
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
using (SqlCommand cmd = new SqlCommand("insert into TEMP_User(Name,Subject,Description) VALUES(@name,@subject,@desc)", con))
cmd.Parameters.AddWithValue("@name", username);
cmd.Parameters.AddWithValue("@subject", subj);
cmd.Parameters.AddWithValue("@desc", desc);
int i= cmd.ExecuteNonQuery();
if (i == 1)
msg = "true";
msg = "false";
return msg;

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services

Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)

End Sub
<WebMethod()> _
Public Shared Function InsertData(username As String, subj As String, desc As String) As String
Dim msg As String = String.Empty
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
Using cmd As New SqlCommand("insert into TEMP_User(Name,Subject,Description) VALUES(@name,@subject,@desc)", con)
cmd.Parameters.AddWithValue("@name", username)
cmd.Parameters.AddWithValue("@subject", subj)
cmd.Parameters.AddWithValue("@desc", desc)
Dim i As Integer = cmd.ExecuteNonQuery()
If i = 1 Then
msg = "true"
msg = "false"
End If
End Using
End Using
Return msg
End Function
End Class

Download sample code attached 

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...


phani said...

good.... nice example

Anonymous said...

Hmm Wonderful , this method is very useful for me.

Anonymous said...

Nice Article....

Free hosting PHP MY SQL HTML for Developers and students

NO Add's Completely Free

Create Account and Hoat Your Website..

Anonymous said...

You should sanitize the input

Suresh Sundar said...

simple example :)

Ravi Shanker said...

Great Example for a fresher like me.

Shahabuddin shahab said...

Sir Please Post any example for change language in

Anonymous said...


Anonymous said...

nice one

avnish kumar singh said...

sir plz can u tell me how to update and delete record with jason plz provide me any link or code plz

Thanks in advance

Sachin said...

Explained with Simplicity....Very nice example

Vutla said...

Good Example

Anonymous said...

Really its an very good demo

Give your Valuable Comments

Other Related Posts

© 2010-2012 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.