0

我一直在寻找一段时间,但我找不到答案。以下代码有效,但删除按钮给出了 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 行为,字符串方法) +745 System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +107 System.Web.UI.WebControls.SqlDataSourceView。 ExecuteDbCommand(DbCommand 命令,DataSourceOperation 操作)+380 System.Web.UI.WebControls.SqlDataSourceView.ExecuteDelete(IDictionary 键,IDictionary oldValues)+568 System.Web.UI.DataSourceView.Delete(IDictionary 键,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(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)+1724布尔型 includeStagesAfterAsyncPoint) +1724布尔型 includeStagesAfterAsyncPoint) +1724

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

4

1 回答 1

0

我修复它。sqldatasource 无法使用 MSSQL 2000 删除记录。我必须使用 ADO.NET 并使用自定义删除按钮

于 2013-04-06T05:14:03.873 回答