title: Convert method won't work when I'm trying to convert string variables to numeric & DateTime types (Npgsql Parameters)
body: <p>So, I am using C#/.NET and am using Npgsql.  Basically, I keep running into the error <code>System.InvalidCastException: 'Can't write CLR type System.String with handler type NumericHandler'</code> and I can't seem to resolve it.</p>
  <p>The error seems pretty self-explanatory.  Basically, I'm trying to use a string but Postgresql wants a Numeric data type. (Would that be a decimal or double data type?)</p>
  <p>For some context, I am re-writing old VB code to C# and in the VB version the initial declarations of the numeric variables are strings.  And in the code there is some string manipulation methods that are used.</p>
  <p>My thought process was it'd be easier and faster to just let the string manipulation stuff happen and then convert it to a decimal or a double later on. (Decimal's and Double's are Numeric postgresql equivalents, I think?)</p>
  <p>So here's an example of the initial declartions:</p>
  <pre><code>string tno = &quot;&quot;;
  string iw = &quot;&quot;;
  string ws = &quot;&quot;;
  string ftg = &quot;0&quot;;
  string hl = &quot;0&quot;;
  string ll = &quot;0&quot;;
  string date = &quot;2012/02/04&quot;;
  string va = &quot;0&quot;;
  string dev = &quot;0&quot;;
  </code></pre>
  <p>Later on in the code there are some string manipulation methods that are used.  As I said earlier I thought it'd be faster to just use some Conversion methods to convert the strings to their proper data types before feeding it to postgresql.</p>
  <p>For example:</p>
  <pre><code>Convert.ToDecimal(tno);
  </code></pre>
  <p>Here's an example of the SQL string:</p>
  <pre><code>string sqlString = @&quot;UPDATE header
  SET ft = @gftg, high_limit=@ghl, low_limit=@gll, start_date = @gsdd, avg_ga = @gvgga, std_dev = @gstd
  WHERE cmpy_id = 'ABC' and tag_no = @gto and ip_num = @gim&quot;;
  </code></pre>
  <p>Here's the Npgsql parameter code:</p>
  <pre><code>cmd.Parameters.Add(&quot;@gim&quot;, NpgsqlTypes.NpgsqlDbType.Varchar).Value = iw;                              
  cmd.Parameters.Add(&quot;@gftg&quot;, NpgsqlTypes.NpgsqlDbType.Numeric).Value = ftg;                            
  cmd.Parameters.Add(&quot;@ghl&quot;, NpgsqlTypes.NpgsqlDbType.Numeric).Value = hl;                              
  cmd.Parameters.Add(&quot;@gll&quot;, NpgsqlTypes.NpgsqlDbType.Numeric).Value = ll;                              
  cmd.Parameters.Add(&quot;@gsdd&quot;, NpgsqlTypes.NpgsqlDbType.Date).Value = date;                             
  cmd.Parameters.Add(&quot;@gvgga&quot;, NpgsqlTypes.NpgsqlDbType.Numeric).Value = va;                              
  cmd.Parameters.Add(&quot;@gstd&quot;, NpgsqlTypes.NpgsqlDbType.Numeric).Value = dev;
  </code></pre>
  <p>This isn't really working though and it's probably for an obvious reason that I'm hoping you guys can help me with.</p>
  <p>Is there no easy way to do this and will I just have to change the initial variable declarations to the correct data types right away?  So for example perhaps It should be more like this?  Then I would have to mess with the code below it and do some string manipulation equivalents or something like that..</p>
  <pre><code>decimal tno = &quot;&quot;;
  string iw = &quot;&quot;;
  string ws = &quot;&quot;;
  decimal ftg = &quot;0&quot;;
  decimal hl = &quot;0&quot;;
  decimal ll = &quot;0&quot;;
  string date = &quot;2012/02/04&quot;;
  decimal va = &quot;0&quot;;
  decimal dev = &quot;0&quot;;
  </code></pre>
  <p>Any ideas why the <code>Convert.ToDecimal(tno)</code> before inputting into the postgres npgsql parameters isn't working?</p>