View Single Post
  #3 (permalink)  
Old February 9th, 2009, 10:49 AM
Dmitriy Dmitriy is offline
Friend of Wrox
 
Join Date: Jul 2003
Location: , , .
Posts: 121
Thanks: 1
Thanked 0 Times in 0 Posts
Default Pass parameters to stored procedure from GridView

This is my Code behind:

Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
namespace LibraryFileUpload.Restricted
{
publicpartialclassUsersList : System.Web.UI.Page
{
privatestring sErrorMessage = null;
protectedvoid Page_Load(object sender, EventArgs e)
{
}
protectedvoid usersGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
Session["SelectedUserName"] = usersGridView.Rows[e.RowIndex].Cells[1].Text;
}
catch (Exception en)
{
sErrorMessage = en.Message;
Session["Message"] = "ERROR inside the usersGridView_RowDeleting() function. " + sErrorMessage + " Contact customer support,please. ";
Response.Redirect("ErrorForm.aspx", false);
}
}
protectedvoid LoginStatus2_LoggedOut(object sender, EventArgs e)
{
Session.Clear();
Session.Abandon();
}
protectedvoid SqlDataSource_Users_Deleting(object sender, SqlDataSourceCommandEventArgs e)
{
if (Session["SelectedUserName"] != null)
{
e.Command.Parameters["@ApplicationName"].Value = "/";
e.Command.Parameters["@UserName"].Value = (String)Session["SelectedUserName"];
e.Command.Parameters["@TablesToDeleteFrom"].Value = 5;
e.Command.Parameters["@NumTablesDeletedFrom"].Value = 0;
}
}
protectedvoid usersGridView_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
if (e.Exception != null)
{
e.ExceptionHandled = true;
sErrorMessage = e.Exception.Message;
Session["Message"] = "ERROR inside the usersGridView_RowDeleted() function. " + sErrorMessage + " Contact customer support,please. ";
Response.Redirect("ErrorForm.aspx", false);
}
}
}
}
And this is my form:

Code:
<%@PageLanguage="C#"MasterPageFile="~/Restricted/LibraryFileUpload4.Master"AutoEventWireup="true"CodeBehind="UsersList.aspx.cs"Inherits="LibraryFileUpload.Restricted.UsersList"Title="Untitled Page" %>
<asp:ContentID="Content1"ContentPlaceHolderID="head"runat="server">
<styletype="text/css">
.styleTitle
{
font-weight: bold;
text-decoration: underline;
height: 80px;
}
.style7
{
font-weight: bold;
text-decoration: underline;
height: 43px;
}
.styleWaitMessage
{
font-weight: bold;
color: Blue;
}
</style>
</asp:Content>
<asp:ContentID="Content2"ContentPlaceHolderID="ContentPlaceHolder1"runat="server">
<%--<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>--%>
<tablestyle="width: 100%;">
<tr>
<tdalign="center">
<tablestyle="width: 100%;">
<trvalign="top">
<tdalign="center"class="style7">
<fontcolor="#336699"size="4">Users List Screen</font>
</td>
</tr>
<tr>
<tdalign="center">
<br/>
<asp:UpdateProgressID="UpdateProgress1"runat="server">
<ProgressTemplate>
<divclass="styleWaitMessage">
<imgsrc="../Images/spinner3-bluey.gif"/> Processing ... Wait, please.
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</td>
</tr>
<trclass="style6">
<tdalign="center">
<asp:GridViewID="usersGridView"runat="server"AllowPaging="True"AllowSorting="True"BorderStyle="Double"BorderWidth="2px"CellPadding="4"CellSpacing="2"EnableSortingAndPagingCallbacks="True"Font-Bold="True"ForeColor="#333333"Width="346px"AutoGenerateColumns="False"DataSourceID="SqlDataSource_Users"onrowdeleting="usersGridView_RowDeleting"DataKeyNames="UserName"onrowdeleted="usersGridView_RowDeleted">
<FooterStyleBackColor="#5D7B9D"Font-Bold="True"ForeColor="White"/>
<RowStyleBackColor="#F7F6F3"ForeColor="#333333"/>
<Columns>
<asp:CommandFieldButtonType="Button"ShowDeleteButton="True"/>
<asp:BoundFieldDataField="UserName"HeaderText="UserName"SortExpression="UserName"/>
<asp:BoundFieldDataField="Email"HeaderText="Email"SortExpression="Email"/>
<asp:BoundFieldDataField="CreateDate"HeaderText="CreateDate"SortExpression="CreateDate"/>
</Columns>
<PagerStyleBackColor="#284775"ForeColor="White"HorizontalAlign="Center"/>
<SelectedRowStyleBackColor="#E2DED6"Font-Bold="True"ForeColor="#333333"/>
<HeaderStyleBackColor="#5D7B9D"Font-Bold="True"ForeColor="White"/>
<EditRowStyleBackColor="#999999"/>
<AlternatingRowStyleBackColor="White"ForeColor="#284775"/>
</asp:GridView>
<asp:SqlDataSourceID="SqlDataSource_Users"runat="server"ConnectionString="<%$ ConnectionStrings:MyFileUploadDBConnectionString %>"SelectCommand="SELECT aspnet_Users.UserName, aspnet_Membership.Email, aspnet_Membership.CreateDate FROM aspnet_Users INNER JOIN aspnet_Membership ON aspnet_Users.UserId = aspnet_Membership.UserId"DeleteCommandType="StoredProcedure"DeleteCommand="dbo.aspnet_Users_DeleteUser"ondeleting="SqlDataSource_Users_Deleting">
<DeleteParameters><asp:ParameterDirection="ReturnValue"Name="RETURN_VALUE"Type="Int32"DefaultValue=""/>
<asp:ParameterName="ApplicationName"Type="String"DefaultValue=""/>
<asp:ParameterDefaultValue=""Name="UserName"Type="String"></asp:Parameter>
<asp:ParameterDefaultValue=""Name="TablesToDeleteFrom"Type="Int32"/>
<asp:ParameterDefaultValue=""Direction="InputOutput"Name="NumTablesDeletedFrom"Type="Int32"/>
</DeleteParameters>
</asp:SqlDataSource>
</td>
</tr>
</table></td>
<tdalign="right"valign="top"width="50px">
<asp:LoginStatusID="LoginStatus2"runat="server"Height="31px"ToolTip="User Login/Logout"Width="46px"onloggedout="LoginStatus2_LoggedOut"/>
</td>
</tr>
</table>
<%--</ContentTemplate>
</asp:UpdatePanel> --%> 
</asp:Content>
Should I specify the returned value as well?