找不到具有参数的非泛型方法“删除”

时间:2009-08-05 12:14:01

标签: asp.net

当使用绑定到ObjectDataSource的Gridview控件时,这是从.net返回的字符串。 ObjectDataSource绑定到.net DataSet中的tableAdapter。

数据集有一个自动生成的表适配器,并在我的数据库中创建了更新,插入,选择和删除存储过程。

网格现在正在使用此源,应允许插入,更新和删除。

插入和更新正在运行,但删除专门提供错误

ObjectDataSource'odsCustomerAliases'找不到具有参数的非泛型方法'Delete':CustomerAlias,original_CustomerAlias。

虽然我可以阅读错误,但我已经尝试了很多事情并且无法使其发挥作用。我无法真正看到它如何期待参数'original_CustomerAlias'

我可以确认proc中不存在此参数。

以下是一些看似正确的代码段。

<asp:ObjectDataSource ID="odsCustomerAliases" runat="server" DeleteMethod="Delete"
                InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
                TypeName="SLRDataAccess.dsTableAdapters.CustomerAliasesTableAdapter" UpdateMethod="Update">
                <DeleteParameters>
                    <asp:Parameter Name="CustomerAlias" Type="String" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="original_CustomerAlias" Type="String" />
                    <asp:Parameter Name="CustomerAlias" Type="String" />
                </UpdateParameters>
                <SelectParameters>
                    <asp:SessionParameter Name="CustomerID" SessionField="CustomerID" Type="Int32" />
                </SelectParameters>
                <InsertParameters>
                    <asp:Parameter Name="CustomerAlias" Type="String" />
                    <asp:Parameter Name="CustomerID" Type="Int32" />
                </InsertParameters>
            </asp:ObjectDataSource>

自动生成的数据集中的部分。

<DeleteCommand>
              <DbCommand CommandType="StoredProcedure" ModifiedByUser="False">
                <CommandText>dbo.usp_DeleteCustomerAlias</CommandText>
                <Parameters>
                  <Parameter AllowDbNull="True" AutogeneratedName="" DataSourceName="" DbType="Int32" Direction="ReturnValue" ParameterName="@RETURN_VALUE" Precision="10" ProviderType="Int" Scale="0" Size="4" SourceColumnNullMapping="False" SourceVersion="Current">
                  </Parameter>
                  <Parameter AllowDbNull="True" AutogeneratedName="" DataSourceName="" DbType="AnsiString" Direction="Input" ParameterName="@CustomerAlias" Precision="0" ProviderType="VarChar" Scale="0" Size="100" SourceColumn="CustomerAlias" SourceColumnNullMapping="False" SourceVersion="Current">
                  </Parameter>
                </Parameters>
              </DbCommand>
            </DeleteCommand>

我想象设计师的最终代码片段实际上并不相关,但是......

<Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
     Global.System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter"),  _
     Global.System.ComponentModel.DataObjectMethodAttribute(Global.System.ComponentModel.DataObjectMethodType.Delete, true)>  _
    Public Overloads Overridable Function Delete(ByVal CustomerAlias As String) As Integer
        If (CustomerAlias Is Nothing) Then
            Me.Adapter.DeleteCommand.Parameters(1).Value = Global.System.DBNull.Value
        Else
            Me.Adapter.DeleteCommand.Parameters(1).Value = CType(CustomerAlias,String)
        End If
        Dim previousConnectionState As Global.System.Data.ConnectionState = Me.Adapter.DeleteCommand.Connection.State
        If ((Me.Adapter.DeleteCommand.Connection.State And Global.System.Data.ConnectionState.Open)  _
                    <> Global.System.Data.ConnectionState.Open) Then
            Me.Adapter.DeleteCommand.Connection.Open
        End If
        Try 
            Dim returnValue As Integer = Me.Adapter.DeleteCommand.ExecuteNonQuery
            Return returnValue
        Finally
            If (previousConnectionState = Global.System.Data.ConnectionState.Closed) Then
                Me.Adapter.DeleteCommand.Connection.Close
            End If
        End Try
    End Function

4 个答案:

答案 0 :(得分:2)

属性OldValuesParameterFormatString存在问题 - 强制方法接受2个参数。只需删除它属性。

答案 1 :(得分:0)

我发生了同样的错误,我只是将OldValuesParameterFormatString属性的值从“original_ {0}”更改为“{0}”来解决它。

希望这有帮助。

答案 2 :(得分:0)

您必须确保参数名称在所有位置都匹配。 如果您看到此错误:ObjectDataSource找不到具有参数的非泛型方法。赔率是你的id在所有地方都不匹配。

以下是一个示例,请注意网格中使用的id“ScheduleId”,ods参数和数据访问功能

<asp:GridView ID="uxgvSchedule" DataKeyNames="ScheduleId" ... 

<asp:TemplateField HeaderText="">
    <ItemTemplate>
        <asp:LinkButton ID="lblDeleteSchedule" runat="server" CommandName="Delete" Text="Delete"  CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>" />
    </ItemTemplate>
</asp:TemplateField>

</asp:GridView>

<asp:ObjectDataSource ID="odsSchedule" DeleteMethod="DeleteSchedule" OnDeleting="odsSchedule_Deleting" ... />

<DeleteParameters>
    <asp:Parameter Name="ScheduleId" Type="Int32" />
</DeleteParameters>

Protected Sub uxgvSchedule_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles uxgvSchedule.RowCommand
Try
    Dim tempInt As Integer
    If Integer.TryParse(e.CommandArgument, tempInt) Then
        Dim row As GridViewRow = uxgvSchedule.Rows(Convert.ToInt32(e.CommandArgument))
        Dim rowkey As DataKey = uxgvSchedule.DataKeys(row.RowIndex)
        uxlblScheduleId.Text = rowkey.Value
    End If
Catch ex As Exception
End Try
End Sub

Protected Sub odsSchedule_Deleting(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) Handles odsSchedule.Deleting
    ************VERY COMMON CAUSE - FIX ******************
    'e.InputParameters("ScheduleId") = uxlblScheduleId.Text

    '2nd time i ran into this problem the error was indicating more 
    'than one parameter or a completely different set of parameters, 
    'so I would nearly do this instead from now on in this function
    e.InputParameters.Clear()
    e.InputParameters.Add("ScheduleId", uxlblScheduleId.Text)
End Sub

Public Function DeleteSchedule(ByVal ScheduleId As Integer) As Integer
    Dim rowsAffected As Integer = 0
    Dim con As SqlConnection = New SqlConnection(connectionString)
    Dim cmd As SqlCommand = New SqlCommand("DeleteSchedule", con)
    cmd.CommandType = CommandType.StoredProcedure

    cmd.Parameters.Add(New SqlParameter("@ScheduleId", SqlDbType.Int))
    cmd.Parameters("@ScheduleId").Value = ScheduleId

    Try
        con.Open()
        rowsAffected = cmd.ExecuteNonQuery()
    Catch sqlex As SqlException
    Catch ex As Exception
    Finally
        con.Close()
    End Try
    Return rowsAffected
End Function

答案 3 :(得分:0)

OldValuesParameterFormatString DeleteParameters`中定义ObjectDataSource' tag with the same name of

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    DeleteMethod="DeleteBook" SelectMethod="LoadAllBooks" **OldValuesParameterFormatString="ISBN"** 
    TypeName="BusinessLayer.clsBusiness">
    <DeleteParameters>
        <asp:Parameter Name="ISBN" Type="Int32" />
    </DeleteParameters>
</asp:ObjectDataSource>