0

Hi I have a table tblRegistration which have three fields

Member_ID(int)  Left_Placement(Int) Right_Placement(int)

and one function

CREATE FUNCTION [dbo].[fnGetChildCount](
@ParentId INT
)
RETURNS INT
BEGIN
 DECLARE @ChildCount INT
 DECLARE @Left INT
DECLARE @Right INT


SET @ChildCount = 0
 IF NOT @ParentId IS NULL AND @ParentId <> 0
 BEGIN
 SET @ChildCount = 1
 SELECT @Left = Left_Placement, @Right = Right_Placement FROM tblRegistration WHERE Member_ID = @ParentId
SELECT @ChildCount = @ChildCount + [dbo].[fnGetChildCount](@Left)
 SELECT @ChildCount = @ChildCount + [dbo].[fnGetChildCount](@Right)
 END
 RETURN @ChildCount
END;

Now I use This Code

    SqlCommand cmd = new SqlCommand("SELECT Member_ID, dbo.fnGetChildCount(Left_Placement) AS [Left Count], dbo.fnGetChildCount(Right_Placement) AS [Right Count] FROM tblRegistration", con);
    dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            string member = dr["dbo.fnGetChildCount(Left_Placement)"].ToString();

        }
    }

To read Left Count And Right Count Which Is Not table column then

HOW CAN I READ THE LEFT COUNT(dbo.fnGetChildCount(Left_Placement) AS [Left Count]) AND RIGHT COUNT(dbo.fnGetChildCount(Right_Placement) AS [Right Count])

thank you in Advance

1
  • 1
    FYI: This kind of stuff usually does better in a stored procedure. Commented Mar 25, 2013 at 19:30

1 Answer 1

0

Use the column aliases to retrieve the computed column values.

string leftCount = dr["Left Count"].ToString();
string rightCount = dr["Right Count"].ToString();
Sign up to request clarification or add additional context in comments.

2 Comments

you don't have to wrap column names with [] when reading using datareader
Ya Mr. Rummell It is working string leftCount = dr["Left Count"].ToString(); string rightCount = dr["Right Count"].ToString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.