Abdur Razique

Abdur Razique

  • NA
  • 124
  • 1.4k

C# code to loop inside loop

Aug 4 2017 5:08 AM

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); 
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); 
} 
 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)