0

First off I am fairly new to C# but I am trying to convert a string to an Int. I get no errors in Visual Studio but when I actually try to preview my page in Sitecore I get an Error. The error is System.FormatException: Input string was not in a correct format.

This is my code:

public string numColumnStr = "";
        numColumnStr = parameters.columns;
        int numColumns = 2;
        if (numColumns != null)
            numColumns = Convert.ToInt32(numColumnStr);

I want the default value of the columns to be 2. and i have

1
  • 2
    What is parameters.columns? I also don't get why you're doing if (numColumns != null) when you're directly setting numColumns the line before... Commented Jul 9, 2014 at 15:46

6 Answers 6

1

I made two changes, first I used tryparse instead of convert.

Second, I made the judgement call that you meant to compare the string to null, not the integer. So I updated that line to check if the string is null or empty.

public string numColumnStr = "";
        numColumnStr = parameters.columns;
        int numColumns = 2;
        if (!string.IsNullOrEmpty(numColumnStr))
            int.TryParse(numColumnStr, out numColumns);
Sign up to request clarification or add additional context in comments.

1 Comment

@user3416165, glad I could help. If this solved your issue, please make sure to mark it as the answer.
1

Looks to me like you are checking the wrong variable. Perhaps you mean to do this:

public string numColumnStr = "";
    numColumnStr = parameters.columns;
    int numColumns = 2;
    if (!String.IsNullOrEmpty(numColumnStr))
        numColumns = Convert.ToInt32(numColumnStr);

1 Comment

Thanks for the help @Johan Hjalmarsson
1

Although your code is quite awkward, it will work with a minor change:

change:

if (numColumns != null)

to:

if (numColumnStr != null)

Comments

1

It looks like that coming string is not a valid integer.

Use tryParse(), if it is parsed successfully or it is a valid integer it will return a flag true:

bool parsed = int.TryParse(numColumnStr,out numColumns);

if(parsed)
    // do something with numColumns here

See Details on MSDN

1 Comment

thanks for the link, this was helpful for me to understand the concept better.
0

Here's the c# code you need:

int numColumns = 2; // default value
int.TryParse(parameters.columns, out numColumns);

Now I suspect that parameters.columns does not actually point to what you expect. The error you are getting is what would be thrown, if the value was null or empty.

Comments

0

Since you are using Sitecore, you can also make use of the utility functions in Sitecore.Kernel.dll:

int numColumns = Sitecore.MainUtil.GetInt(parameters.columns, 2)

And FYI, internally the function does the following:

public static int GetInt(string value, int defaultValue)
{
  int result;
  if (value == null || value.Length == 0 || !int.TryParse(value, out result))
    return defaultValue;
  else
    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.