我目前正在开发一个项目,该项目使用ObjectDataSources和GridView控件来运行一些数据库访问和更新。
我遇到的当前问题是,当单击gridview中的“更新”按钮时,会抛出异常错误。根据我的理解,这个错误似乎是说网格视图将许多参数传递给update语句。
我希望它发送2个对象,“orignal_Incident”和“incident”,它们应该包含我需要的所有字段。但它似乎也传递了“DateClosed”和“Description”字段。
是否有可能编辑gridview发送的参数?或者还有什么需要做才能使这项工作?
以下是与该部分程序有关的代码和错误:
错误:
ObjectDataSource'obsIncidents'找不到具有参数的非泛型方法'UpdateIncident':original_Incident,incident,DateClosed,Description。
ObjectDataSource的ASP.NET代码
<asp:ObjectDataSource ID="obsIncidents" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetCustomerIncidents" TypeName="IncidentDB" UpdateMethod="UpdateIncident">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCustomer" Name="CustomerID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="original_Incident" Type="Object" />
<asp:Parameter Name="incident" Type="Object" />
</UpdateParameters>
</asp:ObjectDataSource>
GridView的ASP.NET代码
<asp:GridView ID="gvIncidents" runat="server" AutoGenerateColumns="False" DataSourceID="obsIncidents" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="IncidentID" HeaderText="ID" ReadOnly="True" ControlStyle-Width="30" />
<asp:BoundField DataField="ProductCode" HeaderText="Product Code" ReadOnly="True" ControlStyle-Width="70" />
<asp:BoundField DataField="DateOpened" DataFormatString="{0:d}" HeaderText="Date Opened" ReadOnly="True" ControlStyle-Width="70" />
<asp:BoundField DataField="DateClosed" HeaderText="Date Closed" DataFormatString="{0:d}" ControlStyle-Width="70" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" ControlStyle-Width="150" />
<asp:TemplateField HeaderText="Description" ControlStyle-Width="300" >
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>' Width="300"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("Description") %>' Width="300" Rows="4" TextMode="MultiLine"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
类文件中的Visual Basic更新方法
<DataObjectMethod(DataObjectMethodType.Update)>
Public Shared Function UpdateIncident(
ByVal original_Incident As Incident,
ByVal incident As Incident) As Integer
Dim con As New SqlConnection(TechSupportDB.GetConnectionString)
Dim up As String = "UPDATE Incidents " &
"SET DateClosed = @DateClosed, " &
"Description = @Description " &
"WHERE IncidentID = @original_IncidentID " &
"AND ProductCode = @original_ProductCode " &
"AND DateOpened = @original_DateOpened " &
"AND (DateClosed = @original_DateClosed " &
"OR DateClosed IS NULL " &
"AND @original_DateClosed IS NULL) " &
"AND Title = @original_Title " &
"AND Description = @original_Description"
Dim cmd As New SqlCommand(up, con)
If incident.DateClosed = #12:00:00 AM# Then
cmd.Parameters.AddWithValue("DateClosed", DBNull.Value)
Else
cmd.Parameters.AddWithValue("DateClosed", incident.DateClosed)
End If
cmd.Parameters.AddWithValue("Description", incident.description)
cmd.Parameters.AddWithValue("original_IncidentID", original_Incident.IncidentID)
cmd.Parameters.AddWithValue("original_ProductCode", original_Incident.ProductCode)
cmd.Parameters.AddWithValue("original_DateOpened", original_Incident.DateOpened)
If original_Incident.DateClosed = #12:00:00 AM# Then
cmd.Parameters.AddWithValue("original_DateClosed", DBNull.Value)
Else
cmd.Parameters.AddWithValue("original_DateClosed", original_Incident.DateClosed)
End If
cmd.Parameters.AddWithValue("original_Title", original_Incident.title)
cmd.Parameters.AddWithValue("original_Description", original_Incident.description)
con.Open()
Dim i As Integer = cmd.ExecuteNonQuery()
con.Close()
Return i
End Function
答案 0 :(得分:0)
AzureShadow,
只需将OldValuesParameterFormatString =“original_ {0}”更改为OldValuesParameterFormatString =“{0}”。它应该完美!我认为您的案例和this case非常相似。看看那篇文章。很清楚。