代码之家  ›  专栏  ›  技术社区  ›  Robert

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

  •  0
  • Robert  · 技术社区  · 15 年前

    这是在使用绑定到ObjectDataSource的Gridview控件时从.net返回的字符串。ObjectDataSource绑定到.net数据集中的tableAdapter。

    数据集有一个自动生成的表适配器,它在我的数据库中创建了一个update、insert、select和delete存储过程。

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

    ObjectDataSource“odscustomeralases”找不到具有以下参数的非泛型方法“Delete”:CustomerAlias、original_CustomerAlias。

    虽然我能读懂错误,但我已经尝试了很多事情,无法让它起作用。我真的看不出它是如何期望参数“original_CustomerAlias”

    下面是一些似乎是正确的代码片段。

    <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 回复  |  直到 15 年前
        1
  •  2
  •   Dewfy    15 年前

    属性OldValuesParameterFormatString有问题-它强制方法接受2个参数。完全删除它的属性。

        2
  •  0
  •   Mostafa Elmoghazi    14 年前

    希望这有帮助。

        3
  •  0
  •   Cla    13 年前

    必须确保参数名称在所有位置都匹配。 如果看到此错误:ObjectDataSource找不到具有参数的非泛型方法。很可能你的身份证在所有地方都不匹配。

    <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
    
        4
  •  0
  •   Mikhail    12 年前

    定义 OldValuesParameterFormatString 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>