Share Blog

Saturday, May 17, 2014

How to Use Store Procedure With Asp.net

----------Create table---------

----insert Data in Table-------
-----SELECT  STORE PROCEDURE---------
CREATE proc SelectStore
as
begin
select * from sunil
end

 -----DELETE  STORE PROCEDURE---------
Create Procedure DeleteStore
@id int
as
begin
delete from sunil where id=@id
end
-----INSERT  STORE PROCEDURE---------
CREATE  procedure InsertStore
@Id int ,@Name varchar(50),@Address varchar(50)
as
begin
insert into sunil values(@id,@name,@Address)
----UPDATE  STORE PROCEDURE---------
CREATE Proc UpdateStore
@id int,@Name Varchar(50),@Address varchar(50)
as
begin
update sunil set Name=@Name,Address=@Address where id=@id
end
Step -1 First open your visual studio --> File -->New -->Select ASP.NET Empty Website and select Visual C# from left window -->OK-->Now Open Solution Explorer-->Now Right click on website --> Add New Items --> Select web Page & Visual C# --> as shown below:-


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


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1
        {
            width: 100%;
        }
        .auto-style2
        {
            width: 136px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
   
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">ID</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Name</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Address</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Insert" />
                </td>
                <td>
                    <asp:Button ID="Button3" runat="server" Text="Delete" OnClick="Button3_Click" />
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="Update" />
                </td>
                <td>
                    <asp:Button ID="Button5" runat="server" Text="Show" OnClick="Button5_Click" />
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </form>
</body>
</html>



-------Default page Code--------
using System;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=SUNIL;Initial Catalog=storeprocedure;Integrated Security=True");
   // SqlCommand cmd = new SqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
  

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
     SqlCommand cmd = new SqlCommand("InsertStore", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Id",SqlDbType.VarChar).Value= Convert.ToInt32(TextBox2.Text);
        cmd.Parameters.Add("@name",SqlDbType.VarChar).Value=TextBox3.Text;
        cmd.Parameters.Add("@address",SqlDbType.VarChar).Value=TextBox4.Text;
      
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("<script>alert('data successfully')</script>");

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("DeleteStore",con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id",SqlDbType.VarChar).Value=Convert.ToInt32(TextBox2.Text);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("<script>alert('data deleted')</script>");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("UpdateStore",con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id",SqlDbType.VarChar).Value=Convert.ToInt32(TextBox2.Text);
        cmd.Parameters.AddWithValue("@name",SqlDbType.VarChar).Value=TextBox3.Text;
        cmd.Parameters.AddWithValue("@address",SqlDbType.VarChar).Value=TextBox4.Text;
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("<script>alert('data updated')</script>");
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("SelectStore",con);
        cmd.CommandType = CommandType.StoredProcedure;
        //cmd.Parameters.AddWithValue("@id", SqlDbType.VarChar).Value = Convert.ToInt32(TextBox2.Text);
     //  SqlDataReader dr= new SqlDataReader();

       GridView1.DataSource = cmd.ExecuteReader();
       GridView1.DataBind();

    }
}

No comments:

Post a Comment