5
Answers

C# code to loop inside loop

Abdur Razique

Abdur Razique

7y
172
1

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