SqlParameter with default value set to 0 doesn’t work as expected

Use caution when you use this overload of the SqlParameter constructor to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero, as the following C# example demonstrates. Parameter = new SqlParameter(“@pname”, Convert.ToInt32(0)); If you do not … Read more

Why does the SqlParameter name/value constructor treat 0 as null?

As stated in the documentation for that constructor: When you specify an Object in the value parameter, the SqlDbType is inferred from the Microsoft .NET Framework type of the Object. Use caution when you use this overload of the SqlParameter constructor to specify integer parameter values. Because this overload takes a value of type Object, … Read more

WHERE IN (array of IDs)

You can’t (unfortunately) do that. A Sql Parameter can only be a single value, so you’d have to do: WHERE buildingID IN (@buildingID1, @buildingID2, @buildingID3…) Which, of course, requires you to know how many building ids there are, or to dynamically construct the query. As a workaround*, I’ve done the following: WHERE buildingID IN (@buildingID) … Read more

Size property has an invalid size of 0

You need to define a length when specifying the varchar parameter: SqlParameter job1 = cmd2.Parameters.Add(“@job”, SqlDbType.VarChar, 50); You should use the same length as defined in your SQL Server stored procedure. And btw: if your stored procedure also has no length defined (something like @job VARCHAR OUTPUT) – then you’ve defined a varchar string of … Read more

Assign null to a SqlParameter

The problem is that the ?: operator cannot determine the return type because you are either returning an int value or a DBNull type value, which are not compatible. You can of course cast the instance of AgeIndex to be type object which would satisfy the ?: requirement. You can use the ?? null-coalescing operator … Read more