473,769 Members | 5,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is wrong with my DataGrid?

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.ItemInd ex 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="U serID" runat="server"
GridLines="Vert ical" CellPadding="2"
BorderWidth="1p x" BorderColor="#C CCCFF" CssClass="txt"
AutoGenerateCol umns="False" OnCancelCommand ="Cancel_Cli ck"
OnUpdateCommand ="Update_Cli ck" OnDeleteCommand ="Delete_Cli ck"
OnEditCommand=" Edit_Click">
<SelectedItemSt yle VerticalAlign=" Top"></SelectedItemSty le>
<EditItemStyl e VerticalAlign=" Top"
BackColor="#000 033"></EditItemStyle>
<AlternatingIte mStyle VerticalAlign=" Top"></AlternatingItem Style>
<ItemStyle VerticalAlign=" Top"></ItemStyle>
<HeaderStyle Font-Size="10px" Font-Names="Verdana"
Font-Bold="True" HorizontalAlign ="Center"
VerticalAlign=" Middle" BackColor="#000 044"></HeaderStyle>
<Columns>
<asp:TemplateCo lumn HeaderText="Sta ff Name">
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,"UserPrefi x")%>
<%# DataBinder.Eval (Container.Data Item,"UserFName ")%>
<%# DataBinder.Eval (Container.Data Item,"UserLName ")%>
<BR>
Organization
ID: <%#DataBinder.E val(Container.D ataItem,"ClubID ")%>
</ItemTemplate>
<EditItemTempla te>
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>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Use r Access">
<ItemTemplate >
Email Address:<br>
<%# DataBinder.Eval (Container.Data Item, "UserEmail" )%>
<br>
Password: <%#
DataBinder.Eval (Container.Data Item,"UserPassw ord")%><br>
User Role: <%# DataBinder.Eval (Container.Data Item,
"AccessID")%><b r>
<br>
<br>
Show: <%# DataBinder.Eval (Container.Data Item,"show")%>< br>
Booking: <%#
DataBinder.Eval (Container.Data Item,"Bookings" )%><br>
General: <%#
DataBinder.Eval (Container.Data Item,"General") %><br>
Guest List: <%#
DataBinder.Eval (Container.Data Item,"GuestList ")%>
</ItemTemplate>
<EditItemTempla te>
User Email:<br>
<asp:TextBox ID="UserEmail" Runat="server"> </asp:TextBox><br >
User Password:<br>
<asp:TextBox ID="UserPasswor d"
Runat="server"> </asp:TextBox><br >
User Role:<br>
<asp:DropDownLi st ID="AccessID" Runat="server">
<asp:ListItem Selected="True" >Select Role</asp:ListItem>
<asp:ListItem Value="Admin">A dministrator</asp:ListItem>
<asp:ListItem Value="Promotor ">Promotor</asp:ListItem>
<asp:ListItem Value="General Staff">General
Staff</asp:ListItem>
</asp:DropDownLis t><br>
Show On Contacts:
<asp:CheckBox ID="show" Runat="server"> </asp:CheckBox><b r>
<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>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Sta ff Address">
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,"UserStree tNumber")%>
<%# DataBinder.Eval (Container.Data Item, "UserAddress")% ><br>
<%# DataBinder.Eval (Container.Data Item,"UserCity" )%>
, <%#
DataBinder.Eval (Container.Data Item,"UserProvi nce")%><br />
<%# DataBinder.Eval (Container.Data Item,"UserPosta l")%>
</ItemTemplate>
<EditItemTempla te>
Street Number:
<asp:TextBox ID="UserStreetN umber" 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="UserProvinc e"
Runat="server"> </asp:TextBox><br >
Postal Code:<br>
<asp:TextBox ID="UserPostal " Runat="server"> </asp:TextBox>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Con tact Information">
<ItemTemplate >
Phone: <%#
DataBinder.Eval (Container.Data Item,"UserPhone ","{0:###-###-####}")%><br>
Fax: <%#
DataBinder.Eval (Container.Data Item,"UserFax", "{0:###-###-####}")%><br>
Mobile: <%#
DataBinder.Eval (Container.Data Item,"UserMobil e","{0:###-###-####}")%><br>
</ItemTemplate>
<EditItemTempla te>
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>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:EditComman dColumn ButtonType="Lin kButton"
UpdateText="Upd ate" HeaderText="Use r Management" CancelText="Can cel"
EditText="Edit Row"></asp:EditCommand Column>
<asp:ButtonColu mn Text="Delete" HeaderText="Del ete User"
CommandName="De lete"></asp:ButtonColum n>
</Columns>
</asp:datagrid>
Here is the codebehind for the event Delete_Click:
{
clubconn=new SqlConnection(s trclubconn);
clubconn.Open() ;
clubcmd=new SqlCommand(Glob als.UserDelete_ SP,clubconn);
clubcmd.Command Type=CommandTyp e.StoredProcedu re;
clubcmd.Paramet ers.Add(Globals .User_tbl_UserI D_PARM,e.Item.I temIndex);
clubcmd.Execute NonQuery();
BindData();
clubconn.Close( );
}

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

Globals.User_tb l_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?
Nov 19 '05 #1
1 1449
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)clu bdg.DataKeys[(int)e.Item.Ite mIndex];

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

Sam-
"I am Sam" wrote:
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.ItemInd ex 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="U serID" runat="server"
GridLines="Vert ical" CellPadding="2"
BorderWidth="1p x" BorderColor="#C CCCFF" CssClass="txt"
AutoGenerateCol umns="False" OnCancelCommand ="Cancel_Cli ck"
OnUpdateCommand ="Update_Cli ck" OnDeleteCommand ="Delete_Cli ck"
OnEditCommand=" Edit_Click">
<SelectedItemSt yle VerticalAlign=" Top"></SelectedItemSty le>
<EditItemStyl e VerticalAlign=" Top"
BackColor="#000 033"></EditItemStyle>
<AlternatingIte mStyle VerticalAlign=" Top"></AlternatingItem Style>
<ItemStyle VerticalAlign=" Top"></ItemStyle>
<HeaderStyle Font-Size="10px" Font-Names="Verdana"
Font-Bold="True" HorizontalAlign ="Center"
VerticalAlign=" Middle" BackColor="#000 044"></HeaderStyle>
<Columns>
<asp:TemplateCo lumn HeaderText="Sta ff Name">
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,"UserPrefi x")%>
<%# DataBinder.Eval (Container.Data Item,"UserFName ")%>
<%# DataBinder.Eval (Container.Data Item,"UserLName ")%>
<BR>
Organization
ID: <%#DataBinder.E val(Container.D ataItem,"ClubID ")%>
</ItemTemplate>
<EditItemTempla te>
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>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Use r Access">
<ItemTemplate >
Email Address:<br>
<%# DataBinder.Eval (Container.Data Item, "UserEmail" )%>
<br>
Password: <%#
DataBinder.Eval (Container.Data Item,"UserPassw ord")%><br>
User Role: <%# DataBinder.Eval (Container.Data Item,
"AccessID")%><b r>
<br>
<br>
Show: <%# DataBinder.Eval (Container.Data Item,"show")%>< br>
Booking: <%#
DataBinder.Eval (Container.Data Item,"Bookings" )%><br>
General: <%#
DataBinder.Eval (Container.Data Item,"General") %><br>
Guest List: <%#
DataBinder.Eval (Container.Data Item,"GuestList ")%>
</ItemTemplate>
<EditItemTempla te>
User Email:<br>
<asp:TextBox ID="UserEmail" Runat="server"> </asp:TextBox><br >
User Password:<br>
<asp:TextBox ID="UserPasswor d"
Runat="server"> </asp:TextBox><br >
User Role:<br>
<asp:DropDownLi st ID="AccessID" Runat="server">
<asp:ListItem Selected="True" >Select Role</asp:ListItem>
<asp:ListItem Value="Admin">A dministrator</asp:ListItem>
<asp:ListItem Value="Promotor ">Promotor</asp:ListItem>
<asp:ListItem Value="General Staff">General
Staff</asp:ListItem>
</asp:DropDownLis t><br>
Show On Contacts:
<asp:CheckBox ID="show" Runat="server"> </asp:CheckBox><b r>
<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>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Sta ff Address">
<ItemTemplate >
<%# DataBinder.Eval (Container.Data Item,"UserStree tNumber")%>
<%# DataBinder.Eval (Container.Data Item, "UserAddress")% ><br>
<%# DataBinder.Eval (Container.Data Item,"UserCity" )%>
, <%#
DataBinder.Eval (Container.Data Item,"UserProvi nce")%><br />
<%# DataBinder.Eval (Container.Data Item,"UserPosta l")%>
</ItemTemplate>
<EditItemTempla te>
Street Number:
<asp:TextBox ID="UserStreetN umber" 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="UserProvinc e"
Runat="server"> </asp:TextBox><br >
Postal Code:<br>
<asp:TextBox ID="UserPostal " Runat="server"> </asp:TextBox>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Con tact Information">
<ItemTemplate >
Phone: <%#
DataBinder.Eval (Container.Data Item,"UserPhone ","{0:###-###-####}")%><br>
Fax: <%#
DataBinder.Eval (Container.Data Item,"UserFax", "{0:###-###-####}")%><br>
Mobile: <%#
DataBinder.Eval (Container.Data Item,"UserMobil e","{0:###-###-####}")%><br>
</ItemTemplate>
<EditItemTempla te>
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>
</EditItemTemplat e>
</asp:TemplateCol umn>
<asp:EditComman dColumn ButtonType="Lin kButton"
UpdateText="Upd ate" HeaderText="Use r Management" CancelText="Can cel"
EditText="Edit Row"></asp:EditCommand Column>
<asp:ButtonColu mn Text="Delete" HeaderText="Del ete User"
CommandName="De lete"></asp:ButtonColum n>
</Columns>
</asp:datagrid>
Here is the codebehind for the event Delete_Click:
{
clubconn=new SqlConnection(s trclubconn);
clubconn.Open() ;
clubcmd=new SqlCommand(Glob als.UserDelete_ SP,clubconn);
clubcmd.Command Type=CommandTyp e.StoredProcedu re;
clubcmd.Paramet ers.Add(Globals .User_tbl_UserI D_PARM,e.Item.I temIndex);
clubcmd.Execute NonQuery();
BindData();
clubconn.Close( );
}

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

Globals.User_tb l_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?

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
22845
by: Joe Bloggs | last post by:
Anyone know why the code below does not work. All I want is to check a checkbox, then hit a button, then for the value of the DataKeyField in the DataGrid to show up as the Text property in a Label. The DataGrid is working OK as if the foreach loop, the bit where its coming unstuck is the check box definition or test to see if it is checked. I don't get a compilation error - just that it doesn't work. <asp:DataGrid id="DataGrid1"...
2
2711
by: Aaron Ackerman | last post by:
I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried everything and I am at a loss. The using goes into add mode with the add button adds his data then updates with the update button, seems simple. I am using ALL visual controls (supposedly to simplify things. If I was not using the visual controls and calling an ExecuteNonQuery no prob. Please look at my code and tell me what I am doing wrong. Also, what are the advatages...
4
1229
by: Kristoffer Arfvidson | last post by:
Im trying to get access to a table in codebehind, because the information in this table is different depending on what it says in the database... So, either I have to access it from codebehind, or Ill have to make bit of code on the aspx page... However, I would prefere code in the aspx page at the moment, but it seemed easier to access thecode from codebehind... However... When I run the following code, (when I look at the code it...
7
1852
by: David Laub | last post by:
I have stumbled across various Netscape issues, none of which appear to be solvable by tweaking the clientTarget or targetSchema properties. At this point, I'm not even interested in "solving" these problems - I'm more interested in isolating them, .i.e. finding a "complete" list of issues. Here's my list of serious issues found so far. By serious, I mean functionality that fails, as opposed to much less serious (albeit annoying) display...
0
1312
by: Solomon Shaffer | last post by:
This is very interesting - and odd. I have a number of LinkButtons that are created in a custom data grid that I created. The purpose of the LinkButtons is to provide alphabetical filtering functionality on the grid. Therefore, a LinkButton is added for every letter of the alphabet. I also have a numeric filter button and an "All" button that removes the filter. All of the LinkButtons are wired up to the same event called...
4
2619
by: Aaron Smith | last post by:
Dim dv As DataView = New DataView(FacilitiesDS1.Facilities, "", "ID ASC", DataViewRowState.CurrentRows) Dim iPos As Integer = dv.Find(dr.Item("ID")) Me.BindingContext(FacilitiesDS1, "Facilities").Position = iPos That is the code.. dr is DataRow. If dr.Item("ID") = 3, the find will return position 0, which it should have been 1, and if the ID = 2, it will return 1, which should have been 0.. The DA has a connection string that sorts it...
1
2099
by: fiaolle | last post by:
Hi The first set of source code is the class for a combobox in a grid, hopefully. In the second set of code we try to use the combobox class, but the grid is empty. I don't understand how this works. The first set of code I downloaded from internet and when i tried it, it worked fine. But when I changed the second part of the code it started to give me trouble. Before I changed the code they used Datacolumns,Datarows and Datatables and...
0
1334
by: Kim | last post by:
Im having a problem with one of my datagrids. Basically the datagrid has paging included in it and when a user clicks on page other than the first page and the user selects a row the value is stored but the problem is the value is wrong its still getting the value from the first page. I have view state enabled on both page and datagrid. Im not sure whats wrong, any help will be greatly appreciated
0
1535
by: arlie_maija | last post by:
Hey - I'm writing a control that contains a DataGrid, and I'm unable to get the update event to fire. When I click the update link, the edit event fires. heres the details... my control overrides CreateChildControls and dynamically creates the DataGrid, creates an EditCommandColumn which it adds to the
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.