Thursday, November 13, 2014

Jquery EXAMPLE

============
Design - jquerydemo.aspx
============
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimpleDemo.aspx.cs" Inherits="SimpleDemo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#<%=btnsub.ClientID %>').click(function () {
                $.ajax({
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: 'SimpleDemo.aspx/IUD',
                    data: "{'Type':'Save','Id':'" + document.getElementById('txtid').value + "','Name':'" + document.getElementById('txtname').value + "', 'Email':'" + document.getElementById('txtemail').value + "','mobile':'" + document.getElementById('txtmobile').value + "','company':'" + document.getElementById('txtcompany').value + "','salary':'" + document.getElementById('txtsalary').value + "'}",
                    async: false,
                    success: function (response) {
                        $('#<%=txtid.ClientID %>').val('');
                        $('#<%=txtname.ClientID %>').val('');
                        $('#<%=txtemail.ClientID%>').val('');
                        $('#<%=txtmobile.ClientID %>').val('');
                        $('#<%=txtcompany.ClientID%>').val('');
                        $('#<%=txtsalary.ClientID%>').val('');
                        alert("Record Has been Saved in Database");
                    },
                    error: function () {
                        console.log('there is some error');
                    }

                });

            });

        });
     
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <fieldset>
            <legend>Insert</legend>
            <table>
                <tr>
                    <td>
                        Id:
                    </td>
                    <td>
                        <asp:TextBox ID="txtid" runat="server" ClientIDMode="Static"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Name:
                    </td>
                    <td>
                        <asp:TextBox ID="txtname" runat="server" ClientIDMode="Static"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Email:
                    </td>
                    <td>
                        <asp:TextBox ID="txtemail" runat="server" ClientIDMode="Static"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Mobile:
                    </td>
                    <td>
                        <asp:TextBox ID="txtmobile" runat="server" ClientIDMode="Static"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Company:
                    </td>
                    <td>
                        <asp:TextBox ID="txtcompany" runat="server" ClientIDMode="Static"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Salary:
                    </td>
                    <td>
                        <asp:TextBox ID="txtsalary" runat="server" ClientIDMode="Static"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnsub" runat="server" Text="submit" ClientIDMode="Static" />
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
            </table>
            <asp:GridView ID="grvEmp" runat="server" AutoGenerateColumns="False"
                CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
                GridLines="None">
                <AlternatingRowStyle BackColor="White" />
                <Columns>          
                    <asp:BoundField HeaderText="ID" DataField="Id" />
                    <asp:BoundField HeaderText="Name" DataField="Name" />
                    <asp:BoundField HeaderText="Email" DataField="Email" />
                    <asp:ButtonField Text="Edit" ButtonType="Button" />
                </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            </asp:GridView>
        </fieldset>
    </div>
    </form>
</body>
</html>


============
Coding - jquerydemo.aspx.cs
============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
public partial class SimpleDemo : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
        }
    }

    [WebMethod]
    public static string IUD(string Type, int Id, string Name, string Email, string mobile, string company, int salary)
    {
        Class obj = new Class();
        return obj.iuddata(Type, Id,Name, Email, mobile, company, salary);
    }

    [WebMethod]
    public DataSet Fetch(string Type, int Id)
    {
        Class obj = new Class();
        grvEmp.DataSource = obj.fetchdata(Type, Id);
        grvEmp.DataBind();
        return obj.fetchdata(Type, Id);
    }
}

=================
Class File - class.cs
=================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for Class
/// </summary>
public class Class
{
    SqlConnection con = new SqlConnection(@"Data Source=VISION4-PC;Initial Catalog=JsonData;User ID=sa;Password=vision");
    DataSet ds = new DataSet();
public Class()
{
//
// TODO: Add constructor logic here
//
}

    public string iuddata(string Type,int Id, string Name, string Email, string mobile, string company, int salary)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("[dbo].[IUDJson]", con);
        cmd.Parameters.AddWithValue("@Type", Type);
        cmd.Parameters.AddWithValue("@Id", Id);
        cmd.Parameters.AddWithValue("@Name", Name);
        cmd.Parameters.AddWithValue("@Email", Email);
        cmd.Parameters.AddWithValue("@mobile", mobile);
        cmd.Parameters.AddWithValue("@company", company);
        cmd.Parameters.AddWithValue("@salary", salary);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
        con.Close();
        return "Record Successfully Done.";
    }

    public DataSet fetchdata(string Type, int Id)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("[dbo].[IUDJson]", con);
        cmd.Parameters.AddWithValue("@Type", Type);
        cmd.Parameters.AddWithValue("@Id", Id);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        con.Close();
        return ds;
    }
}

=============
DATABASE SCRIPT
=============

USE [JsonData]
GO
/****** Object:  Table [dbo].[DemoDB]    Script Date: 11/14/2014 12:05:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[DemoDB](
[Id] [int] NOT NULL,
[Name] [varchar](50) NULL,
[Email] [varchar](50) NULL,
[mobile] [varchar](50) NULL,
[company] [varchar](50) NULL,
[salary] [int] NULL,
 CONSTRAINT [PK_DemoDB] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[DemoDB] ([Id], [Name], [Email], [mobile], [company], [salary]) VALUES (1, N'jalpa', N'hitpatel@gmail.com', N'8794561230', N'vision', 5205)
INSERT [dbo].[DemoDB] ([Id], [Name], [Email], [mobile], [company], [salary]) VALUES (12, N'vision', N'hitpatel@gmail.com', N'7987546410', N'infotech', 450100)
/****** Object:  Table [dbo].[Student]    Script Date: 11/14/2014 12:05:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student](
[Id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[e_id] [varchar](50) NULL,
[Mobile] [varchar](50) NULL,
[city] [varchar](50) NULL,
 CONSTRAINT [PK__Student__3214EC077F60ED59] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Student] ON
INSERT [dbo].[Student] ([Id], [name], [e_id], [Mobile], [city]) VALUES (1, N'Hitesh', N'hitpatel@gmail.com', N'7987894532', N'surat')
INSERT [dbo].[Student] ([Id], [name], [e_id], [Mobile], [city]) VALUES (2, N'Abhay', N'abhay@gmail.com', N'9878456512', N'surat')
SET IDENTITY_INSERT [dbo].[Student] OFF
/****** Object:  StoredProcedure [dbo].[IUDJson]    Script Date: 11/14/2014 12:05:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--exec [dbo].[IUDJson] 'Save',1,'Hitesh','hitpatel@gmail.com','7895461230','vision',12000
CREATE PROCEDURE [dbo].[IUDJson]
--string mode, string name, string email, string mobile, string company, int salary
@Type varchar(15),
@id int=0,
@name varchar(30)='',
@email varchar(30)='',
@mobile varchar(15)='',
@company varchar(30)='',
@salary int=0
AS
BEGIN

if @Type = 'Save'
begin
if Exists (select 1 from dbo.DemoDB where Id=@id)
begin
update dbo.DemoDB set Name=@name,Email=@email,mobile=@mobile,company=@company,salary=@salary
where Id = @id
end
else
begin
Insert into dbo.DemoDB values (@id,@name,@email,@mobile,@company,@salary)

end
end

if @Type = 'Delete'
begin
Delete from dbo.DemoDB where Id=@id
end
END
GO

No comments:

Post a Comment

Demo for Repository Pattern in ASP.Net

----------------------------------------------------------- ----------------------------------------------------------- Repository Projec...