|
Colleague wrote a little method in C# code, where he created SqlCommand object and assigned to it's CommandText property as shown:
command1.CommandText = "select dbo.fn_GetValue(‘parameters')";
As you understood he tried to select a result of the database function. The function returns value of type bit. After invoking ExecuteScalar, command returned an object, which could not be converted to the bool type as expected. Strange! As I saw execution returned value as a string with the exception that added an empty spaces in the end. So, I advise you to avoid using such kind of commands, use execution of stored procedures instead. In that case the result will be as expected.
BTW, I found some not very good solution of this problem also. Just change a command text as followed:
command1.CommandText = "select cast(dbo.fn_GetValue(‘parameters') as bit)";
Everything will work, but again, I would use stored procedure instead, and of course, in case of stored procedures, you must specify the correct CommandType property value of the SqlCommand object. The property value must be System.Data.CommandType.StoredProcedure instead of System.Data.CommandType.Text.
I recommend visiting MSDN library article which explains details of working with the SqlCommand object shipped with Microsoft .NET framework, in particular on how to Set and Get Parameters for Command Objects.
That's all for today.
Don't forget, to have a rest is as important as to make a good job.
|