Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Finally i came to this but the having following issue

I want to retrieve all User_id's next after my entered User_id referencing each other from the table by Reference_id, the code below gives the exact result but it retrieves all User_id's from "2001 to 2005".

I want if i enter the "2002" as User_id from a textbox then it must retrieve from "2003 - 2005"

Table_xyz column User_id have value= 2001, 2002, 2003, 2004, 2005
Table_xyz column Reference_id have value= 2000, 2001, 2002, 2003, 2004

C#
var gCmd = new SqlCommand(@"SELECT User_id FROM Table_xyz", nCon); 
SqlDataAdapter Sda = new SqlDataAdapter(gCmd); 
DataTable Dt = new DataTable(); Sda.Fill(Dt); 
for (int i = 0; i < Dt.Rows.Count; i++) 
{ 
    string referenceid = Dt.Rows[i]["User_id"].ToString(); 
    var gCmd1 = new SqlCommand(@"SELECT User_id FROM Table_xyz WHERE 
                                 Reference_id = '" + referenceid + "'", nCon); 
    SqlDataAdapter Sda1 = new SqlDataAdapter(gCmd1); 
    DataTable Dt1 = new DataTable(); 
    Sda1.Fill(Dt1); 
    Response.Write(referenceid); 
}


What I have tried:

I tried adding "SELECT User_id FROM Table_xyz WHERE User_id = '2001'" to the first command but it returns only a single value where User_id matched "2001"
Posted
Updated 3-Aug-17 23:18pm
v3

Use a recursive query, and you'll be able to retrieve all records in one hit:
SQL
WITH cte As
(
    SELECT
        User_id,
        '/' + CAST(User_id As varchar(max)) + '/' As path
    FROM
        Table_xyz
    WHERE
        Reference_id = @ID
    
    UNION ALL
    
    SELECT
        B.User_id,
        A.path + CAST(B.User_id As varchar(max)) + '/'
    FROM
        cte As A
        INNER JOIN Table_xyz As B
        ON B.Reference_id = A.User_id
    WHERE
        A.path Not Like '%/' + CAST(B.User_id As varchar(max)) + '/%'
)
SELECT
    User_id
FROM
    cte
;
 
Share this answer
 
Comments
GeoFinex 4-Aug-17 6:09am    
I tried your code and is helpful for me but a new issue occurred because it compares values as an Integer and my User_id, Reference_id may have numeric and alphanumeric values like "ricky_global9, peter123"

See me corrected code:

var gCmd = new SqlCommand(@"(SELECT dIstributor_iD FROM dEmo_aCcounts WHERE
@Sponsor_id <= dIstributor_iD )", nCon);

gCmd.Parameters.AddWithValue("@Sponsor_id", "2723022");
SqlDataAdapter Sda = new SqlDataAdapter(gCmd);
DataTable Dt = new DataTable();
Sda.Fill(Dt);

for (int i = 0; i < Dt.Rows.Count; i++)
{
Response.Write(Dt.Rows[i]["dIstributor_iD"]);
}
Richard Deeming 4-Aug-17 7:14am    
The code I posted will still work with alphanumeric IDs. (Assuming you've not broken your database by storing multiple values in a single field?)

The only potential issue is if your IDs include the / character. If they do, pick a different character to separate IDs in the path.
GeoFinex 4-Aug-17 7:48am    
Yes working, but it's giving unexpected result when i changed the user_id values to alpha numeric.
Richard Deeming 4-Aug-17 8:05am    
Without seeing a sample of the input, the expected output, and the actual output, I can't tell you what the problem is.

Try creating a demo on SQL Fiddle[^].
GeoFinex 4-Aug-17 13:52pm    
Here is the fiddle http://sqlfiddle.com/#!9/33fbe4/17
I think you need SqlCommand.ExecuteScalar, see example here: SqlCommand.ExecuteScalar Method (System.Data.SqlClient)[^]
 
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