CRM 3.0 标注不起作用:事件:postcreate,异常:System.InvalidCastException:指定的强制转换无效
只是尝试执行一个(我认为)简单的标注,从存储在 CRM 服务器上的文本文件中读取数字,将其用作 CRM 表单上的值之一,并递增数字,然后将其重写到文本文件中。当我加载表单时,标注表面上不执行任何操作,服务器上的事件查看器会向我提供此无用的无效转换错误消息。我已经检查了代码并更改了各种内容,但没有效果,但我对 CRM 标注和 C# 都是全新的,所以我可能错过了一些愚蠢的东西。这是代码:
using System;
using System.IO;
using Microsoft.Crm.Callout;
namespace IncrementCompetitorNumber
{
public class Increment
{
public string IncrementNumber()
{
string ProjectAutoNumber = "D:\\CRM_Misc\\incrementers\\new_competitornumber.txt";
string AutoNumber = "0";
string ReturnThis = "0";
int i = 0;
lock(this)
{
TextReader tr = new StreamReader(ProjectAutoNumber);
AutoNumber = tr.ReadLine();
tr.Close();
ReturnThis = AutoNumber;
i = Convert.ToInt32(AutoNumber);
i++;
AutoNumber = i.ToString();
TextWriter tw = new StreamWriter(ProjectAutoNumber);
tw.WriteLine(AutoNumber);
tw.Close();
}
return ReturnThis;
}
}
}
那么...有人知道我做错了什么吗?
Just trying to do a (I thought) simple callout to read a number from a text file stored on the CRM server, use it as one of the values on the CRM form, and increment the number and then rewrite it to the text file. When I load the form, the callout ostensibly does nothing and Event Viewer on the server gives me this unhelpful invalid cast error message. I've gone over the code and changed various things to no avail, but I'm brand new to both CRM callouts and C#, so I'm probably missing something dumb. Here's the code:
using System;
using System.IO;
using Microsoft.Crm.Callout;
namespace IncrementCompetitorNumber
{
public class Increment
{
public string IncrementNumber()
{
string ProjectAutoNumber = "D:\\CRM_Misc\\incrementers\\new_competitornumber.txt";
string AutoNumber = "0";
string ReturnThis = "0";
int i = 0;
lock(this)
{
TextReader tr = new StreamReader(ProjectAutoNumber);
AutoNumber = tr.ReadLine();
tr.Close();
ReturnThis = AutoNumber;
i = Convert.ToInt32(AutoNumber);
i++;
AutoNumber = i.ToString();
TextWriter tw = new StreamWriter(ProjectAutoNumber);
tw.WriteLine(AutoNumber);
tw.Close();
}
return ReturnThis;
}
}
}
So... anyone know what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的代码中没有任何内容会导致无效的强制转换异常,异常发生在哪一行?
值得一提的是,您发布的代码不是 CRM 3.0 标注。
CRM 3.0 标注类必须从
CrmCalloutBase
继承,然后您可以覆盖各种事件方法之一,例如PostUpdate
。您是否在其他地方完成了此操作并从那里调用该类?好的,从你的第二条评论我知道你做错了什么。您还没有正确设置您的班级。我假设您想在某个时候对返回的字符串执行某些操作,但我暂时忽略了这一点,并且该值将被丢弃。
更改如下:
There is nothing in the codde you have posted the would cause an invalid cast exception, what line does the exception occur on?
One thing to mention is that the code you have posted is not a CRM 3.0 callout.
A CRM 3.0 callout class has to inherit from
CrmCalloutBase
and you then have overide one of the various event methods likePostUpdate
. Have you done this else where and are calling this class from there?OK from your second comment i know what you are doing wrong. You have not setup your class corectly. I assume you want to do something with the returned string at some point but i have ignored that for now and the value will just be discarded.
Change it as follows: