Monday 14 May 2012

Insert,Update and Delete through the StoreProcedure


Database Table Design
 
Page Design
 
Source Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!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>
 
        <asp:Panel ID="Panel1" runat="server"
            style="position:absolute; top: 96px; left: 329px; width: 455px; height: 238px; background-color: #66FFFF;">


            <asp:TextBox ID="TextBox2" runat="server"
                style="position:absolute; top: 89px; left: 218px;"></asp:TextBox>
            <asp:RadioButton ID="RadioButton1" runat="server" GroupName="a"
                style="position:absolute; top: 133px; left: 265px; height: 21px;" Text="User" />
            <asp:RadioButton ID="RadioButton2" runat="server" GroupName="a"
                style="position:absolute; top: 132px; left: 150px;" Text="AdminiStrator" />
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
                style="position:absolute; top: 174px; left: 205px; font-weight: 700; background-color: #339933;"
                Text="Login" />
            <asp:Label ID="Label1" runat="server"
                style="position:absolute; top: 43px; left: 142px;" Text="UserId"></asp:Label>
            <asp:Label ID="Label2" runat="server"
                style="position:absolute; top: 88px; left: 137px;" Text="Password"></asp:Label>
            <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
            <asp:Button ID="Button3" runat="server" onclick="Button3_Click"
                Style="position:absolute; top: 208px; left: 258px; font-weight: 700; background-color: #FFFF00;"
                Text="Delete" />
            <asp:Button ID="Button2" runat="server" onclick="Button2_Click"
                Style="position:absolute; top: 209px; left: 192px; font-weight: 700; background-color: #FFFF00;"
                Text="Insert" />
            <asp:TextBox ID="TextBox3" runat="server"
                style="position:absolute; top: -32px; left: 194px;"></asp:TextBox>
          <asp:TextBox ID="TextBox1" runat="server"
        style="position:absolute; top: 42px; left: 219px;"></asp:TextBox>
        </asp:Panel> 
    </div>
    <p>

    <asp:Label ID="Label4" runat="server" Text=" Id"
            Style="position:absolute; top: 66px; left: 492px;"></asp:Label>

    </p>
    <p>
 
        <asp:Button ID="Button4" runat="server" Text="Update"
            Style="position:absolute; top: 304px; left: 450px; font-weight: 700; background-color: #FFFF00;"
            onclick="Button4_Click"/>
 
        </p>
    <asp:GridView ID="GridView1" runat="server"
        Style="position:absolute; top: 347px; left: 452px;" CellPadding="4"
        ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>

    </form>
</body>
</html>
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
    SqlConnection Con;
    SqlCommand Cmd;
    SqlDataAdapter Da;
    SqlDataReader dr;
    DataTable Dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        Con = new SqlConnection(ConfigurationManager.ConnectionStrings["Satya"].ToString());
        Con.Open();
        if (!IsPostBack)
        {
            display();
        }
    }
 //Retrive Data from database
    public void display()
    {
        DataTable ds=new DataTable();
        try
        {
            if (Con.State == ConnectionState.Closed)
            {
                Con.Open();
            }
         
            Cmd = new SqlCommand("STLOGINDEMO", Con);
            Da = new SqlDataAdapter(Cmd);
            Cmd.CommandType = CommandType.StoredProcedure;
            Da.Fill(ds);
            if (ds.Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }

        catch (Exception Ex)
        {
            string a = Ex.ToString();
        }
    
    }
//Login Page How to Login
    protected void Button1_Click(object sender, EventArgs e)
    {
        string un = TextBox1.Text;
        string pwd = TextBox2.Text;
        string us;
        if (RadioButton2.Checked == true)
        {
            us = "AdminiStrator";
        }
        else
        {
            us = "User";
        }
        try
        {
            if (Con.State == ConnectionState.Closed)
            {
                Con.Open();
            }
            Cmd = new SqlCommand("Select [UserName],[Password],[UserType] from LoginDemo where UserName='" + un + "' and Password='" + pwd + "' and UserType='" + us + "'", Con);
            dr = Cmd.ExecuteReader();
            if (dr.Read())
            {
                if ((TextBox1.Text == dr[0].ToString()) && (TextBox2.Text == dr[1].ToString()) && ((RadioButton2.Text == dr["UserType"].ToString()) || (RadioButton1.Text == dr["UserType"].ToString())))
                {
                    Response.Redirect("HomePage.aspx");
                }
            }
            else
            {
                Label3.Text = "Plz Enter Correct USER ID AND PASSWORD";
            }
        }
        catch (Exception Ex)
        {
            string a = Ex.ToString();
        }
        finally
        {
            if (this.Con.State == ConnectionState.Open)
                Con.Close();
            if (this.Con != null) this.Con = null;
            if (this.Con != null) this.Con = null;
        }
    }
//Insert  Data in to Database
    protected void Button2_Click(object sender, EventArgs e)
    {
        string ab;
        try
        {
            if (Con.State == ConnectionState.Closed)
            {
                Con.Open();
            }
            Cmd = new SqlCommand("InsertLogindemo",Con);
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.Parameters.AddWithValue("@UserName",TextBox1.Text);
            Cmd.Parameters.AddWithValue("@Password", TextBox2.Text);
            if (RadioButton1.Checked == true)
            {
                ab = RadioButton1.Text;
            }
            else
            {
                ab = RadioButton2.Text;
            }
            Cmd.Parameters.AddWithValue("@UserType", ab.ToString());
            Cmd.ExecuteNonQuery();
            display();
        }
        catch (Exception Ex)
        {
            string a = Ex.ToString();
        }
        finally
        {
            if (this.Con.State == ConnectionState.Open)
                Con.Close();
            if (this.Con != null) this.Con = null;
            if (this.Con != null) this.Con = null;
        }
    }
//Delete data From Database
    protected void Button3_Click(object sender, EventArgs e)
    {
      try
        {
            if (Con.State == ConnectionState.Closed)
            {
                Con.Open();
            }

            Cmd = new SqlCommand("DeleteLogindemo",Con);
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.Parameters.AddWithValue("@Id",TextBox3.Text);
          Cmd.ExecuteNonQuery();
          Label3.Text = "succ";
          display();
        }
        catch (Exception Ex)
        {
            string a = Ex.ToString();
        }
        finally
        {
            if (this.Con.State == ConnectionState.Open)
                Con.Close();
            if (this.Con != null) this.Con = null;
            if (this.Con != null) this.Con = null;
        }

    }
//update Data from Database
    protected void Button4_Click(object sender, EventArgs e)
    {
        string ab;
        try
        {
            if (Con.State == ConnectionState.Closed)
            {
                Con.Open();
            }

            Cmd = new SqlCommand("updateLogindemo", Con);
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.Parameters.AddWithValue("@Id", TextBox3.Text);
            Cmd.Parameters.AddWithValue("@UserName", TextBox1.Text);
            Cmd.Parameters.AddWithValue("@Password", TextBox2.Text);
            if (RadioButton1.Checked == true)
            {
                ab = RadioButton1.Text;
            }
            else
            {
                ab = RadioButton2.Text;
            }
            Cmd.Parameters.AddWithValue("@UserType", ab.ToString());
            Cmd.ExecuteNonQuery();
            Label3.Text = "succ";
            display();
        }
        catch (Exception Ex)
        {
            string a = Ex.ToString();
        }
        finally
        {
            if (this.Con.State == ConnectionState.Open)
                Con.Close();
            if (this.Con != null) this.Con = null;
            if (this.Con != null) this.Con = null;
        }
    }
}

insert Storeprocedure
ALTER Procedure InsertLogindemo
(
@UserName Varchar(50),
@Password Varchar(50),
@UserType Varchar(50)
)
As
BEGIN
Insert into LoginDemo (UserName,Password,UserType) values(@UserName,@Password,@UserType)
END
insert Storeprocedure
ALTER Procedure DeleteLogindemo
@Id int
As
BEGIN
delete from LoginDemo where Id=@Id
END

Update Storeprocedure
ALTER Procedure UpdateLogindemo

@Id int,
@UserName varchar(50),
@Password varchar(50),
@UserType varchar(50)
As
BEGIN
update  LoginDemo set UserName=@UserName,Password=@Password,UserType=@UserType where Id=@Id
END

Select StoreProcedure
ALTER Procedure STLOGINDEMO
AS
BEGIN
Select Id,UserName,Password,UserType from LoginDemo
END


No comments:

Post a Comment