Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send a ref int value to sqlserver from ASPx page?

I have this Stored procedure that returns UserID:

ALTER PROCEDURE dbo.GetUserID
(
@TopicID int, 
    @AskerID int
)
AS
SELECT       @AskerID = Post.Pt_CreatedBy 
WHERE        (Topic.Tp_ID = @TopicID)
RETURN @AskerID

Now when I call it from the Data access layer page using C#, I use this function:

 MyDatabaseDataContext StudyAppdb = new MyDatabaseDataContext();

public int GetUserID(int TopicID)
    {
        int UserID = -1;
        UserID = db.GetUserID(TopicID, UserID);
        return UserID;
    }

But GetUserID is supposed to take (int , ref int).... I don't know what is ref int & how can I pass it to the function.

like image 855
Sana Joseph Avatar asked Dec 04 '25 12:12

Sana Joseph


2 Answers

Since GetUserID is supposed to take (int , ref int), you should also use the ref keyword explicily when calling this method. Instead of calling the method with db.GetUserID(TopicID, UserID) you should use the ref key word, something like:

public int GetUserID(int TopicID)
{
    int UserID = -1;
    UserID = db.GetUserID(TopicID, ref UserID);
    return UserID;
}

You can read more about ref keyword.

like image 182
Mahmoud Gamal Avatar answered Dec 06 '25 04:12

Mahmoud Gamal


You can Write a Function Instead of Stored Procedure like given below

CREATE FUNCTION dbo.GetUserID
(
@TopicID int, 
    @AskerID int
)
RETURNS INT
AS
BEGIN
SELECT       @AskerID = Post.Pt_CreatedBy 
WHERE        (Topic.Tp_ID = @TopicID)
RETURN @AskerID
END

OR change the procedure with OUTPUT parameter. Then You have to change the data access layer code to capture that value..

like image 20
Joe G Joseph Avatar answered Dec 06 '25 03:12

Joe G Joseph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!