Gridview Delete Link返回" [ERROR]:数据源不支持删除' SqlDataSource1'除非指定了DeleteCommand。"

时间:2017-04-09 11:56:54

标签: c# asp.net gridview

我有一个预订系统,我的网格视图中的删除链接出现了问题,该链接应该删除临时保留的房间,它会返回一个错误页面,说明"数据源不支持删除' SqlDataSource1'除非指定了DeleteCommand。"但当我重新登录我使用的帐户时,数据被删除了。这是我的代码

ASP.net

<asp:GridView ID="cartGView" runat="server" DataKeyNames="ROOM_ID" AutoGenerateColumns="False" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" Width="100%" DataSourceID="SqlDataSource1" GridLines="Horizontal" OnRowDeleting="cartGView_RowDeleting">
                        <RowStyle HorizontalAlign ="Center" />
                        <Columns>
                            <asp:CommandField SelectText="Delete" ShowDeleteButton="True" />
                            <asp:BoundField DataField="ROOM_ID" HeaderText="Room ID" SortExpression="ROOM_ID" HeaderStyle-HorizontalAlign="Center">
                                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="ROOM_TYPE" HeaderText="Room Type" SortExpression="ROOM_TYPE" HeaderStyle-HorizontalAlign="Center">
                                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="NO_GUEST" HeaderText="Guest" SortExpression="NO_GUEST" HeaderStyle-HorizontalAlign="Center">
                                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="PRICE" HeaderText="Price" SortExpression="PRICE" HeaderStyle-HorizontalAlign="Center">
                                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="SUB_PRICE" HeaderText="Sub Price" SortExpression="SUB_PRICE" HeaderStyle-HorizontalAlign="Center">
                                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                            </asp:BoundField>
                        </Columns>
                        <EmptyDataTemplate>
                            <br></br><center>Select Room</center>
                            <br></br>
                        </EmptyDataTemplate>
                        <FooterStyle BackColor="White" ForeColor="#333333" />
                        <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F7F7F7" />
                        <SortedAscendingHeaderStyle BackColor="#487575" />
                        <SortedDescendingCellStyle BackColor="#E5E5E5" />
                        <SortedDescendingHeaderStyle BackColor="#275353" />
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Bakasyunan %>" SelectCommand="SELECT DISTINCT [ROOM_ID], [ROOM_TYPE], [PRICE], [SUB_PRICE], [NO_GUEST] FROM [TBL_MyCart] WHERE ([USERNAME] = @USERNAME)" OnSelecting="SqlDataSource1_Selecting">
                        <SelectParameters>
                            <asp:SessionParameter Name="USERNAME" SessionField="username" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>

C#:

protected void cartGView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            string roomid = Convert.ToString(cartGView.DataKeys[e.RowIndex].Values[0]);
            cd.DeleteRecordWalkCart(roomid);
            cartGView.DataSourceID = "SqlDataSource1";
            cartGView.DataBind();


            if (cartGView.Rows.Count == 0)
            {
                subtotalBox.Text = "P 0.00";
                continueBtn.Enabled = false;
                continueBtn.BackColor = System.Drawing.ColorTranslator.FromHtml("#2C2A2A");
            }
            else if (cartGView.Rows.Count > 0)
            {
                SqlCommand sumcmd = new SqlCommand("SELECT SUM(SUB_PRICE) FROM TBL_MyCart", conn); //<-- Add Subtotal
                conn.Open();
                string sum = sumcmd.ExecuteScalar().ToString();
                conn.Close();
                subtotalBox.Text = "P " + sum + ".00";
                continueBtn.Enabled = true;
                continueBtn.BackColor = System.Drawing.ColorTranslator.FromHtml("#6c0404");
            }
        }
        catch(Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }

任何帮助将不胜感激!谢谢!

0 个答案:

没有答案