在ASP.NET中使用ObjectDataSource更新GridView
我目前正在从事一个项目,该项目使用ObjectDataSources和GridView控件来运行一些数据库访问和更新 我遇到的当前问题是,当单击gridview中的Update按钮时,会抛出异常错误。据我所知,这个错误似乎是说网格视图正在向update语句传递许多参数 我希望它发送两个对象,“orignal_事件”和“事件”,它们应该包含我需要的所有字段。但它似乎也在传递“DateClosed”和“Description”字段 是否有可能编辑gridview发送的参数?或者还有什么别的事情需要做才能使这项工作成功 以下是与此部分程序相关的代码和错误: 错误: ObjectDataSource“obsIncidents”找不到具有以下参数的非泛型方法“UpdateIncident”:原始事件、事件、日期关闭、描述 ObjectDataSource的ASP.NET代码在ASP.NET中使用ObjectDataSource更新GridView,asp.net,vb.net,gridview,sql-update,objectdatasource,Asp.net,Vb.net,Gridview,Sql Update,Objectdatasource,我目前正在从事一个项目,该项目使用ObjectDataSources和GridView控件来运行一些数据库访问和更新 我遇到的当前问题是,当单击gridview中的Update按钮时,会抛出异常错误。据我所知,这个错误似乎是说网格视图正在向update语句传递许多参数 我希望它发送两个对象,“orignal_事件”和“事件”,它们应该包含我需要的所有字段。但它似乎也在传递“DateClosed”和“Description”字段 是否有可能编辑gridview发送的参数?或者还有什么别的事情需要做
<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
公共共享函数更新事件(
ByVal original_事件作为事件,
ByVal事件作为事件)作为整数
Dim con作为新的SqlConnection(TechSupportDB.GetConnectionString)
暗显为String=“更新事件”&
SET DateClosed=@DateClosed&
“Description=@Description”&
“其中IncidentID=@original_IncidentID”&
“和ProductCode=@original_ProductCode”&
“AND DateOpened=@original_DateOpened”&
“和(DateClosed=@original_DateClosed”&
“或DateClosed为空”&
“且@original_DateClosed为空)”&
“和标题=@原始标题”&
“和描述=@原始描述”
Dim cmd作为新的SqlCommand(向上,向上)
如果incident.DateClosed=#12:00:00 AM#那么
cmd.Parameters.AddWithValue(“DateClosed”,DBNull.Value)
其他的
cmd.Parameters.AddWithValue(“DateClosed”,incident.DateClosed)
如果结束
cmd.Parameters.AddWithValue(“说明”,事件说明)
cmd.Parameters.AddWithValue(“原始事件”IncidentID,“原始事件”IncidentID)
cmd.Parameters.AddWithValue(“原始产品代码”,原始事件.ProductCode)
cmd.Parameters.AddWithValue(“原始\u DateOpened”,原始\u Incident.DateOpened)
如果原始事件日期关闭=#12:00:00 AM#则
cmd.Parameters.AddWithValue(“原始_DateClosed”,DBNull.Value)
其他的
cmd.Parameters.AddWithValue(“原始事件日期关闭”,原始事件日期关闭)
如果结束
cmd.Parameters.AddWithValue(“原始标题”,原始事件标题)
cmd.Parameters.AddWithValue(“原始描述”,原始事件描述)
con.Open()
Dim i As Integer=cmd.ExecuteNonQuery()
con.Close()
返回i
端函数
AzureShadow
只需将OldValuesParameterFormatString=“原始{0}”更改为OldValuesParameterFormatString=“{0}”。它应该工作得很好!我认为你的情况和我的情况很相似。你也可以看看那篇文章。这将是非常清楚的