Crazy ADO.Net Error?

Status
Not open for further replies.

Sheriff

Ars Tribunus Militum
1,841
alright, i should be getting a value back in @ret (an output variable) when i run this stored procedure, but im getting a null for the parameter value after running the procedure. heres the thing, if i run the procedure under query analyzer it return the value correctly.

heres the stored procedure

Code:
 CREATE PROCEDURE 

Forum_GetPosts

(

@ThreadID int,
@PageNum int,
 @ret int OUTPUT

)

AS

set @ret = (select (count(a.PostID) / 50) + 1 from Forum_Post  a where a.ThreadID = @ThreadID)

exec Forum_UpdateViews @ThreadID


select  top 50   

Forum_Post.*,
 Forum_User.UserName, 
Forum_User.Location, 
Forum_ForumGroupPostCount.PostCount,
Forum_User.RegisterDate

FROM        
 Forum_Post  

INNER JOIN Forum_User ON Forum_Post.UserID = Forum_User.UserID 
left outer join Forum_ForumGroupPostCount on Forum_User.UserID = Forum_ForumGroupPostCount.UserID

 where ThreadID = @ThreadID and ( [Order] >(50 * (@PageNum -1 )))  order by [Order] asc


GO

now if i comment out the last select statement (the large one) i get the return value after the SqlCommand.ExecuteReader(), but i get a null if that query is run. I can put the query where @ret is set anywhere in the procedure and it will correctly report back IF the main select is commented out. The SqlParamater is set correctly (Output Direction) as far as i can tell. Whats confusing me here, and makes me think that everything i am doing is legal, is that the value will correctly be set no matter if the main select is run when testing from query analyzer. Does anyone see something wrong with the query? I'd like to be able to run this all once instead of having to get the pagecount and then run another command to update views and get posts.

Thanks for any help,
Sheriff
 

temp

Seniorius Lurkius
44
I'm still trying to get up to speed on ADO.NET myself, so I'm not sure how much help this is. But did you try executing your stored procedure from Query Analyzer using sp_executesql instead of just executing the sql directly? The ExecuteReader method of SqlCommand uses sp_executesql to execute your statement and it doesn't always play nice with SET statements. If your stored procedure fails to do the right thing when executed through sp_executesql this is probably your problem.

You might try replacing your SET statement in the stored procedure with an equivalent SELECT statement, which could help. Maybe.
 

Kurt

Ars Tribunus Angusticlavius
8,820
Subscriptor++
Can you post the appropriate code?

You might also try switching your SQL to this, then running it and see what happens:

Code:
CREATE PROCEDURE 

Forum_GetPosts

(

@ThreadID int,
@PageNum int,
 @ret int OUTPUT

)

AS

select @ret = ((count(a.PostID) / 50) + 1) from Forum_Post  a where a.ThreadID = @ThreadID

exec Forum_UpdateViews @ThreadID


select  top 50   

Forum_Post.*,
 Forum_User.UserName, 
Forum_User.Location, 
Forum_ForumGroupPostCount.PostCount,
Forum_User.RegisterDate

FROM        
 Forum_Post  

INNER JOIN Forum_User ON Forum_Post.UserID = Forum_User.UserID 
left outer join Forum_ForumGroupPostCount on Forum_User.UserID = Forum_ForumGroupPostCount.UserID

 where ThreadID = @ThreadID and ( [Order] >(50 * (@PageNum -1 )))  order by [Order] asc


GO
 
If memory serves (it probably doesn't), if you're using a DataReader instead of a DataTable to get at your stored proc, you don't get access to your out parameters until after you close your connection.
Also, another sort of odd thing is that parameters don't automatically return themselves like you'd expect when you pass in a parameter with ParameterDirection.Output. You have to access the parameters collection and retrieve out storedProc.Parameters["@ret"].Value, possibly wrapping it in a Convert.ToWhatever() to make it useful.
 

Sheriff

Ars Tribunus Militum
1,841
im in your debt jxl, after the connection is closed, poof! i have access to my page count. thanks.

Just out of curiosity though, why would the restrict your access output parameters when you are also returning a recordset until after the connection is closed? it seemed that if i only return the @ret then everything would be available immediately, but if i returned any records it wouldnt be available until after I closed the connection.

hey, atleast it works, and these kind of things are exactly why i wanted to do this project.


thanks again.
 
Status
Not open for further replies.