From an answer or comment to my question here, I was directed here.
So I changed my code from this:
double _imdbThreshold = 0.0;
(IMDBRating is a Decimal(2,1) data type in the SQL Server (Express) Database.)
...
cmd.Parameters.AddWithValue("@IMDBMinRating", _imdbThreshold);
...to this:
cmd.Parameters.Add("@IMDBMinRating", SqlDbType.Decimal, 2, 1).Value = _imdbThreshold;
...based on this example in the article linked above:
cmd.Parameters.Add("@Parameter", SqlDbType.Decimal, 11, 4).Value = MyDecimalVariable;
But I get, "Cannot convert from int to string":
?!?
Why is it finding fault with my "1" int arg there?
I can't find a reference any more, but years ago when using SqlHelper, the guidance came with a number of extension methods that simplify working with
SqlCommand
andSqlParameter
.The following extension method will allow your code to work, I can only speculate but I suspect the code you referenced used a similar extension method and they were not aware it was not part of the standard runtime. a very easy mistake to make!
I find this syntax simpler to read when you stare at a lot of these all day, but it is also consistent with the standard
Add
methods for types that do not support precision:Given the number of other existing methods, I can only imagine that the original developers simply forgot to include this simple method in the
SqlParameterCollection
definition.