Sei sulla pagina 1di 11

Proceduri Stocate (Stored Procedures)

O procedurã stocatã este o secvenþã de instrucþiuni SQL, salvatã în baza de date,care poate fi apelatã de aplicaþii
diferite. Sql Server compileazã procedurile stocate,ceea ce creºte eficienþa utilizãrii lor. De asemenea, procedurile
stocate pot avea para-metri.O procedurã stocatã poate fi apelatã folosind obiectul SqlCommand:
SqlCommandcmd =newSqlCommand(“StoredProcedure1”, conn); cmd.CommandType
=CommandType.StoredProcedure;//Tipul obiectului comanda este pro- cedura stocata
Primul parametru al constructorului este un ºir de caractere ce reprezintã numeleprocedurii stocate. A doua
instrucþiune de mai sus spune obiectului SqlCommand cetip de comandã va fi executatã, prin intermediul
proprietãþii CommandType.

Exemplu:
SqlCommandcmd =newSqlCommand(“StoredProcedure1”, conn); cmd.CommandType
=CommandType.StoredProcedure;//Tipul obiectului coman- da este procedura stocatapersonDs =newDataSet();
personDa =newSqlDataAdapter(“”, conn); personDa.SelectCommand =
cmd;personDa.Fill(personDs,“PersonTable”);
Apelul procedurilor stocate, parametrizate, este asemãnator cu cel al interogãrilorcu parametri.
//Obiect Comanda, in care primul parametru este numele procedurii stocateSqlCommandcmd
=newSqlCommand(“City”, conn); cmd.CommandType =CommandType.StoredProcedure;//Tipul obiectului coman-
da este procedura stocatacmd.Parameters.Add(newSqlParameter(“@City”, inputCity)); personDs =newDataSet();
personDa =newSqlDataAdapter(“”, conn); personDa.SelectCommand =
cmd;personDa.Fill(personDs,“PersonTable”);
Primul argument al constructorului obiectului SqlCommand este numele proce-durii stocate. Aceastã procedurã are
un parametru numit @City. De aceea trebuiefolosit un obiect de tip SqlParameter pentru a adauga acest parametru la
obiectul detip Command.

In this Article you can learn how to edit, update, delete, and cancel in gridview.
First drag and drop Gridview.In gridview properties set AutoGenarateColumns to
False.
Next open Default.aspx source code. To make a columns in Gridview use
<asp:TemplateField></asp:TemplateField>
Here first I created a table name 'emp' in my database.it contains 3 colomns as
id,name,marks,I am creating this colomns in gridview as follows:
 
view source

print?
<asp:GridView ID="GridView1" runat="server"
01 AutoGenerateColumns="False" Style="z-index: 100;left: 298px;
position: absolute; top: 118px">
02 <Columns >
03 <asp:TemplateField HeaderText ="IDNO">
04 <ItemTemplate>
0 <asp:LabelID="lblid"runat="server"Text='<%#Eval("rowid")
5 %>'></asp:Labe>
0
</ItemTemplate>
6
07 </asp:TemplateField>    
08  <asp:TemplateField HeaderText="Name" >
09 <ItemTemplate > <%#Eval("name") %></ItemTemplate> 
10 </asp:TemplateField>

11 <asp:TemplateField HeaderText ="Marks">


12 <ItemTemplate><%#Eval("marks") %></ItemTemplate>
13 </asp:TemplateField>
14            

15 </Columns>
16 </asp:GridView>

 
In default.asp.cs page fill the gridview using SqlDataAdapter using below
code:

view source

print?
SqlDataAdapter da = new SqlDataAdapter("select * from emp",
1
conn);
2        DataSet ds = new DataSet();

3        da.Fill(ds, "emp");
4        GridView1.DataSource = ds.Tables[0];
5        GridView1.DataBind();
6        conn.Close();

Next in Default.asp page select your gridview in Properties set


AutoGenarateDeleteButton=True and AutoGenarateEditButton=True.
 

 
 
 
Next we have to write a code for editing,updating,cancel:In Default.aspx source
code we have to add <EditItemTemplate>
This <EditItemTemplate> is used to Edit the Row in Gridview.Here I am going to Edit
only two columns name and marks.
 

 
 
 
For Editing a Gridview:
 
 
In Gridview Events:Double Click on RowEditing Event and write below code
 
view source

print?
protected void GridView1_RowEditing(object
1
sender,GridViewEditEventArgs e)
2     {
3         GridView1.EditIndex = e.NewEditIndex;
4         bind();
5          
6     }
When you click on Edit link it shows Update,Cancel links
 
 
 
 
For Updating a Gridview:
 

Updating link is used to update a Particular row in emp table using


Gridview.
 
Double click on RowUpdating Event and write below code
view source

print?
protected void GridView1_RowUpdating(object sender,
01
GridViewUpdateEventArgs e)
02     {
        GridViewRow row =
03
(GridViewRow)GridView1.Rows[e.RowIndex];
04         Label lbl = (Label)row.FindControl("lblid");
        TextBox textname =
05
(TextBox)row.FindControl("textbox1");
        TextBox textmarks =
06
(TextBox)row.FindControl("textbox2");
07         
08         GridView1.EditIndex = -1;
09         conn.Open();
        SqlCommand cmd = new SqlCommand("update  emp set
10 marks=" + textmarks.Text + " , name='" + textname.Text + "'
where rowid=" + lbl.Text + "", conn);
11        
12         cmd.ExecuteNonQuery();
13         conn.Close();
14         bind();
15         
16    
17     }
 
 
 For Canceling a gridview row Operation:
 
Cancel Link in used to cancel the particular row operation before upadating.when you
click on Gricview it goes in first stage.
 
Double click on RowCancelingEdit Event and wrtie belwo code
 
view source

print?
protected void GridView1_RowCancelingEdit(object
1
sender,GridViewCancelEditEventArgs e)
2     {
3         GridView1.EditIndex = -1;
4         bind();
5     }
 
For Deleting a Gridview row:
Delete Link is used to delete a Row in a emp table.it permanently deletes
a particular Row From GridView
 
Double Click on RowDeleting Event and write below code
 
view source

print?
protected void GridView1_RowDeleting(object sender,
01
GridViewDeleteEventArgs e)
02     {
        GridViewRow row =
03
(GridViewRow)GridView1.Rows[e.RowIndex];
04         Label lbldeleteID = (Label)row.FindControl("lblid");

05         conn.Open();
        SqlCommand cmd = new SqlCommand("delete  emp where
06
rowid=" + lbldeleteID.Text + "", conn);
07         cmd.ExecuteNonQuery();
08         conn.Close();
09         bind();
10     }
 
The Complete code is written as follows:

view source

print?
<%@ Page Language="C#" AutoEventWireup="true" 
01
CodeFile="Default.aspx.cs" Inherits="_Default" %>
02   
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
03 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
04   
05 <html xmlns="http://www.w3.org/1999/xhtml" >
06 <head runat="server">
07     <title>Untitled Page</title>
08 </head>

09 <body>
10     <form id="form1" runat="server">
11     <div>
        <asp:GridView ID="GridView1" runat="server"
12
AutoGenerateColumns="False" Style="z-index: 100;
            left: 298px; position: absolute; top: 118px"
AllowSorting="True" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True"
OnRowEditing="GridView1_RowEditing"
1
3 OnRowCancelingEdit="GridView1_RowCancelingEdit" 
OnRowUpdating="GridView1_RowUpdating"
OnRowDeleting="GridView1_RowDeleting" BackColor="#FF8080"
BorderColor="SaddleBrown" BorderStyle="None" CellPadding="4"
ForeColor="#333333" GridLines="None" PageSize="5">
1
            <Columns >
4
            <asp:TemplateField HeaderText
="IDNO"><ItemTemplate ><asp:Label ID="lblid" runat ="server"
15
Text ='<%#Eval("rowid") %>'
></asp:Label></ItemTemplate></asp:TemplateField>
16               
            <asp:TemplateField HeaderText="Name"
17
><ItemTemplate > <%#Eval("name") %></ItemTemplate> 
18             <EditItemTemplate >
            <asp:TextBox ID="textbox1" runat ="server" Text
19
='<%#Eval("name") %>'></asp:TextBox>
20             </EditItemTemplate>
21             </asp:TemplateField>
22               
            <asp:TemplateField HeaderText
23
="Marks"><ItemTemplate><%#Eval("marks") %> </ItemTemplate>
24            <EditItemTemplate >
           <asp:TextBox ID="textbox2" runat ="server" Text ='<
25
%#Eval("marks") %>'></asp:TextBox>
26            </EditItemTemplate>
27             </asp:TemplateField>
28               

29             </Columns>
            <FooterStyle BackColor="#990000" Font-Bold="True"
30
ForeColor="White" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333"
31
/>
            <SelectedRowStyle BackColor="#FFCC66" Font-
32
Bold="True" ForeColor="Navy" />
            <PagerStyle BackColor="#FFCC66"
33
ForeColor="#333333" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#990000" Font-Bold="True"
34
ForeColor="White" />
35             <AlternatingRowStyle BackColor="White" />
36         </asp:GridView>

37       
38     </div>
39     </form>
40 </body>
41 </html><>

and in your .cs page

view source

print?
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Web;
05 using System.Web.Security;
06 using System.Web.UI;

07 using System.Web.UI.WebControls;
08 using System.Web.UI.WebControls.WebParts;
09 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 public partial class _Default : System.Web.UI.Page 
12 {

13     SqlConnection conn;
14     protected void Page_Load(object sender, EventArgs e)
15     {
        conn = new SqlConnection("Data
16
Source=INTHIYAAZ;Initial Catalog=shakeer;uid=sa;pwd=sa;");
17         if(!IsPostBack )
18         {
19             bind();
20        }

21     }
    protected void GridView1_RowEditing(object sender,
22
GridViewEditEventArgs e)
23     {
24         GridView1.EditIndex = e.NewEditIndex;
25         bind();
26           

27     }
    protected void GridView1_RowCancelingEdit(object sender,
28
GridViewCancelEditEventArgs e)
29     {
30         GridView1.EditIndex = -1;
31         bind();
32     }

33      
    protected void GridView1_RowUpdating(object sender,
34
GridViewUpdateEventArgs e)
35     {
        GridViewRow row =
36
(GridViewRow)GridView1.Rows[e.RowIndex];
37         Label lbl = (Label)row.FindControl("lblid");
        TextBox textname =
38
(TextBox)row.FindControl("textbox1");
        TextBox textmarks =
39
(TextBox)row.FindControl("textbox2");
40          
41         GridView1.EditIndex = -1;
42         conn.Open();
        SqlCommand cmd = new SqlCommand("update  emp set
43 marks=" + textmarks.Text + " , name='" + textname.Text + "'
where rowid=" + lbl.Text + "", conn);
44         
45         cmd.ExecuteNonQuery();
46         conn.Close();
47         bind();
48          

49     }
50     public void bind()
51     {
52           
53         conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from
54
emp", conn);
55         DataSet ds = new DataSet();
56         da.Fill(ds, "emp");
57         GridView1.DataSource = ds.Tables[0];
58         GridView1.DataBind();
59         conn.Close();
60     }
    protected void GridView1_SelectedIndexChanging(object
61
sender, GridViewSelectEventArgs e)
62     {
63           
64     }
    protected void GridView1_RowCommand(object sender,
65
GridViewCommandEventArgs e)
66     {
67          
68     }
    protected void GridView1_RowDeleting(object sender,
69
GridViewDeleteEventArgs e)
70     {
        GridViewRow row =
71
(GridViewRow)GridView1.Rows[e.RowIndex];
72         Label lbldeleteID = (Label)row.FindControl("lblid");

73         conn.Open();
        SqlCommand cmd = new SqlCommand("delete  emp where
74
rowid=" + lbldeleteID.Text + "", conn);
75         cmd.ExecuteNonQuery();
76         conn.Close();
77         bind();
78     }
    protected void GridView1_PageIndexChanging(object sender,
79
GridViewPageEventArgs e)
80     {
81         GridView1.PageIndex = e.NewPageIndex;
82         bind();
83       
84     }
85

Potrebbero piacerti anche