CoderBlog.NET
Tengiz Tutisani's blog.

 
Useful Links:
 
LanguageBox 
 
Language

 
SearchBox 
 
 
ArchiveBox 
 
Archive
<February 2012>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
 
CategoriesBox 
 
Categories
  ASP.NET (5)
  Ajax (1)
  C# (7)
  Common (1)
  HTML (3)
  JavaScript (5)
  Products (1)
  SEO (1)
  SQL (1)
  Visual Studio (1)
  Windows (1)
 
FriendSitesBox 
 
Friend Sites
  AdMoney Project
  Cайт о гитаре и музыке!
  Design Studio IMAGINE
 
CustomContentBox [1] 
 



 
 
ExecuteScalar() result cannot be converted as expected
Strange results
Posted By:   tengo
At:   4/30/2009 9:55:00 AM
 

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.


Filed Under:   C# | SQL |
Post Tags:   csharp | execute | function | object | procedure | result | scalar | sql |
  Comments [0]



 
CustomContentBox [2] 
 
Useful Links:
 
CategoryViewBox 
 
CoderBlog.NET
  Get your own Blog Engine
  SMS Sending Service
  What do you want to see on CoderBlog.NET?
  IMAGINE.GE created a header image for CoderBlog.NET
  CoderBlog.NET offers link exchange
  Quick language selection added
  CoderBlog.NET ‘s what is what
  Welcome to CoderBlog.NET

  Jump to Category...
 
TagsBox 
 
Popular Tags
aspnetblogcsharpdesigndivengineerrorformhtmljavascript
 
AuthorsBox 
 
Authors
  Tengiz Tutisani  (25)
 
SubscriptionsBox 
 
Subscriptions
Your Email:
 
LoginStatusBox 
 
Site Members
Login
 
Copyright © 2009 . CoderBlog.NET