5 Replies - 1279 Views - Last Post: 17 July 2012 - 02:48 AM Rate Topic: -----

#1 inkey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-June 12

C# Input string was not in a correct format

Posted 16 July 2012 - 09:24 AM

Hi Guys this is a great forum and i have learnt alot from using the tutorials.

However i have having some trouble with some of my code at the moment. I keep getting a error saying C# Input string was not in a correct format.

Here is the code in questions that i am having the error with.

  arrData = clsData.getwarninglevel(machineID, startTime, endTime);
				                if (arrData.Count > 0 )
				                    {
					                    string[] Alarmlevels = arrData[0].ToString().Split('\t');
                                        pFAlarmLimitOk = Convert.ToInt32(Alarmlevels[0]);
                                        pFAlarmLimitWarning = Convert.ToInt32(Alarmlevels[1]);
				                    }



Any you guys know how could get around this.

Is This A Good Question/Topic? 0
  • +

Replies To: C# Input string was not in a correct format

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,851
  • Joined: 18-April 07

Re: C# Input string was not in a correct format

Posted 16 July 2012 - 09:29 AM

And what is the data in arrData[0]? After splitting on the tab, if either the first or second piece is text which is not looking like an int (for instance contains a letter) you are going to get that error on the conversions.

So put a breakpoint on line 4 in the code above and take a look at what content is in arrData[0]. Make sure that is purely numbers with an optional sign.

Then check Alarmlevels[0] and Alarmlevels[1] to make sure they are both nothing but numbers.

:)

This post has been edited by Martyr2: 16 July 2012 - 09:30 AM

Was This Post Helpful? 2
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Re: C# Input string was not in a correct format

Posted 16 July 2012 - 10:40 AM

The TryParse methods can also be very handy. Instead of throwing an exception, they simply return false.

int.TryParse(int)
Was This Post Helpful? 1
  • +
  • -

#4 inkey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-June 12

Re: C# Input string was not in a correct format

Posted 17 July 2012 - 12:54 AM

Hi guys i have tried what you have said regarding debugging.

The Value that it has when it throws the exception is 0.

I know this is a int and it should not be throwing this.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3163
  • View blog
  • Posts: 9,557
  • Joined: 05-May 12

Re: C# Input string was not in a correct format

Posted 17 July 2012 - 01:03 AM

Is it line 5 or line 6 that is throwing an exception?

The value that they are asking about is Alarmlevels[0] and Alarmlevels[1]. If both are valid strings, then the values should be some integer in double quotes like "45" or "0" or "23".

Your response above doesn't provide enough information. Is "Value" above referring to pFAlarmLimitOk, pFAlarmLimitWarning, Alarmlevels[0], or Alarmlevels[1]?
Was This Post Helpful? 0
  • +
  • -

#6 inkey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 26-June 12

Re: C# Input string was not in a correct format

Posted 17 July 2012 - 02:48 AM

Got sorted.

The problem was that the data that was being returned from Alarmlevels[0] was null.

I changed the SQL statement to include the use of COALESCE to change any null values to charcter 0

I used something similar to this

SELECT COALESCE([WL],'0') as WL , COALESCE([DL],'0') as DL

Thanks Inkey
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1