Sunday, 31 August 2014

Insert,Select,Delete, Update by SQL Helper

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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div><table>
       
       <tr><td><asp:Label ID="Label2" runat="server" Text="Name"></asp:Label></td><td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td></tr>
     
       <tr><td>
           <asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
               Text="Update" />
           <asp:Button ID="Button2" runat="server" Text="Insert" onclick="Button2_Click" />
           </td></tr>
     
        </table>
       
    <table>
    <tr><td>
  <asp:GridView ID="Gridupdate" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
            CellPadding="4" OnPageIndexChanging="Gridstate_PageIndexChanging" OnRowDeleting="Gridupdate_RowDeleting"
            OnSelectedIndexChanged="Gridstate_SelectedIndexChanging" Width="100%" EmptyDataText="no record found">
                                <Columns>
                                 <asp:TemplateField>
                           <ItemTemplate>
                         
                             <asp:Label ID="ID" runat="server"
                                   Text='<%#Eval("Id")%>' Visible="false"></asp:Label>
                           </ItemTemplate>
                           </asp:TemplateField>
                                    <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True"
                                        SortExpression="Name" />
                                   
                                 
                                   <asp:HyperLinkField Text="Edit" DataNavigateUrlFields="Id" DataNavigateUrlFormatString="Update.aspx?Id={0}" />
                                 
                                   
                                 
                                   
                                 
                                    <asp:CommandField ShowDeleteButton="True" />
                                 
                                   
                                 
                                   
                                 
                                </Columns>
                                <EmptyDataTemplate>
                                    NO record
                                </EmptyDataTemplate>
                                <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                                <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                                <RowStyle BackColor="White" ForeColor="#003399" />
                                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                            </asp:GridView>

     
                            <asp:HiddenField ID="hdn_update" runat="server" />
                            </td></tr>
    </table>
    </div>
    </form>
</body>
</html>


.CS....>>>

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 Microsoft.ApplicationBlocks.Data;
namespace WebApplication1
{
    public partial class Update : System.Web.UI.Page
    {
        string str = System.Configuration.ConfigurationManager.AppSettings["SQLConnkuldeep"].ToString();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                show();
                if (Request.QueryString["Id"] != null)
                {
                    hdn_update.Value = Request.QueryString["Id"].ToString();
                    showpudate(Convert.ToInt32(hdn_update.Value));
                
                }
            }
        }
 
        public void show()
        {
            DataSet ds = SqlHelper.ExecuteDataset(str, CommandType.StoredProcedure, "sp_select");

            

                Gridupdate.DataSource = ds;
                Gridupdate.DataBind();
            
        }
        public void showpudate( int id)
        {
            SqlParameter[] prm = new SqlParameter[1];
            prm[0] = new SqlParameter("@id", Convert.ToInt32(id));
           

          
         DataSet ds=  SqlHelper.ExecuteDataset(str, CommandType.StoredProcedure, "sp_select1", prm);
          if (ds.Tables[0].Rows.Count > 0)
          {
            TextBox2.Text= ds.Tables[0].Rows[0]["name"].ToString();
          }
        }


        protected void Gridupdate_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Gridupdate.PageIndex = e.NewPageIndex;
            show();
        }

        protected void Gridupdate_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
            string ID = ((Label)(Gridupdate.Rows[e.RowIndex].FindControl("Id"))).Text;
 SqlParameter[] prm = new SqlParameter[1];
            prm[0] = new SqlParameter("@id",ID);
           

           
           SqlHelper.ExecuteNonQuery(str, CommandType.StoredProcedure, "sp_delete", prm);
        
            show();
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {

            if (Request.QueryString["Id"] != null)
            {
                SqlParameter[] prm = new SqlParameter[3];
                prm[0] = new SqlParameter("@id", Convert.ToInt32(hdn_update.Value));

                prm[1] = new SqlParameter("@name", TextBox2.Text);



                prm[2] = new SqlParameter("@result", SqlDbType.Int);
                prm[2].Direction = ParameterDirection.ReturnValue;
                SqlHelper.ExecuteNonQuery(str, CommandType.StoredProcedure, "sp_update1", prm);

                Int32 check = Convert.ToInt32(prm[2].Value);

                if (check == 1)
                {
                    show();

                }
            }
            else {

                Response.Write("click on edit");
            }
        }

        protected void Gridstate_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Gridupdate.PageIndex = e.NewPageIndex;
            show();

        }

        protected void Gridstate_SelectedIndexChanging(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
SqlParameter[] prm = new SqlParameter[2];
            prm[0] = new SqlParameter("@name", TextBox2.Text);
       
           
           prm[1] = new SqlParameter("@result", SqlDbType.Int);
            prm[1].Direction = ParameterDirection.ReturnValue;
           SqlHelper.ExecuteNonQuery(str,CommandType.StoredProcedure,"sp_insert", prm);

         Int32 check = Convert.ToInt32(prm[1].Value);
          if (check == 1)
            {
       
                show();
             clear();

        }
        else 
    {
        Response.Write("hi");
                
    }
        }



        public void clear()
        {
            TextBox2.Text = string.Empty;
          
        }
        }
    }

 table........>>>

USE [test]
GO

/****** Object:  Table [dbo].[tbl_user]    Script Date: 08/31/2014 06:51:11 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_user](
[Id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[Isactive] [bit] NULL,
 CONSTRAINT [PK_tbl_user] 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

ALTER TABLE [dbo].[tbl_user] ADD  CONSTRAINT [DF_tbl_user_Isactive]  DEFAULT ((1)) FOR [Isactive]
GO

store procedure ......................>>>>


CREATE PROCEDURE [dbo].[sp_insert]
(
@name varchar(100)
--@result  Output
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- Insert statements for procedure here
insert into tbl_user
(
name
)
values
(
@name
)
--set @result =1
return 1  
END

Create  PROCEDURE [dbo].[sp_select]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- update statements for procedure here
select id, name from tbl_user where Isactive=1

 
end

ALTER PROCEDURE [dbo].[sp_select1]
(
@id int
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- update statements for procedure here
select * from tbl_user where Id=@id and  Isactive=1

return 1  
end

ALTER PROCEDURE [dbo].[sp_update1]
(
@id int,
@name varchar(100)
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- update statements for procedure here
UPDATE tbl_user SET name=@name WHERE Id=@id

return 1  
end
ALTER PROCEDURE [dbo].[sp_delete]
(
@id int
--@result  Output
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- update statements for procedure here
update  tbl_user
set isactive=0
where Id=@id

return 1  
end

No comments:

Post a Comment