Question

There are a few issues with this. right now, all I am trying to do is to get the delete command to work. The Edit doesnt work either, and guessing the issue is similar. This is a learning project for myself. Any help is greatly appreciated. Thank you.

 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" >
    <AlternatingRowStyle BackColor="White" />
    <EditRowStyle BackColor="#7C6F57" />

    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#E3EAEB" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F8FAFA" />
    <SortedAscendingHeaderStyle BackColor="#246B61" />
    <SortedDescendingCellStyle BackColor="#D4DFE1" />
    <SortedDescendingHeaderStyle BackColor="#15524A" />
    <Columns>

      <asp:BoundField DataField="MenuID" HeaderText="ID" Visible="true" ReadOnly="true" />

      <asp:TemplateField HeaderText="Date">
        <EditItemTemplate>
          <asp:TextBox ID="txtGridDate" runat="server" Text='<%# Bind("Date", "{0:M/dd/yyyy}")%>' Width="75px"></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
          <asp:Label ID="lblGridDate" runat="server" Text='<%# Bind("Date", "{0:M/dd/yyyy}") %>'></asp:Label>
        </ItemTemplate>
      </asp:TemplateField>

      <asp:TemplateField HeaderText="Description">
        <EditItemTemplate>
          <asp:TextBox ID="txtGridDescription" runat="server" Text='<%# Bind("Description") %>' Width="600px"></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
          <asp:Label ID="lblGridDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
        </ItemTemplate>
      </asp:TemplateField>

      <asp:TemplateField HeaderText="MealType">
        <EditItemTemplate>
          <asp:DropDownList ID="ddlGridMealType" runat="server">
            <asp:ListItem Value="1" Text="Breakfast"></asp:ListItem>
            <asp:ListItem Value="2" Text="Dinner"></asp:ListItem>
          </asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>
          <asp:Label ID="lblGridType" runat="server" Text='<%# Bind("MealType") %>'></asp:Label>
        </ItemTemplate>         
      </asp:TemplateField>

      <asp:CommandField ButtonType="Button" ShowEditButton="true" ValidationGroup="edit" />     
      <asp:CommandField ButtonType="Button" ShowDeleteButton="true" />
    </Columns>        
  </asp:GridView>
  <asp:SqlDataSource 
    ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CampRandolphConnectionString %>" 
    SelectCommand="sproc_sel_AllMeals" SelectCommandType="StoredProcedure" 
    InsertCommand="sproc_ins_Meal" InsertCommandType="StoredProcedure"
    UpdateCommand="sproc_edit_Meal" UpdateCommandType="StoredProcedure" 
    DeleteCommand="sproc_del_MenuItem" DeleteCommandType="StoredProcedure" >
    <InsertParameters>
      <asp:Parameter DbType="Date" Name="Date" />
      <asp:Parameter Name="Description" Type="String" />
      <asp:Parameter Name="MealCategoryID" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
      <asp:Parameter Name="MenuID" Type="Int32" />
      <asp:Parameter DbType="Date" Name="Date" />
      <asp:Parameter Name="Description" Type="String" />
      <asp:Parameter Name="MealCategoryID" Type="Int32" />
    </UpdateParameters>
    <DeleteParameters>
      <asp:Parameter Name="MenuID" Type="Int32" />
    </DeleteParameters>
  </asp:SqlDataSource>

Here is the Stored procedure:

ALTER PROCEDURE [dbo].[sproc_del_MenuItem]
@MenuID int
AS
BEGIN
DELETE FROM CampRandolph.dbo.Menu
WHERE @MenuID = MenuID
END

Thanks in advance.

Was it helpful?

Solution

Your Where clause is wrong in your stored procedure. Should be:

ALTER PROCEDURE [dbo].[sproc_del_MenuItem]
@MenuID int
AS
BEGIN
DELETE FROM CampRandolph.dbo.Menu
WHERE MenuID = @MenuID
END

In your definition of the GridView add DataKeyNames property:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="MenuID" ....

I also read that the HeaderText needs to be the same as key name:

 <asp:BoundField DataField="MenuID" HeaderText="MenuID" Visible="true" ReadOnly="true" />

OTHER TIPS

you are user autogenerated column set to be false and you also using

<asp:BoundField DataField="MenuID" HeaderText="ID" Visible="true" ReadOnly="true" />

instead of this you use :-

<asp:TemplateField>
<ItemTemplate>
<asp:TextBox id="menuidtxt" runat="server" Text='<%# Eval("MenuID")%>'/>
</ItemTemplate>
</asp:TemplateField>

now will get your menuid back. hope this works

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top