嵌套Gridview Delete返回Unspecified错误

时间:2013-04-01 04:45:27

标签: asp.net gridview

我一直在寻找,但我找不到答案。以下代码有效,但“删除”按钮提供了Oledb未指定的错误。 我在EmptyTemplate上添加了一条新记录,后面显示的代码很好,但是当我在网格中记录并单击删除按钮时,给我一个错误。请帮忙。

 <asp:SqlDataSource ID="AllotDataSource" runat="server" ConnectionString='<%$ ConnectionStrings:BlastConnString %>' DeleteCommand="DELETE FROM [Allotment] WHERE [AllotID] = ?" InsertCommand="INSERT INTO [Allotment] ([AllotID], [LinkConvID], [LinkHotelID], [NumRooms], [DateIn], [DateOut], [Notes]) VALUES (?, ?, ?, ?, ?, ?, ?)" ProviderName='<%$ ConnectionStrings:BlastConnString.ProviderName %>' SelectCommand="SELECT dbo.Allotment.AllotID, dbo.Allotment.LinkConvID, dbo.Allotment.LinkHotelID, dbo.Allotment.NumRooms, dbo.Allotment.DateIn, dbo.Allotment.DateOut, dbo.Allotment.Notes, dbo.Hotels.Name, dbo.Hotels.Grade FROM dbo.Allotment INNER JOIN dbo.Hotels ON dbo.Allotment.LinkHotelID = dbo.Hotels.htID WHERE (dbo.Allotment.LinkConvID = CONVERT (varchar(36), ?))" UpdateCommand="UPDATE [Allotment] SET [LinkConvID] = ?, [LinkHotelID] = ?, [NumRooms] = ?, [DateIn] = ?, [DateOut] = ?, [Notes] = ? WHERE [AllotID] = ?">
                                    <DeleteParameters>
                                        <asp:Parameter Name="AllotID" Type="Object"></asp:Parameter>
                                    </DeleteParameters>
                                    <InsertParameters>
                                        <asp:Parameter Name="AllotID" Type="Object"></asp:Parameter>
                                        <asp:Parameter Name="LinkConvID" Type="Object"></asp:Parameter>
                                        <asp:Parameter Name="LinkHotelID" Type="Int32"></asp:Parameter>
                                        <asp:Parameter Name="NumRooms" Type="Int32"></asp:Parameter>
                                        <asp:Parameter Name="DateIn" Type="DateTime"></asp:Parameter>
                                        <asp:Parameter Name="DateOut" Type="DateTime"></asp:Parameter>
                                        <asp:Parameter Name="Notes" Type="String"></asp:Parameter>
                                    </InsertParameters>
                                    <SelectParameters>
                                        <asp:ControlParameter ControlID="ConvID" PropertyName="Value" Name="LinkConvID" Type="Object"></asp:ControlParameter>
                                    </SelectParameters>
                                    <UpdateParameters>
                                        <asp:Parameter Name="LinkConvID" Type="Object"></asp:Parameter>
                                        <asp:Parameter Name="LinkHotelID" Type="Int32"></asp:Parameter>
                                        <asp:Parameter Name="NumRooms" Type="Int32"></asp:Parameter>
                                        <asp:Parameter Name="DateIn" Type="DateTime"></asp:Parameter>
                                        <asp:Parameter Name="DateOut" Type="DateTime"></asp:Parameter>
                                        <asp:Parameter Name="Notes" Type="String"></asp:Parameter>
                                        <asp:Parameter Name="AllotID" Type="Object"></asp:Parameter>
                                    </UpdateParameters>
                                </asp:SqlDataSource>
                                <asp:GridView ID="AllotGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="AllotID" DataSourceID="AllotDataSource" OnRowCommand="AllotGridView_RowCommand">
                                    <Columns>
                                        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"></asp:CommandField>
                                        <asp:BoundField DataField="NumRooms" HeaderText="NumRooms" SortExpression="NumRooms"></asp:BoundField>
                                        <asp:BoundField DataField="DateIn" HeaderText="DateIn" SortExpression="DateIn"></asp:BoundField>
                                        <asp:BoundField DataField="DateOut" HeaderText="DateOut" SortExpression="DateOut"></asp:BoundField>
                                        <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes"></asp:BoundField>
                                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField>
                                        <asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade"></asp:BoundField>                                            
                                    </Columns>                                        
                                    <EmptyDataTemplate>
                                        <p style="font-style: italic">Add a new allotment:</p>
                                        <table style="font-size:small">
                                            <tr>
                                                <th>Hotel
                                                </th>
                                                <th># Rms
                                                </th>
                                                <th>From
                                                </th>
                                                <th>To
                                                </th>
                                                <th>Notes
                                                </th>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <asp:DropDownList ID="htlDDL" runat="server" DataSourceID="HtlDataSource" DataTextField="Name" DataValueField="htID"></asp:DropDownList>
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="NoRoomsBox" runat="server" Width="10px"></asp:TextBox>
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="DateInBox" runat="server" TextMode="Date"></asp:TextBox>
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="DateOutBox" runat="server" TextMode="Date"></asp:TextBox>
                                                </td>
                                                <td>
                                                    <asp:TextBox ID="NotesBox" runat="server" TextMode="MultiLine"></asp:TextBox>
                                                </td>
                                                <td>
                                                    <asp:Button ID="AddHtlBut" runat="server" CausesValidation="false" Text="Add" CssClass="button" CommandName="emptyInsert"></asp:Button>
                                                </td>
                                            </tr>
                                        </table>
                                    </EmptyDataTemplate>
                                </asp:GridView>

这是背后的代码:

  protected void AllotGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("emptyInsert"))
        {
            try
            {
                GridView childGrid = (GridView)sender;
                GridViewRow parentGridRow = (GridViewRow)childGrid.Parent.Parent;
                Control control = parentGridRow.Controls[0];
                string convID = (control.FindControl("ConvID") as HiddenField).Value;

                control = childGrid.Controls[0].Controls[0];
                string hotelID = (control.FindControl("htlDDL") as DropDownList).SelectedValue;
                string nRooms = (control.FindControl("NoRoomsBox") as TextBox).Text;
                string dateIn = (control.FindControl("DateInBox") as TextBox).Text;
                string dateOut = (control.FindControl("DateOutBox") as TextBox).Text;
                string note = (control.FindControl("NotesBox") as TextBox).Text;

                OleDbConnection Bconn = new OleDbConnection(ConfigurationManager.ConnectionStrings["BlastConnString"].ConnectionString);

                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandText = "INSERT INTO Allotment(LinkConvID, LinkHotelID, NumRooms, DateIn, DateOut, Notes) VALUES (?, ?, ?, ?, ?, ?)";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = Bconn;

                cmd.Parameters.Add("LinkConvID", OleDbType.VarChar).Value = convID;
                cmd.Parameters.Add("LinkHotelID", OleDbType.Integer, 4).Value = Convert.ToInt16(hotelID);
                cmd.Parameters.Add("NumRooms", OleDbType.Integer, 4).Value = Convert.ToInt16(nRooms);
                cmd.Parameters.Add("DateIn", OleDbType.Date).Value = Convert.ToDateTime(dateIn);
                cmd.Parameters.Add("DateOut", OleDbType.Date).Value = Convert.ToDateTime(dateOut);
                cmd.Parameters.Add("Notes", OleDbType.VarChar).Value = note;

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                Bconn.Dispose();
            }
            catch (OleDbException ex)
            {
                SQLErrorLabel.Text = ex.ToString();
            }
        }

        ConvGridView.DataBind();

    }
}

抱歉忘了包含错误消息:

[OleDbException(0x80004005):未指定错误]    System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior,String method)+745    System.Data.OleDb.OleDbCommand.ExecuteNonQuery()+ 107    System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand命令,DataSourceOperation操作)+380    System.Web.UI.WebControls.SqlDataSourceView.ExecuteDelete(IDictionary keys,IDictionary oldValues)+568    System.Web.UI.DataSourceView.Delete(IDictionary keys,IDictionary oldValues,DataSourceViewOperationCallback回调)+84    System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row,Int32 rowIndex)+930    System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e,Boolean causeValidation,String validationGroup)+974    System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument)+205    System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+13    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+13    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+9642898    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+1724

编辑: 我隔离了这个问题。我在这个表上创建了一个只有sqldatasource和基本GridView的空白页面,并且在Delete按钮上仍然显示相同的错误消息。我连接到MS SQL 2000并且必须使用OleDb ......这可能是个问题吗?我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:0)

我解决了。 sqldatasource无法删除MSSQL 2000的记录。我不得不使用ADO.NET并使用自定义删除按钮