【问题标题】:could not find a non-generic method 'Delete' that has parameters找不到具有参数的非泛型方法“删除”
【发布时间】:2012-12-23 05:17:33
【问题描述】:

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

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

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

插入和更新工作正常,但删除特别给出错误

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

虽然我可以阅读错误,但我已经尝试了很多方法,但无法使其正常工作。我真的看不出它是如何期待一个参数“original_CustomerAlias”

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

这里有一些似乎是正确的代码 sn-ps。

<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>

我认为设计师的最终代码 sn-p 实际上并不相关,但是...

<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

【问题讨论】:

  • 你发现objectdatasource删除事件的参数有多少?
  • 你有没有把 OldValuesParameterFormatString="original_{0}" 改成 OldValuesParameterFormatString="{0}"

标签: asp.net


【解决方案1】:

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

【讨论】:

  • 然后按预期添加这个虚构的删除
  • 我确实将预期的参数添加到存储的过程中,但这没有任何区别。但是标记显示删除语句无论如何都不需要这个参数,而不是在数据集设计器或页面上。很多人都遇到了这个错误,并且实际上不需要更改 oldvaluesparameterformatstring,因为无论如何该对象都应该保持旧的和新的。也许我应该将 delete 参数更改为 original_ 版本并尝试过,但我只是编写了自己的代码,并取消了命中和未命中自动生成的类。
【解决方案2】:

同样的错误发生在我身上,我通过将 OldValuesParameterFormatString 属性的值从“original_{0}”更改为“{0}”来解决它。

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    您必须确保您的参数名称在所有位置都匹配。 如果您看到此错误: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
    

    【讨论】:

      【解决方案4】:

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

      <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>
      

      【讨论】:

        猜你喜欢
        • 2013-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多