.Net ObjectDataSource错误:对象与目标类型不匹配
我的页面上有一个 ObjectDataSource,当调用其 Insert 方法时,该页面会产生错误“对象与目标类型不匹配”。通过谷歌搜索此消息,我相信该消息具有欺骗性,并且实际上在 ObjectDataSource 尝试调用该方法的对象上收到了空引用错误,但如果我能找出原因,我就该死了。
<asp:ObjectDataSource ID="dsAddComment" runat="server"
DataObjectTypeName="BookCatalogue.InteractionDocuments.UserComment"
SelectMethod="GetNewComment" TypeName="BookCatalogue.AddCommentPresenter"
InsertMethod="AddComment" OnObjectCreating="dsAddComment_ObjectCreating" />
Insert 时调用的类型是 AddCommentPresenter。 AddComment 方法不是静态的。如果我将其更改为静态,则不会收到错误,并且可以毫无问题地找到该方法。当它不是静态时,就会发生错误。这就是为什么我认为根本问题是在调用 AddComment 方法时我没有获得 Presenter 类的有效实例。
我的 AddCommentPresenter 类没有无参数构造函数。这通常会导致错误。为了解决这个问题,我重写了页面代码隐藏中的 ObjectCreating 事件,并分配了 Presenter 类的一个实例。
protected void dsAddComment_ObjectCreating(对象发送者,ObjectDataSourceEventArgs e) { e.ObjectInstance = 演示者; 我可以单步执行ObjectCreating
方法,它是一个有效的非 null Presenter 实例,被传递到 e.ObjectInstance 属性中。
我的 AddComment 方法具有正确的签名。
public void AddComment(UserComment newComment)
{
...
}
我还检查了一些明显的事情,例如 aspx 页面上的类型名称拼写错误,但那里一切都是正确的。
有人有什么想法吗?我必须说我发现 ObjectDataSource 类很难使用......
I have an ObjectDataSource on a page that is producing the error "Object does not match target type" when its Insert method is invoked. From Googling this message, I believe this the message is deceptive and I'm actually getting a null reference error on the object that the ObjectDataSource is trying to invoke the method on, but I'm darned if I can figure out why.
<asp:ObjectDataSource ID="dsAddComment" runat="server"
DataObjectTypeName="BookCatalogue.InteractionDocuments.UserComment"
SelectMethod="GetNewComment" TypeName="BookCatalogue.AddCommentPresenter"
InsertMethod="AddComment" OnObjectCreating="dsAddComment_ObjectCreating" />
The Type that is being called upon Insert is an AddCommentPresenter. The method AddComment is not static. If I change this to static, I don't get the error and the method is found without problem. When it's not static, the error occurs. That's whyI htink that the underlying problem is that somehow I'm not getting a valid instance of my Presenter class when the AddComment method is invoked.
My AddCommentPresenter class doesn't have a parameterless constructor. This will cause an error normally. To get around it I'm overriding the ObjectCreating event in my page's code-behind and assigning an instance of the Presenter class.
protected void dsAddComment_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = presenter;
}
I can step through my ObjectCreating method and it is a valid, non-null Presenter instance that's being passed into the e.ObjectInstance property.
My AddComment method has the correct signature.
public void AddComment(UserComment newComment)
{
...
}
I've also checked the obvious things, like misspelling the type name on the aspx page, but all is correct there.
Anyone have any ideas? I must say that I find the ObjectDataSource class very difficult to work with....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一位同事找到了我的问题的原因。我的 Web 应用程序中的 AddCommentPresenter 类是在网站的 App_Code 目录中定义的。由于某种原因,这导致了错误。将其从那里移到网站的主目录中,代码就可以工作了。我在任何 ObjectDataSource 文档中都找不到任何提及这是该控件的潜在问题的内容,但你就知道了。
我还被告知应该可以将类保留在 App_Code 文件夹中,但在 TypeName 末尾包含语法“,__code”。例如
,但就我个人而言,当我尝试时,我并没有得到它的工作。 ASP.Net 论坛的另一篇文章建议将 TypeName 更改为
That 对我来说也不起作用。我最终只是将类从 App_Code 文件夹中取出。
A colleague found the cause of my problem. The AddCommentPresenter class in my web app was defined in the website's App_Code directory. For some reason, that was causing the error. Move it out of there, into the main directory of the website and the code worked. I can't find any mention in any ObjectDataSource documentation of this being a potential gotcha for the control, but there you go.
I've also been told that it should be possible to keep the class in the App_Code folder, but include the syntax ",__code" at the end of the TypeName. E.g.
but personally, I didn't get that working when I tried it. Another post to an ASP.Net forum suggested changing the TypeName to
That didn't work for me, either. I ended up just pulling the class out of the App_Code folder.