我一直在尋找一段時間,但我找不到答案。下面的代碼工作,但刪除按鈕給出一個Oledb未指定的錯誤。 我在EmptyTemplate上添加了一個新記錄,其代碼如下所示,工作正常,但是當我在網格中記錄並單擊Delete按鈕時,會給我一個錯誤。請幫忙。嵌套Gridview刪除正在返回未指定錯誤
<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行爲,String方法)+745 System.Data.OleDb.OleDbCommand.ExecuteNonQuery()+10 System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand命令,DataSour (IDictionary鍵,IDictionary oldValues,DataSourceViewOperationCallback回調)+880 System.Web.UI.WebControls.SqlDataSourceView.ExecuteDelete(IDictionary鍵,IDictionary oldValues) .Web.UI.WebControls.GridView.HandleDelete(GridViewRow行,Int32 rowIndex)+930 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e,Boolean causesValidation,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
編輯: 我孤立的問題。我在這個表上只創建了一個只有sqldatasource和基本GridView的空白頁面,而在Delete按鈕上仍然有相同的錯誤消息。我連接到MS SQL 2000,必須使用OleDb ...可以這是一個問題?我能做些什麼來解決它?
請提及錯誤文本。 –
對不起,我忘了。見上面我剛剛添加它。 – emadev
此網格位於父網格視圖的單元格內。這可能是問題嗎?任何人? – emadev