0
votes

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?

1

1 Answers

0
votes

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 like PostUpdate. 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:

using System;
using System.IO;
using Microsoft.Crm.Callout;

namespace IncrementCompetitorNumber 
{
    public class Increment : CrmCalloutBase
    {
        public override void PostCreate(CalloutUserContext userContext, CalloutEntityContext entityContext, string postImageEntityXml)
        {
            IncrementNumber();
        }

        private 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;
        }
    }
}