Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 911103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:08:52+00:00 2026-05-15T17:08:52+00:00

Just trying to do a (I thought) simple callout to read a number from

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:08:52+00:00Added an answer on May 15, 2026 at 5:08 pm

    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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i just started learning swings. And thought of trying out a simple program, but
I'm trying to write a very simple app that will do just one very
I'm new to iPhone development and I'm just trying out some simple drawing routines
I'm just trying to do a simple modal message on page load. There are
trying to find absolute value and i thought there was a simple way to
Trying to implement what I thought was a simple concept. I have a user
I'm trying to do something I thought would be simple. Query SimpleGeo , take
So I thought this would just be a simple issue however I'm getting the
So I'm trying to use both local and global resources in a simple Net
I'm trying to get my first MS Chart to appear - just a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.