title: Parsing a decimal from a DataReader
body: <p>I found a workaround for this error, but am now really curious as to why this would be happening, was wondering if anyone else has had this error. </p>
  
  <p>My function is as follows:</p>
  
  <pre><code>public void Blog_GetRating(int blogID, ref decimal rating, ref int voteCount)
  {
      // Sql statements
      // Sql commands
  
      if (DataReader.Read())
      {
          // this line throws a 'Input string was not in a correct format.' error.
          rating = decimal.Parse(DataReader["Rating"].ToString());
  
          // this works absolutly fine?!
          decimal _rating = 0;
          decimal.TryParse(DataReader["Rating"].ToString(), out _rating);
  
          rating = _rating;
      }
  }
  </code></pre>
  
  <p>Anyone ever seen that before?</p>
  
  <p>What's even weirder is if i type this:</p>
  
  <pre><code>rating = decimal.Parse("4.0");
  </code></pre>
  
  <p>that works fine, the 4.0 is what is coming out from my DataReader.</p>
  
  <p>As I said previous, the TryParse method works fine so it's not stopping me from carrying, but now I'm really interested to see if anyone has an answer for it.</p>
  
  <p>Looking forward to some replies!</p>
  
  <p>Sean</p>
  
  <p><strong>EDIT - SOLVED</strong></p>
  
  <p>The decimal.Parse method was working fine, the second time the function was running (was in a loop), a post hadn't been rated so a null value was being returned by the data reader. Wrapping COALESCE round my calculation in SQL solved the problem fine. Hence why, as you said, the tryparse method wasn't throwing an exception, just keeping the default of 0 to _rating.</p>