ObjectDataSource控件执行Delete操作时,出现“未能找到带参数的非泛型方法”的解决方案...

使用ObjectDataSource对象执行GridView数据的删除操作时,经常会遇到“未能找到带参数的非泛型方法”的错误,后面是一个参数列表。这个问题困扰我很长时间,今天浏览Studio帮助文档时,让我解决了这个问题。多云转晴了,呵呵!
1、问题陈述。
首先让我们看一下出现问题的程序代码吧!这个是ASPX页面的代码
ContractedBlock.gif ExpandedBlockStart.gif Code
 1<asp:GridView ID="gvwTopics" runat="server" AutoGenerateColumns="False" DataSourceID="objTopics" DataKeyNames="ID" BackColor="White" BorderColor="#404040" BorderStyle="None" BorderWidth="1px" CellPadding="8" GridLines="Horizontal" AllowPaging="True" Width="100%" OnRowCreated="gvwTopics_RowCreated" OnRowCommand="gvwTopics_RowCommand" OnRowDeleting="gvwTopics_RowDeleting">
 2                <Columns>
 3                    <asp:BoundField DataField="ID" HeaderText="编号" SortExpression="ID" >                    
 4                        <ItemStyle HorizontalAlign="Center" Width="10%" />
 5                        <HeaderStyle HorizontalAlign="Center" Width="10%" />
 6                    </asp:BoundField>
 7                    <asp:TemplateField HeaderText="标题" SortExpression="Title">
 8                        <ItemTemplate>
 9                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
10                        </ItemTemplate>
11                        <ItemStyle HorizontalAlign="Left" Width="45%" />
12                        <HeaderStyle HorizontalAlign="Center" Width="45%" />
13                    </asp:TemplateField>
14                    <asp:TemplateField HeaderText="当前主题" SortExpression="IsCurrent">
15                        <ItemTemplate>
16                            <asp:Image ID="image1" runat="Server" ImageUrl="~/Images/Checkmark.gif"  Visible='<%# Eval("IsCurrent") %>' />
17                        </ItemTemplate>
18                        <ItemStyle HorizontalAlign="Center" Width="15%" />
19                        <HeaderStyle HorizontalAlign="Center" Width="15%" />
20                    </asp:TemplateField>
21                    <asp:TemplateField HeaderText="创建日期" SortExpression="AddedDate">
22                        <ItemTemplate>
23                            <asp:Label ID="Label3" runat="server" Text='<%# ((DateTime)Eval("AddedDate")).ToShortDateString() %>'></asp:Label>
24                        </ItemTemplate>
25                        <ItemStyle HorizontalAlign="Center" Width="20%" />
26                        <HeaderStyle HorizontalAlign="Center" Width="20%" />
27                    </asp:TemplateField>
28                    <asp:TemplateField ShowHeader="False">
29                        <ItemTemplate>
30                            <asp:ImageButton ID="ibnSelect" runat="server" ImageUrl="~/Images/Edit.gif" CommandName="Select" />
31                            &nbsp;&nbsp;<asp:ImageButton ID="ibnDelete" runat="Server" ImageUrl="~/Images/Delete.gif" CommandName="Delete" CommandArgument='<%# Eval("ID") %>' />
32                        </ItemTemplate>
33                        <ItemStyle HorizontalAlign="Center" Width="10%" />
34                        <HeaderStyle Width="10%" />
35                    </asp:TemplateField>
36                </Columns>
37                <EmptyDataTemplate>
38                    No Records to Display!
39                </EmptyDataTemplate>
40                <PagerTemplate>
41                    <br />
42                    <asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1)  + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>
43                    <asp:LinkButton ID="lbnFirst" runat="Server" Text="首页"  Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First" ></asp:LinkButton>
44                    <asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="Prev"  ></asp:LinkButton>
45                    <asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next" ></asp:LinkButton>
46                    <asp:LinkButton ID="lbnLast" runat="Server" Text="尾页"   Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Last" ></asp:LinkButton>
47                </PagerTemplate>
48                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
49                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
50                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
51                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
52                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
53                <AlternatingRowStyle BackColor="#F7F7F7" />
54            </asp:GridView>            
55        <asp:ObjectDataSource ID="objTopics" runat="server" DeleteMethod="DeleteTopic"
56            SelectMethod="GetTopics" TypeName="ETP.BLL.Topic">
57            <DeleteParameters>
58                <asp:Parameter Name="topicID" Type="Int32"  />
59            </DeleteParameters>
60            <SelectParameters>
61                <asp:Parameter DefaultValue="false" Name="includeArchived" Type="Boolean" />
62            </SelectParameters>
63        </asp:ObjectDataSource>
这是我的Topic类中的DeleteTopic方法:
1    public   static   bool  DeleteTopic( int  topicID)
2  {
3       bool  ret  =  SiteProvider.Polls.DeleteTopic(topicID);
4 
5       return  ret;
6  }
错误提示如下图所示:


2、错误分析:

查阅了一下VS Studio 帮助文档,找到了下面这段话:


参数合并
参数从三个来源添加到 DeleteParameters 集合中:

从 数据绑定控件(在运行时)
从 DeleteParameters 元素(以声明方式)。

从 Deleting 方法(以声明方式)。

首先,将通过数据绑定控件生成的所有参数添加到 DeleteParameters 集合中。例如,如果 ObjectDataSource 控件绑定到具有列 Name 和 Number 的 GridView 控件,则将 Name 和 Number 的参数添加到集合中。参数的确切名称取决于 OldValuesParameterFormatString 属性。这些参数的数据类型是 string。然后,添加 DeleteParameters 元素中列出的参数。如果 DeleteParameters 元素中的参数与 DeleteParameters 集合中的现有参数同名,则修改现有参数以与 DeleteParameters 元素中指定的参数匹配。这通常用于修改参数中的数据类型。最后,可以在 Deleting 事件中以编程方式添加和移除参数,该事件在 Delete 方法运行前发生。合并参数后解析方法。下一节讨论方法解析。

方法解析
调用 Delete 方法时,数据绑定控件中的数据字段、DeleteParameters 元素中以声明方式创建的参数和 Deleting 事件处理程序中添加的参数全部合并在一起。(有关更多信息,请参见上一节。)然后,ObjectDataSource 对象尝试查找可以调用的方法。首先,它查找具有 DeleteMethod 属性中指定的名称的一个或多个方法。如果没有找到匹配项,则引发 InvalidOperationException 异常。如果找到了匹配项,它随后将查找匹配的参数名。例如,假设 TypeName 属性指定的类型有两个名为 DeleteARecord 的方法。一个 DeleteARecord 带一个参数 ID,另一个 DeleteARecord 带两个参数 Name 和 Number。如果 DeleteParameters 集合只包含一个名为 ID 的参数,则调用仅带 ID 参数的 DeleteARecord 方法。解析方法时不检查参数类型。参数的顺序无关紧要。

结合这段话,我们来分析上面的代码:
在执行Delete操作时,DeleteParameters集合中有两个参数:ID,来自于数据绑定;topicID,来自于DeleteParameters中的声明。因为这两个参数名不同,所以无法完成“修改现有参数以与DeleteParameters元素中指定的参数匹配”的工作。因此,就会出现上述的错误。

3、解决办法:
第一种办法:修改Topic类中的DeleteTopic(int topicID),将参数topicID修改为id(不用区分大小写)。然后将DeleteParameters的参数名也改为id
第二种方法:在gvwTopics的RowDeleting事件处理函数中,对DeleteParameters中的参数进行修改,代码如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
1 protected void gvwTopics_RowDeleting(object sender, GridViewDeleteEventArgs e)
2     {
3         int id = (int)e.Keys["ID"];
4         e.Keys.Clear();              //clear all params
5         e.Keys.Add("topicID", id);   //add new param,
6     }

这样就很好的解决了!


转载于:https://www.cnblogs.com/lao-zhang/archive/2009/09/17/1568516.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值