SEA
SEA

Reputation: 167

C# - using TableAdapter to return a single value from stored procedure returns null

I do not understand but my stored procedure which I added to table adapter only returns null value. It is supposed to return a simple integer value. In the preview I had with data set desinger, I could clearly get the integer value that I wanted. But for some reason I cannot get the value from my codes.

I followed the instruction of MSDN library: http://msdn.microsoft.com/en-us/library/37hwc7kt(VS.80).aspx

My code for c# is:

humansDataSetTableAdapters.ProfilesTableAdapter tableAdapter 
= new humansDataSetTableAdapters.ProfilesTableAdapter(); 

int returnValue = (int)tableAdapter.getSample();

Console.Write(returnValue);

My code for stored procedure getSample is:

DECLARE @r int
SET @r = 7
RETURN @r

Can anybody let me know how I can solve this problem?? Any help will be appreciated!

Upvotes: 3

Views: 7899

Answers (3)

Nikki9696
Nikki9696

Reputation: 6348

Scalar expects a result, not a return. Be definition, it looks for the first column, first row. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

Try

DECLARE @r int
SET @r = 7
SELECT @r

Upvotes: 3

Derek Hunziker
Derek Hunziker

Reputation: 13141

Are you using a typed DataSet? If so, make sure your Stored Procedure is set to return a Scalar value.

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176886

rather than going for this solution I would like to suggest you to use ExecuteScalar if stored procedure returning single value.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

Upvotes: 1

Related Questions