Introduction
This is a simple CRUD operation using Ajax or without loading the page and binding the data in HTML Table using Ajax.
Description
To implement this first we need to create a table in our database. The table structure I have used is like the following:

The SQL query for creating the table is as follows:
CREATE TABLE [dbo].[TblUser](
[EmpId] [int] IDENTITY(1,1) NOT NULL,
[Fname] [nvarchar](30) NOT NULL,
[Mname] [nvarchar](30) NULL,
[Lname] [nvarchar](30) NULL,
[Gender] [nchar](10) NOT NULL,
[EMail] [nvarchar](50) NOT NULL,
[DOB] [nvarchar](30) NOT NULL,
[MaritalStatus] [nvarchar](30) NOT NULL,
[Hobbies] [nvarchar](30) NULL,
[Telephone] [nvarchar](30) NULL,
[Mobile] [nvarchar](30) NULL,
[Address] [nvarchar](300) NOT NULL,
[PinCode] [nvarchar](30) NOT NULL,
[State] [nvarchar](30) NOT NULL,
[Nationality] [nvarchar](30) NOT NULL,
[DOJ] [nvarchar](30) NULL,
[CreatedDate] [datetime] NOT NULL,
[ModifiedDate] [datetime] NOT NULL,
CONSTRAINT [PK_TblUser] PRIMARY KEY CLUSTERED
(
[EmpId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Design
Here I have used HTML controls instead of ASP.NET Server controls and also used Bootstrap Template for better view.
The main reason I prefer HTML controls instead of ASP.NET server controls is that they are lightweight and validation can be managed by client side. But still there are a lot of advantageS of server controls (eg. Maintaining view state, events and many more.).
<div class="col-lg-8">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-bar-chart-o"></i>Employee Details </h3>
</div>
<div class="panel-body">
<div class="form-group col-lg-4">
<label>First Name</label>
<input type="text" name="FirstName" id="FirstName" class="form-control" placeholder="First Name" required="" />
</div>
<div class="form-group col-lg-4">
<label>Middle Name</label>
<input type="text" name="MiddleName" id="MiddleName" class="form-control" placeholder="Kumar" required="" />
</div>
<div class="form-group col-lg-4 ">
<label>Surname</label>
<input type="text" name="Surname" id="Surname" class="form-control" placeholder="Gupta" required="" />
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-6">
<label>Email ID</label>
<input type="email" name="EmailId" id="EmailId" class="form-control" placeholder="[email protected]" required="" />
</div>
<div class="form-group col-lg-6">
<label>Date of Birth </label>
<input type="date" name="Dob" id="Dob" min="1920-01-02" class="form-control datepicker" required="" />
</div>
<div class="form-group col-lg-6">
<label>MaritalStatus</label>
<select name="MaritalStatus" id="MaritalStatus" class="form-control" required="">
<option value="" disabled="disabled">-- Select -- </option>
<option value="Single">Single</option>
<option value="Married">Married</option>
</select>
</div>
<div class="form-group col-lg-6">
<label>Hobbies</label>
<input type="text" name="Hobbies" id="Hobbies" class="form-control" placeholder="Football, Cricket etc." />
</div>
<div class="form-group col-lg-6">
<label>Home Telephone</label>
<input type="text" name="TelephoneNo" id="TelephoneNo" class="form-control" placeholder="1234567890" />
</div>
<div class="form-group col-lg-6">
<label>Mobile</label>
<input type="tel" name="MobileNo" id="MobileNo" class="form-control" required="" placeholder="0987654321" />
</div>
<div class="form-group col-lg-12">
<label>Residential Address</label>
<textarea rows="2" name="ResidentialAddress" id="ResidentialAddress" class="form-control" required="">
<div class="form-group col-lg-6 ">
<label>Pin Code</label>
<input name="PinCode" id="PinCode" class="form-control" placeholder="999999" type="text">
</div>
<div class="form-group col-lg-6">
<label>State</label>
<select name="State" id="State" class="form-control" required="">
<option value="" disabled="disabled">-- Select -- </option>
<option value="Maharashtra">Maharastra</option>
<option value="Assam">Assam</option>
<option value="UP">UP</option>
<option value="Gujarat">Gujarat</option>
<option value="AP">Andhra Pradesh</option>
</select>
</div>
<div class="form-group col-lg-6">
<label>Nationality</label>
<input name="title" id="Nationality" class="form-control" placeholder="Indian" type="text">
</div>
<div class="form-group col-lg-6">
<label>Date of Joining</label>
<input name="Doj" id="Doj" class="form-control datepicker" required="" type="date">
</div>
<div class="form-group col-lg-8">
<div style="float: right">
<input value="Cancel" id="BtnCancel" class="btn btn-primary" type="button">
<input class="btn btn-primary" name="submitButton" id="btnSave" value="Save" type="button">
</div>
</div>
</div>
</div>
</div>
</div>
And the form will somehow look like the following:

Now we need to do Ajax insert for all the fields, that's why I have taken class for all the fields and my class is like this:
Employee Class
public class Employee
{
public int EmpId;
public string FName;
public string LName;
public string MName;
public string Email;
public DateTime Dob;
public string MaritalStatus;
public string Hobbies;
public string HomeMobile;
public string OfficeMobile;
public string Address;
public string Pincode;
public string State;
public string Nationality;
public DateTime Doj;
public DateTime CreatedDateTime;
public DateTime ModifiedDateTime;
}
Save Method to Insert Data in the Database
As we need to call the method through Ajax, so it has to be WebMethod and static:


Soheb AhmadPosted Apr 4, 2024, 1:11 PM
<% for (var data = 0; data < TableData.Rows.Count; data++) sir i dont understand this line can you explain it what is the meaning of TableData.Rows.Count. TableData is tablename,object what is this please explain it
Sunil RockPosted Oct 7, 2020, 11:36 AM
When i click Serverside Code not fire
Saepul BahriPosted Oct 27, 2018, 7:25 PM
Very nice code
Tanmay NehetePosted Aug 23, 2018, 6:40 AM
Need code on edit and delete button
Juan FerreyraPosted Jan 19, 2018, 1:07 PM
Great article!!! Thx
Ade SulistiawanPosted Sep 8, 2017, 8:19 PM
Where place variable TableData? And what the data type?
Vijay BadgujarPosted May 18, 2017, 8:34 AM
Bro when you create table for employee there is filed DOB nvarchar(30) then in Employee Class how can you define DOB as Datetime by the way code is Nice take it as Complement bro
sathish reddyPosted Feb 19, 2017, 2:35 AM
Can you please provide link to download this project
Jeffrey RudeloffPosted Feb 2, 2017, 4:58 AM
Sabyasachi you are a rare thing in these forums, not only is your code what I was looking for but works on the first try after setting up the db and constr. Keep it up!
SubashPosted Aug 1, 2016, 5:40 AM
Nice
Pops FalePosted Mar 29, 2016, 5:11 AM
simple and effective, nice article
Karthik PuppalaPosted Jan 18, 2016, 7:35 AM
Nice Code, easy to understand
vinodh kumarPosted Jan 2, 2016, 1:18 AM
super code
vinodh kumarPosted Jan 2, 2016, 1:18 AM
Superb Coding
Abdul KhaliqPosted Dec 8, 2015, 6:00 AM
Exellent
Pratham DavePosted Sep 26, 2015, 4:46 AM
Nice, pagination & search is not working can you please fixed it.
Ankur ChaturvediPosted Sep 21, 2015, 4:25 AM
Thanks
Ankur ChaturvediPosted Sep 21, 2015, 4:25 AM
When i am using these code than getting "Instance failure." Error can you help me to resolve these problem
Ankur ChaturvediPosted Sep 21, 2015, 4:24 AM
using (TableData) { TableData.Clear(); sda.Fill(TableData); }
tarun sainPosted Sep 12, 2015, 4:02 AM
nice article sir..
Pratham DavePosted Sep 10, 2015, 1:35 AM
Thanks for sharing your knowledge.
Ankit BansalPosted Sep 10, 2015, 1:04 AM
good one..thanks...
Santhakumar MunuswamyPosted Sep 7, 2015, 2:30 PM
Thanks for nice share
RakeshPosted Sep 7, 2015, 12:48 PM
Nice one
Pankaj Kumar ChoudharyPosted Sep 7, 2015, 12:32 PM
Nice Explain Sir...........
Mohammed IbrahimPosted Sep 7, 2015, 11:54 AM
nice
Karthikeyan KPosted Sep 7, 2015, 11:05 AM
Good one sir...Thanks for sharing
Shridhar SharmaPosted Sep 7, 2015, 10:56 AM
nice article