What is wrong with my DataGrid?

G

Guest

I need to be able to delete a record/row from a database table. I am using a
DataGrid which is not sending the row index key properly to the event in the
codebehind. When I set a break point and check the values in the Autos
window e.Items.ItemIndex always returns a 0 so the stored procedure does not
know which record to delete.

Here is the Datagrid markup:
<asp:datagrid id="clubdg" DataKeyField="UserID" runat="server"
GridLines="Vertical" CellPadding="2"
BorderWidth="1px" BorderColor="#CCCCFF" CssClass="txt"
AutoGenerateColumns="False" OnCancelCommand="Cancel_Click"
OnUpdateCommand="Update_Click" OnDeleteCommand="Delete_Click"
OnEditCommand="Edit_Click">
<SelectedItemStyle VerticalAlign="Top"></SelectedItemStyle>
<EditItemStyle VerticalAlign="Top"
BackColor="#000033"></EditItemStyle>
<AlternatingItemStyle VerticalAlign="Top"></AlternatingItemStyle>
<ItemStyle VerticalAlign="Top"></ItemStyle>
<HeaderStyle Font-Size="10px" Font-Names="Verdana"
Font-Bold="True" HorizontalAlign="Center"
VerticalAlign="Middle" BackColor="#000044"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Staff Name">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"UserPrefix")%>
<%# DataBinder.Eval(Container.DataItem,"UserFName")%>
<%# DataBinder.Eval(Container.DataItem,"UserLName")%>
<BR>
Organization
ID: <%#DataBinder.Eval(Container.DataItem,"ClubID")%>
</ItemTemplate>
<EditItemTemplate>
User Prefix:<BR>
<asp:TextBox id="UserPrefix" Runat="server"></asp:TextBox><BR>
User First Name:<BR>
<asp:TextBox id="UserFName" Runat="server"></asp:TextBox><BR>
User Last Name:<BR>
<asp:TextBox id="UserLName" Runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="User Access">
<ItemTemplate>
Email Address:<br>
<%# DataBinder.Eval(Container.DataItem, "UserEmail")%>
<br>
Password: <%#
DataBinder.Eval(Container.DataItem,"UserPassword")%><br>
User Role: <%# DataBinder.Eval(Container.DataItem,
"AccessID")%><br>
<br>
<br>
Show: <%# DataBinder.Eval(Container.DataItem,"show")%><br>
Booking: <%#
DataBinder.Eval(Container.DataItem,"Bookings")%><br>
General: <%#
DataBinder.Eval(Container.DataItem,"General")%><br>
Guest List: <%#
DataBinder.Eval(Container.DataItem,"GuestList")%>
</ItemTemplate>
<EditItemTemplate>
User Email:<br>
<asp:TextBox ID="UserEmail" Runat="server"></asp:TextBox><br>
User Password:<br>
<asp:TextBox ID="UserPassword"
Runat="server"></asp:TextBox><br>
User Role:<br>
<asp:DropDownList ID="AccessID" Runat="server">
<asp:ListItem Selected="True">Select Role</asp:ListItem>
<asp:ListItem Value="Admin">Administrator</asp:ListItem>
<asp:ListItem Value="Promotor">Promotor</asp:ListItem>
<asp:ListItem Value="General Staff">General
Staff</asp:ListItem>
</asp:DropDownList><br>
Show On Contacts:
<asp:CheckBox ID="show" Runat="server"></asp:CheckBox><br>
<br>
<table class="txt" cellpadding="0" cellspacing="0"
border="0" width="100%">
<tr>
<td>in:</td>
</tr>
<tr>
<td>Bookings contact List:</td>
<td>
<asp:CheckBox ID="Bookings"
Runat="server"></asp:CheckBox></td>
</tr>
<tr>
<td>General Information:</td>
<td>
<asp:CheckBox ID="General"
Runat="server"></asp:CheckBox></td>
</tr>
<tr>
<td>Guest List:</td>
<td>
<asp:CheckBox ID="GuestList"
Runat="server"></asp:CheckBox></td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Staff Address">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"UserStreetNumber")%>
<%# DataBinder.Eval(Container.DataItem, "UserAddress")%><br>
<%# DataBinder.Eval(Container.DataItem,"UserCity")%>
, <%#
DataBinder.Eval(Container.DataItem,"UserProvince")%><br />
<%# DataBinder.Eval(Container.DataItem,"UserPostal")%>
</ItemTemplate>
<EditItemTemplate>
Street Number:
<asp:TextBox ID="UserStreetNumber" Width="30"
Runat="server"></asp:TextBox><br>
Street Address:<br>
<asp:TextBox ID="UserAddress"
Runat="server"></asp:TextBox><br>
City:<br>
<asp:TextBox ID="UserCity" Runat="server"></asp:TextBox><br>
Province:<br>
<asp:TextBox ID="UserProvince"
Runat="server"></asp:TextBox><br>
Postal Code:<br>
<asp:TextBox ID="UserPostal" Runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Contact Information">
<ItemTemplate>
Phone: <%#
DataBinder.Eval(Container.DataItem,"UserPhone","{0:###-###-####}")%><br>
Fax: <%#
DataBinder.Eval(Container.DataItem,"UserFax","{0:###-###-####}")%><br>
Mobile: <%#
DataBinder.Eval(Container.DataItem,"UserMobile","{0:###-###-####}")%><br>
</ItemTemplate>
<EditItemTemplate>
Phone:<br>
<asp:TextBox ID="UserPhone" Runat="server"></asp:TextBox><br>
Fax:<br>
<asp:TextBox ID="UserFax" Runat="server"></asp:TextBox><br>
Mobile:<br>
<asp:TextBox ID="UserMobile" Runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" HeaderText="User Management" CancelText="Cancel"
EditText="Edit Row"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" HeaderText="Delete User"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
Here is the codebehind for the event Delete_Click:
{
clubconn=new SqlConnection(strclubconn);
clubconn.Open();
clubcmd=new SqlCommand(Globals.UserDelete_SP,clubconn);
clubcmd.CommandType=CommandType.StoredProcedure;
clubcmd.Parameters.Add(Globals.User_tbl_UserID_PARM,e.Item.ItemIndex);
clubcmd.ExecuteNonQuery();
BindData();
clubconn.Close();
}

The Stored procedure in the Class Globals.UserDelete_SP:
CREATE PROCEDURE dbo.dt_UserDelete
(
@UserID int
)
AS
SET NOCOUNT ON
DELETE
FROM
User_tbl
WHERE
UserID=@UserID
return 0
GO

Globals.User_tbl_UserID_PARM is a referance to a class that returns the
following:

public const string User_tbl_UserID_PARM = "@UserID"

Everything looks fine but the grid is not passing an index value. Anyone
know why this is happening?
 
G

Guest

Everyone I fixed this problem on my own.
For anyone who might be facing the same issue as I just did all I had to do
to pass the primary key over to the delete command event was stuff an integer
variable with the datagrid Datakeys as follows:
int EditID=(int)clubdg.DataKeys[(int)e.Item.ItemIndex];

I hope this saves someone some time in the future. :)

Sam-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top