Hi rajeesh,
Check this sample. now take its reference.
HTML
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#btnSubmit').click(function () {
var obj = {};
obj.name = $('#txtName').val();
obj.address = $('#txtAddress').val();
obj.gender = $("input[name='RadioGender']:checked").next('label').html()
obj.date = $('#txtDate').val();
var course = "";
$('#CheckBoxList1 input:checked').each(function () {
var text = $(this).closest("td").find("label").html();
course += text + ",";
});
obj.course = course;
obj.state = $('#ddlState').find("option:selected").text();
obj.district = $('#ddlDistrict').find("option:selected").text();
obj.email = $('#txtEmail').val();
obj.phone = $('#txtPhoneNumber').val();
if (obj.name != '' && obj.address != '' && obj.gender != '' && obj.date != '' && obj.course != ''
&& obj.state != '' && obj.district != '' && obj.email != '' && obj.phone != '') {
$.ajax({
type: 'POST',
url: 'Default.aspx/Insert',
data: JSON.stringify(obj),
contentType: 'application/json; charset =utf-8',
dataType: "json",
success: function (data) {
if (data.d != "") {
$('#txtname').val('');
$('#txtaddress').val('');
$('#txtdate').val('');
$('#ddlState').val('');
$('#ddlDistrict').val('');
$('#txtemail').val('');
$('#txtphone').val('');
alert("Data Saved Successfully");
} else {
alert("Error Occured, Try Again");
}
},
error: function (result) {
alert("Error Occured, Try Again");
}
});
} else {
alert("Pleae Fill all the Fields");
return false;
}
});
});
</script>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td><asp:Label ID="lblName" runat="server" Text="Name"></asp:Label></td>
<td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="lblAddress" runat="server" Text="Address"></asp:Label></td>
<td><asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="lblGender" runat="server" Text="Gender"></asp:Label></td>
<td>
<asp:RadioButton ID="rbMale" runat="server" GroupName="RadioGender" Text="Male" />
<asp:RadioButton ID="rbFemale" runat="server" GroupName="RadioGender" Text="Female" />
</td>
</tr>
<tr>
<td><asp:Label ID="lblBirthDate" runat="server" Text="Date of Birth"></asp:Label></td>
<td>
<asp:TextBox ID="txtDate" runat="server" autocomplete="off"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate"
PopupButtonID="txtDate">
</asp:CalendarExtender>
</td>
</tr>
<tr>
<td><asp:Label ID="lblCourse" runat="server" Text="Course"></asp:Label></td>
<td>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
<asp:ListItem>ASP.NET</asp:ListItem>
<asp:ListItem>ASP.NET MVC</asp:ListItem>
<asp:ListItem>WFA</asp:ListItem>
<asp:ListItem>WPF</asp:ListItem>
<asp:ListItem>WCF</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td><asp:Label ID="lblState" runat="server" Text="State"></asp:Label></td>
<td>
<asp:DropDownList ID="ddlState" runat="server">
<asp:ListItem Text="Delhi" Value="Delhi"></asp:ListItem>
<asp:ListItem Text="Maharashtra" Value="Maharashtra"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td><asp:Label ID="lblDistrict" runat="server" Text="District"></asp:Label></td>
<td>
<asp:DropDownList ID="ddlDistrict" runat="server">
<asp:ListItem Text="Agra" Value="Agra"></asp:ListItem>
<asp:ListItem Text="Mumbai" Value="Mumbai"></asp:ListItem>
<asp:ListItem Text="Pune" Value="Pune"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td><asp:Label ID="lblEmail" runat="server" Text="Email"></asp:Label></td>
<td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="lblNumber" runat="server" Text="Contact Number"></asp:Label></td>
<td><asp:TextBox ID="txtPhoneNumber" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2"><asp:Button ID="btnSubmit" runat="server" Text="Submit" /></td>
</tr>
<tr>
<td><asp:Label ID="lblMessage" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td><asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager></td>
</tr>
</table>
</div>
</form>
</body>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Services;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Web.Services
Code
C#
[WebMethod]
public static string Insert(string name, string address, string gender, string date, string course, string state, string district, string email, string phone)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into tbprofile values(@name,@address,@gender,@date,@course,@state,@district,@email,@phone)", con);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@address", address);
cmd.Parameters.AddWithValue("@gender", gender);
cmd.Parameters.AddWithValue("@date", date);
cmd.Parameters.AddWithValue("@course", course);
cmd.Parameters.AddWithValue("@state", state);
cmd.Parameters.AddWithValue("@district", district);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@phone", phone);
int i = 0;
i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return "Saved Successfully";
}
else
{
return "";
}
}
}
VB.Net
<WebMethod()> _
Public Shared Function Insert(ByVal name As String, ByVal address As String, ByVal gender As String, ByVal _date As String, ByVal course As String, ByVal state As String, ByVal district As String, ByVal email As String, ByVal phone As String) As String
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
con.Open()
Dim cmd As SqlCommand = New SqlCommand("insert into tbprofile values(@name,@address,@gender,@date,@course,@state,@district,@email,@phone)", con)
cmd.Parameters.AddWithValue("@name", name)
cmd.Parameters.AddWithValue("@address", address)
cmd.Parameters.AddWithValue("@gender", gender)
cmd.Parameters.AddWithValue("@date", _date)
cmd.Parameters.AddWithValue("@course", course)
cmd.Parameters.AddWithValue("@state", state)
cmd.Parameters.AddWithValue("@district", district)
cmd.Parameters.AddWithValue("@email", email)
cmd.Parameters.AddWithValue("@phone", phone)
Dim i As Integer = 0
i = cmd.ExecuteNonQuery()
con.Close()
If i > 0 Then
Return "Saved Successfully"
Else
Return ""
End If
End Using
End Function