We provide IT Services

Register Now Click On

Read More Tutorials Click On

Post Page Advertisement [Top]

how to bind data to grid view with query or json in asp.net:-


Create a webpage in a progect then install the json in a project(How to install Json in project) and add the jquery library in a project. J query accept the html control.
jquery does not accept server control.

Benefits of jquery:- Jquery is light weight project create ,jquery does not post back controls in .net project.And jquery project does not take load time.

What is append function in jquery:-

.append() is also support passing the multiple arguments as input. .append() is declare the continue arguments.


success: function (ds) {
                    ds = JSON.parse(ds.d);
                    $("#tbl").find("tr:gt(0)").remove();
                    for (var i = 0; i < ds.length; i++)
                    {
                        $("#tbl").append('<tr> <td>'+ds[i].Name+'</td> <td>'+ds[i].Age+'</td>  <td>'+ds[i].Address+'</td>  <td>'+ds[i].Mobile+'</td></tr>')
                    }
                },

How to convert data set to json:- 

 variable name(dt) = JsonConvert.SerializeObject(ds.Tables[0]);



where call the data set value in jquery property:- 

success: function(ds){------------}


how to change value string to integer:-


   syntax-      ds = JSON.parse(ds.d);


How to install json in project:-

how to create grid bind project in jquery:-

.Aspx page code:-

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            Databind();
        });
        function Savedata() {
            $.ajax({
                url: 'Reg.aspx/Insert',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                datatype: 'json',
                data: "{A:'" + $("#textname").val() + "',B:'" + $("#textage").val() + "',C:'" + $("#textaddress").val() + "',D:'" + $("#textmobile").val() + "'}",
                success: function () {
                    alert('Insert Successfull !!');
                },
                error: function () {
                    alert('insert error!!')
                }
            });
        }

        function Databind() {
            $.ajax({
                url: 'Reg.aspx/Getdata',
                type: 'post',
                contentType: 'application/json;charset=utf-8',
                datatype: 'json',
                data: "{}",
                success: function (ds) {
                    ds = JSON.parse(ds.d);
                    $("#tbl").find("tr:gt(0)").remove();
                    for (var i = 0; i < ds.length; i++)
                    {
                        $("#tbl").append('<tr> <td>'+ds[i].Name+'</td> <td>'+ds[i].Age+'</td>  <td>'+ds[i].Address+'</td>  <td>'+ds[i].Mobile+'</td></tr>')
                    }
                },
                error: function () {
                    alert('insert error!!')
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Name:</td>
                    <td>
                        <input type="text" id="textname" /></td>
                </tr>

                <tr>
                    <td>Age:</td>
                    <td>
                        <input type="text" id="textage" /></td>
                </tr>

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

                <tr>
                    <td>Mobile</td>
                    <td>
                        <input type="text" id="textmobile" /></td>
                </tr>

                <tr>
                    <td></td>
                    <td>
                        <input type="button" id="btnsave" value="Save" onclick="Savedata()" /></td>
                </tr>
            </table>
            <table id="tbl" border="1" style="background-color:pink" >
                <tr style="background-color:maroon;color:white">
                    <th>Name</th>
                    <th>Age</th>
                    <th>Address</th>
                    <th>Mobile</th>
                    <th></th>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

.cs page code:-


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;              (add data to linq namespace )
using System.Configuration;
using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

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

        }
        [WebMethod]
        public static void Insert(string A,int B, string C,string D)
        {
            con.Open();
            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);
            cmd.Parameters.AddWithValue("@Mobile", D);
            cmd.ExecuteNonQuery();
            con.Close();
        }
        [WebMethod]
        public static string Getdata()
        {
            string dt = "";
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_employee_get", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                dt = JsonConvert.SerializeObject(ds.Tables[0]);
            }
            return dt;
        }
    }
}

Web.config page code:-

<connectionStrings>
    <add name="DBCS" connectionString="data source=Shiva;integrated security=true;initial catalog=jqueryp"/>
  </connectionStrings>

output is:-



No comments:

Post a Comment

| Designed by Rockprogrammer