We provide IT Services

Register Now Click On

Read More Tutorials Click On

Post Page Advertisement [Top]

How to insert record in database using Jquery:-

Create Table:-

create table Employee
Empid int primary key identity(1,1),
Name varchar(50),
Age int,
Address varchar(50)


Create insert Procedure:-

create proc usp_employee_insert
@Name varchar(50),
@Age int,
@Address varchar(50)
insert into Employee(Name,Age,Address) values(@Name,@Age,@Address)

use to j query code with ado.net:-

j query in does not use server control only on html control. j query support Json. How to install json 

Aspx page code in j query:-

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="Jqueryinsertonly.Registration" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="jquery-3.2.1.min.js"></script>
    <script type="text/javascript">

        function Savedata()
                url: 'Registration.aspx/Insert',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                datatype: 'json',
                data: "{A: '" + $("#textname").val() + "', B:'" + $("#textage").val() + "',C: '" + $("#textaddress").val() + "'}",
                success: function () {
                    alert('data is insert !!');
                error: function () {
                    alert('data error !!');
    <form id="form1" runat="server">
                    <td><input type="text" id="textname" /></td>

                    <td><input type="text" id="textage" /></td>

                    <td><input type="text" id="textaddress" /></td>

                    <td><input type="button" id="btnsave" value="Save" onclick="Savedata()" /> </td>


CS page code of Jquery:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Jqueryinsertonly
    public partial class Registration : System.Web.UI.Page
        static SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBC"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)

        public static void Insert(string A, int B, string C)
            SqlCommand cmd = new SqlCommand("usp_employee_insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name", A);
            cmd.Parameters.AddWithValue("@Age", B);
            cmd.Parameters.AddWithValue("@Address", C);


Web.config file code:-

      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
  <add name="DBC" connectionString="data source=Shiva; integrated security=true;initial catalog=jqueryinsertonly"/>

No comments:

Post a Comment

| Designed by Rockprogrammer