Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
string TEMPID = "";
            string Pid = "";
            
            //bool MYFLAG = false;
            SqlConnection con3 = new SqlConnection(ConfigurationManager.ConnectionStrings["amms"].ConnectionString);
            con3.Open();
           i = 1;
            while ( i <= 10)
            {
                SqlDataReader RDR;
               
                SqlCommand CMD2 = new SqlCommand("SELECT ParentId FROM Members WHERE MemId='" + lblMemId.Text + "'", con3);
                RDR = CMD2.ExecuteReader();
                while (RDR.Read())
                {
                   // Response.Write("<script> alert('" + RDR[0].ToString() + "'); </script>");
                    ListBox1.Items.Add(RDR[0].ToString());
                    TEMPID = RDR[0].ToString();
                    if (RDR[0].ToString() != "421")

                        TEMPID = lblMemId.Text;
                        lblMemId.Text = Pid;
                        Pid = TEMPID;
                      //  MYFLAG = true;
                    
                }
                RDR.Close();
                i++;


and my database table is:


MemId	ParentId   ThroughOverId     Leg	lleg	rleg
452	253	        253	     Right	172	586
172	452	        452	     Left	948	651
586	452	        452	     Right	172	973
948	172	        172	     Left	773	885
773	948	        948	     Left	333	430
333	773	        773	     Left	715	757
715	333	        333	     Left	637	759
973	586	        586	     Right	379	
757	333	        333	     Right		
885	948	        948	     Right		
379	973	        973	     Left		
430	773	        773	     Right		
172	586	        586	     Left		651
651	172	        172	     Right		
637	715	        715	     Left		
759	715	        715	     Right		


HOW TO FIND THE ALL PARENT OF 757 i.e, 333,773,948,172,452(just example) upto 11 level.

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 13-Oct-12 6:00am
v2
Comments
[no name] 13-Oct-12 12:06pm    
Wow... 4 reposts... almost SPAM
Kenneth Haugland 13-Oct-12 12:38pm    
New record? Is he close?
[no name] 13-Oct-12 18:16pm    
I have personally seen 6 reposts.
Sergey Alexandrovich Kryukov 14-Oct-12 2:18am    
I removes one or more, after I answered one of the questions, so it could be even more. I call it spam.
OP is advised to use only one page for one question: comment on existing posts, use "Improve question".

Persistent re-posts can cause abuse reports which eventually cause revocation of CodeProject membership!

--SA
Sergey Alexandrovich Kryukov 14-Oct-12 2:19am    
It means trying to avoid solving the problem independently. Answering is pretty much useless in such cases.
--SA

1 solution

SQL
DECLARE @FindParentsForMember INT;
SET  @FindParentsForMember = 757;
WITH Hierarchy(MemId,ParentId) 
AS(
     SELECT DISTINCT parent.MemId,parent.ParentId
     FROM Members parent
     WHERE parent.MemId = @FindParentsForMember

     UNION ALL

     SELECT child.MemId,child.ParentId
     FROM Members child
     INNER JOIN Hierarchy parent ON parent.ParentId = child.MemId
)
SELECT DISTINCT ParentId
FROM Hierarchy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900