I am calling a SQL proc that has 3 OUTPUT params. After the call to the proc one of the params does not return a value when the other two do. Profiler shows that all 3 values are being returned.
The params are declared as follows in the proc…
@UsrVariableID INT OUTPUT, @OrganisationName NVARCHAR(256) OUTPUT, @Visible bit OUTPUT
and the code that calls the proc is like this…
cm.Parameters.AddWithValue('@OrganisationName', name); cm.Parameters['@OrganisationName'].Direction = ParameterDirection.Output; cm.Parameters.AddWithValue('@Visible', visible); cm.Parameters['@Visible'].Direction = ParameterDirection.Output; cm.ExecuteNonQuery(); name = cm.Parameters['@OrganisationName'].Value.ToString(); visible = bool.Parse(cm.Parameters['@Visible'].Value.ToString()); id = int.Parse(cm.Parameters['@UsrVariableID'].Value.ToString());
The param that fails is @OrganisationName.
I’m wondering if its because the param is of type string in the code but NVARCHAR in the proc.
Anyone got any ideas?
With output parameters that have variable length data types (nvarchar, varchar, etc), I’ve found that being more explicit leads to better results. In the case you’ve posted, a type is not specified on the C# side. I would probably change things to look something like the following:
With this you can guarantee the output paratmer has the correct data type, and therefore can access the Value property without and exception being thrown.
Hope this sheds some light.