Unable to delete using gridview

G

Guest

I am using ASP.Net 2.0 and have a gridview on my page. I have everything
working except the delete command. The page reloads except the row I am
trying to delete is still there. I believe it is something really easy, but I
cannot see it. The stored procedue works when run in QA. Can someone tell me
what I am doing wrong/missing that is keeping the delete command from working
in the gridview? Thank you.

I am trying to delete a row out of a line item table (ProjectLocationLI)
joining projects (projectid) and locations (locationid)

--ProjectLocationLI Table
CREATE TABLE [dbo].[ProjectLocationLI] (
[ProjectLocationLIID] [int] IDENTITY (1, 1) NOT NULL ,
[ProjectID] [int] NOT NULL ,
[LocationID] [int] NOT NULL
) ON [PRIMARY]
GO




--Gridview and datasource code

<asp:GridView ID="gvLocations" runat="server"
AutoGenerateColumns="False" CellPadding="4"
BorderColor="#5D7B9D" ForeColor="#333333" GridLines="None"
style="padding:1px; margin-top: 25px; margin-left: 10px;" Width="215px"
BorderStyle="Solid"
BorderWidth="1px"
DataKeyNames="LocationID" DataSourceID="SqlDataSource1">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ButtonType="Image"
CancelImageUrl="~/images/icons/canceledit_icon.gif"
DeleteImageUrl="~/images/icons/delete_icon.gif"
EditImageUrl="~/images/icons/edit_icon.gif" ShowEditButton="True"
UpdateImageUrl="~/images/icons/update_icon.gif" />
<asp:CommandField ButtonType="Image"
DeleteImageUrl="~/images/icons/delete_icon.gif"
DeleteText="" ShowDeleteButton="True" />
<asp:BoundField DataField="Name" HeaderText="Test Locations"
SortExpression="Name" />
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="LightGray" ForeColor="#284775" />
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CommonConnectionString %>"
SelectCommand="ListLocationsByProject"
SelectCommandType="StoredProcedure" DeleteCommand="DeleteLocationFromProject"
DeleteCommandType="StoredProcedure" UpdateCommand="UpdateLocation"
UpdateCommandType="StoredProcedure" InsertCommand="InsertLocation"
InsertCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter Name="projectID" SessionField="ProjectID"
Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:parameter Name="projectID" Type="int32" />
<asp:parameter Name="locationID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="locationID" Type="Int32" />
<asp:parameter Name="name" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="name" Type="String" />
</InsertParameters>
</asp:SqlDataSource>





--Stored Procedure being used
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


ALTER PROCEDURE DeleteLocationFromProject (@projectID int, @locationID int)
AS

DELETE FROM ProjectLocationLI
WHERE ProjectID = @projectID AND LocationID = @locationID


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
 
R

Rad [Visual C# MVP]

I think Your ButtonField for the delete button is incomplete. Add the
following:

CommandName="Delete"

such that it looks like this:

<asp:CommandField ButtonType="Image"
CommandName="Delete"
DeleteImageUrl="~/images/icons/delete_icon.gif"
DeleteText="" ShowDeleteButton="True" />


I am using ASP.Net 2.0 and have a gridview on my page. I have everything
working except the delete command. The page reloads except the row I am
trying to delete is still there. I believe it is something really easy, but I
cannot see it. The stored procedue works when run in QA. Can someone tell me
what I am doing wrong/missing that is keeping the delete command from working
in the gridview? Thank you.

I am trying to delete a row out of a line item table (ProjectLocationLI)
joining projects (projectid) and locations (locationid)

--ProjectLocationLI Table
CREATE TABLE [dbo].[ProjectLocationLI] (
[ProjectLocationLIID] [int] IDENTITY (1, 1) NOT NULL ,
[ProjectID] [int] NOT NULL ,
[LocationID] [int] NOT NULL
) ON [PRIMARY]
GO




--Gridview and datasource code

<asp:GridView ID="gvLocations" runat="server"
AutoGenerateColumns="False" CellPadding="4"
BorderColor="#5D7B9D" ForeColor="#333333" GridLines="None"
style="padding:1px; margin-top: 25px; margin-left: 10px;" Width="215px"
BorderStyle="Solid"
BorderWidth="1px"
DataKeyNames="LocationID" DataSourceID="SqlDataSource1">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ButtonType="Image"
CancelImageUrl="~/images/icons/canceledit_icon.gif"
DeleteImageUrl="~/images/icons/delete_icon.gif"
EditImageUrl="~/images/icons/edit_icon.gif" ShowEditButton="True"
UpdateImageUrl="~/images/icons/update_icon.gif" />
<asp:CommandField ButtonType="Image"
DeleteImageUrl="~/images/icons/delete_icon.gif"
DeleteText="" ShowDeleteButton="True" />
<asp:BoundField DataField="Name" HeaderText="Test Locations"
SortExpression="Name" />
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="LightGray" ForeColor="#284775" />
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CommonConnectionString %>"
SelectCommand="ListLocationsByProject"
SelectCommandType="StoredProcedure" DeleteCommand="DeleteLocationFromProject"
DeleteCommandType="StoredProcedure" UpdateCommand="UpdateLocation"
UpdateCommandType="StoredProcedure" InsertCommand="InsertLocation"
InsertCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter Name="projectID" SessionField="ProjectID"
Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:parameter Name="projectID" Type="int32" />
<asp:parameter Name="locationID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="locationID" Type="Int32" />
<asp:parameter Name="name" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="name" Type="String" />
</InsertParameters>
</asp:SqlDataSource>





--Stored Procedure being used
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


ALTER PROCEDURE DeleteLocationFromProject (@projectID int, @locationID int)
AS

DELETE FROM ProjectLocationLI
WHERE ProjectID = @projectID AND LocationID = @locationID


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
 
G

Guest

Thanks for the reply, but that did not work. I get an error "CommandName is
not a valid attribute for commandfield"

If it helps, it seems if I modify my stored procedure to only require one
parameter of the project id, it will delete the item. But that is for
deleting an entry in the location table itself and I just want to delete the
entry out of the line item table.

Rad said:
I think Your ButtonField for the delete button is incomplete. Add the
following:

CommandName="Delete"

such that it looks like this:

<asp:CommandField ButtonType="Image"
CommandName="Delete"
DeleteImageUrl="~/images/icons/delete_icon.gif"
DeleteText="" ShowDeleteButton="True" />


I am using ASP.Net 2.0 and have a gridview on my page. I have everything
working except the delete command. The page reloads except the row I am
trying to delete is still there. I believe it is something really easy, but I
cannot see it. The stored procedue works when run in QA. Can someone tell me
what I am doing wrong/missing that is keeping the delete command from working
in the gridview? Thank you.

I am trying to delete a row out of a line item table (ProjectLocationLI)
joining projects (projectid) and locations (locationid)

--ProjectLocationLI Table
CREATE TABLE [dbo].[ProjectLocationLI] (
[ProjectLocationLIID] [int] IDENTITY (1, 1) NOT NULL ,
[ProjectID] [int] NOT NULL ,
[LocationID] [int] NOT NULL
) ON [PRIMARY]
GO




--Gridview and datasource code

<asp:GridView ID="gvLocations" runat="server"
AutoGenerateColumns="False" CellPadding="4"
BorderColor="#5D7B9D" ForeColor="#333333" GridLines="None"
style="padding:1px; margin-top: 25px; margin-left: 10px;" Width="215px"
BorderStyle="Solid"
BorderWidth="1px"
DataKeyNames="LocationID" DataSourceID="SqlDataSource1">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ButtonType="Image"
CancelImageUrl="~/images/icons/canceledit_icon.gif"
DeleteImageUrl="~/images/icons/delete_icon.gif"
EditImageUrl="~/images/icons/edit_icon.gif" ShowEditButton="True"
UpdateImageUrl="~/images/icons/update_icon.gif" />
<asp:CommandField ButtonType="Image"
DeleteImageUrl="~/images/icons/delete_icon.gif"
DeleteText="" ShowDeleteButton="True" />
<asp:BoundField DataField="Name" HeaderText="Test Locations"
SortExpression="Name" />
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="LightGray" ForeColor="#284775" />
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CommonConnectionString %>"
SelectCommand="ListLocationsByProject"
SelectCommandType="StoredProcedure" DeleteCommand="DeleteLocationFromProject"
DeleteCommandType="StoredProcedure" UpdateCommand="UpdateLocation"
UpdateCommandType="StoredProcedure" InsertCommand="InsertLocation"
InsertCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter Name="projectID" SessionField="ProjectID"
Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:parameter Name="projectID" Type="int32" />
<asp:parameter Name="locationID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="locationID" Type="Int32" />
<asp:parameter Name="name" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="name" Type="String" />
</InsertParameters>
</asp:SqlDataSource>





--Stored Procedure being used
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


ALTER PROCEDURE DeleteLocationFromProject (@projectID int, @locationID int)
AS

DELETE FROM ProjectLocationLI
WHERE ProjectID = @projectID AND LocationID = @locationID


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
 
D

David Wier

At the place where the delete takes place in code - make sure both items in
the WHERE clause are getting populated, and then passed to the stored
procedure. Naturally, if they're not populated, they won't get there and
nothing will get deleted, which is what is happening, in your case.

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com


Wannabe said:
Thanks for the reply, but that did not work. I get an error "CommandName is
not a valid attribute for commandfield"

If it helps, it seems if I modify my stored procedure to only require one
parameter of the project id, it will delete the item. But that is for
deleting an entry in the location table itself and I just want to delete the
entry out of the line item table.

Rad said:
I think Your ButtonField for the delete button is incomplete. Add the
following:

CommandName="Delete"

such that it looks like this:

<asp:CommandField ButtonType="Image"
CommandName="Delete"
DeleteImageUrl="~/images/icons/delete_icon.gif"
DeleteText="" ShowDeleteButton="True" />


I am using ASP.Net 2.0 and have a gridview on my page. I have everything
working except the delete command. The page reloads except the row I am
trying to delete is still there. I believe it is something really easy, but I
cannot see it. The stored procedue works when run in QA. Can someone tell me
what I am doing wrong/missing that is keeping the delete command from working
in the gridview? Thank you.

I am trying to delete a row out of a line item table (ProjectLocationLI)
joining projects (projectid) and locations (locationid)

--ProjectLocationLI Table
CREATE TABLE [dbo].[ProjectLocationLI] (
[ProjectLocationLIID] [int] IDENTITY (1, 1) NOT NULL ,
[ProjectID] [int] NOT NULL ,
[LocationID] [int] NOT NULL
) ON [PRIMARY]
GO




--Gridview and datasource code

<asp:GridView ID="gvLocations" runat="server"
AutoGenerateColumns="False" CellPadding="4"
BorderColor="#5D7B9D" ForeColor="#333333" GridLines="None"
style="padding:1px; margin-top: 25px; margin-left: 10px;" Width="215px"
BorderStyle="Solid"
BorderWidth="1px"
DataKeyNames="LocationID" DataSourceID="SqlDataSource1">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ButtonType="Image"
CancelImageUrl="~/images/icons/canceledit_icon.gif"
DeleteImageUrl="~/images/icons/delete_icon.gif"
EditImageUrl="~/images/icons/edit_icon.gif" ShowEditButton="True"
UpdateImageUrl="~/images/icons/update_icon.gif" />
<asp:CommandField ButtonType="Image"
DeleteImageUrl="~/images/icons/delete_icon.gif"
DeleteText="" ShowDeleteButton="True" />
<asp:BoundField DataField="Name" HeaderText="Test Locations"
SortExpression="Name" />
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="LightGray" ForeColor="#284775" />
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CommonConnectionString %>"
SelectCommand="ListLocationsByProject"
SelectCommandType="StoredProcedure" DeleteCommand="DeleteLocationFromProject"
DeleteCommandType="StoredProcedure" UpdateCommand="UpdateLocation"
UpdateCommandType="StoredProcedure" InsertCommand="InsertLocation"
InsertCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter Name="projectID" SessionField="ProjectID"
Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:parameter Name="projectID" Type="int32" />
<asp:parameter Name="locationID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="locationID" Type="Int32" />
<asp:parameter Name="name" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="name" Type="String" />
</InsertParameters>
</asp:SqlDataSource>





--Stored Procedure being used
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


ALTER PROCEDURE DeleteLocationFromProject (@projectID int, @locationID int)
AS

DELETE FROM ProjectLocationLI
WHERE ProjectID = @projectID AND LocationID = @locationID


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
 
G

Guest

At the risk of sounding totally dumb, how can I check to make sure the
parameters are being populated? I am using the gridview control and I used
the smart tag and configured which stored prodedure to use through that and
did not write any code.

David Wier said:
At the place where the delete takes place in code - make sure both items in
the WHERE clause are getting populated, and then passed to the stored
procedure. Naturally, if they're not populated, they won't get there and
nothing will get deleted, which is what is happening, in your case.

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com


Wannabe said:
Thanks for the reply, but that did not work. I get an error "CommandName is
not a valid attribute for commandfield"

If it helps, it seems if I modify my stored procedure to only require one
parameter of the project id, it will delete the item. But that is for
deleting an entry in the location table itself and I just want to delete the
entry out of the line item table.

Rad said:
I think Your ButtonField for the delete button is incomplete. Add the
following:

CommandName="Delete"

such that it looks like this:

<asp:CommandField ButtonType="Image"
CommandName="Delete"
DeleteImageUrl="~/images/icons/delete_icon.gif"
DeleteText="" ShowDeleteButton="True" />


On Mon, 20 Nov 2006 09:07:02 -0800, Wannabe

I am using ASP.Net 2.0 and have a gridview on my page. I have everything
working except the delete command. The page reloads except the row I am
trying to delete is still there. I believe it is something really easy, but I
cannot see it. The stored procedue works when run in QA. Can someone tell me
what I am doing wrong/missing that is keeping the delete command from working
in the gridview? Thank you.

I am trying to delete a row out of a line item table (ProjectLocationLI)
joining projects (projectid) and locations (locationid)

--ProjectLocationLI Table
CREATE TABLE [dbo].[ProjectLocationLI] (
[ProjectLocationLIID] [int] IDENTITY (1, 1) NOT NULL ,
[ProjectID] [int] NOT NULL ,
[LocationID] [int] NOT NULL
) ON [PRIMARY]
GO




--Gridview and datasource code

<asp:GridView ID="gvLocations" runat="server"
AutoGenerateColumns="False" CellPadding="4"
BorderColor="#5D7B9D" ForeColor="#333333" GridLines="None"
style="padding:1px; margin-top: 25px; margin-left: 10px;" Width="215px"
BorderStyle="Solid"
BorderWidth="1px"
DataKeyNames="LocationID" DataSourceID="SqlDataSource1">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ButtonType="Image"
CancelImageUrl="~/images/icons/canceledit_icon.gif"
DeleteImageUrl="~/images/icons/delete_icon.gif"
EditImageUrl="~/images/icons/edit_icon.gif" ShowEditButton="True"
UpdateImageUrl="~/images/icons/update_icon.gif" />
<asp:CommandField ButtonType="Image"
DeleteImageUrl="~/images/icons/delete_icon.gif"
DeleteText="" ShowDeleteButton="True" />
<asp:BoundField DataField="Name" HeaderText="Test Locations"
SortExpression="Name" />
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="LightGray" ForeColor="#284775" />
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CommonConnectionString %>"
SelectCommand="ListLocationsByProject"
SelectCommandType="StoredProcedure" DeleteCommand="DeleteLocationFromProject"
DeleteCommandType="StoredProcedure" UpdateCommand="UpdateLocation"
UpdateCommandType="StoredProcedure" InsertCommand="InsertLocation"
InsertCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter Name="projectID" SessionField="ProjectID"
Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:parameter Name="projectID" Type="int32" />
<asp:parameter Name="locationID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="locationID" Type="Int32" />
<asp:parameter Name="name" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="name" Type="String" />
</InsertParameters>
</asp:SqlDataSource>





--Stored Procedure being used
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


ALTER PROCEDURE DeleteLocationFromProject (@projectID int, @locationID int)
AS

DELETE FROM ProjectLocationLI
WHERE ProjectID = @projectID AND LocationID = @locationID


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top