0

I have an excel sheet that has several headers. One of the header is Param2. The variable assigned to Param2 is strParam2 in my code. This is how strParam2 is declared in my code

 String strParam2 = xlRange.Cells[row, 7].Value;

My code ideally reads the cells in the excel sheets and executes a set of actions. I have given a value of 2 in a cell under Param2 in my excel sheet. In order to convert it to string, I used the line

System.Int32.TryParse(strParam2.ToString(), out iDetailLevel);

However, I am getting an error "Cannot implicitly convert double to string" I tried this line too, but however I get an error again.

System.Double.TryParse(strParam2.ToString(), out iDetailLevel);

What is the problem here? Have anybody experienced a similar thing before?

4
  • what is iDetailLevel ? Commented Jun 26, 2012 at 11:37
  • is strParam2 of type Int32 or Double or (as the prefix suggests) is it a string? Commented Jun 26, 2012 at 11:38
  • 1
    @KazR as the prefix suggests, strParam2 is declared as a string by default... Commented Jun 26, 2012 at 11:39
  • @user1473998 Looks like the value of that specific cell is a double and your code is trying to implicitly convert it to a string via the =. You need to check the value of the cell and convert accordingly Commented Jun 26, 2012 at 11:40

3 Answers 3

3

most likely, you're getting the error on the line where you assign to strParam2 because xlRange.Cells[row, 7].Value returns a double, which cannot be implicitly converted to string.

Sign up to request clarification or add additional context in comments.

2 Comments

yeah exactly...i get an error in that line only..actually, the same string variable strParam2 is used many times by different functions...One of the functions requires strParam2 to be an int, for ex 2 or 3 or 5...That's when i get this error..how do i use this string variabel for that particular function ? any idea ???
you cannot change a string variable to be an int later on. Can't you use different variables for different types?
2

Looks like the value of that specific cell is a double and your code is trying to implicitly convert it to a string via the =. You need to check the value of the cell and convert accordingly

String strParam2 = xlRange.Cells[row, 7].Value;

Would be:

String strParam2 = xlRange.Cells[row, 7].Value.ToString();

If, as you say, strParam2 is used elsewhere in code then simply create a new string to hold the header text and leave it unmodified:

string headerText = xlRange.Cells[row, 7].Value.ToString();

4 Comments

Hi...I use that same variable for a few functions in my code that requires it to be a double. if i use ur idea, i am getting errors for my other functions...
Why not create a new string to hold the header text and leave strParam2 alone then?
i get an error if i follow ur idea "Cannot perform runtime binding on a null reference" dat error comes in string headerText = xlRange.Cells[row, 7].Value.ToString();
You'll want to make sure that the cells value is != null before assigning
0

If you have the line:

String strParam2 = xlRange.Cells[row, 7].Value.ToString();

as Duane suggested. You do not need to have the .ToString() inside your parsing. Just use the strParam2 value.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.