2
Answers

Loop inside loop C#

Abdur Razique

Abdur Razique

7y
260
1
Hi i am having a problem writing C# code for my Web application.
 
I have an MS SQL Table named "Table_xyz" with two columns "User_id" & "Refrence_ID"
 
 User_id Reference_id
 1 0
 2 1
 3
 4 3
 5 4
 
I just only want to retrieve all "User_id" based on it's "Refrence_ID" in a loop
 
My Command: Select User_id from Table_xyz where Reference_ID = '0' (This is only to start the loop)
 
The loop must start referencing only from the reference_id which i will enter(it may be any reference id from the table)
 
According to the above command the result will give "1" which is the parent "User_id", for the "Reference_ID" '0'
 
But the issue comes here, i want to keep this first result to retrieve the next result for second result and then for third and so on.
 
Means the result(returned value) will become the "Reference_ID" for upcoming query looping each other and this way it will find entire table.
 
My Code below:
 
try
{
nCon.Open();
var gCmd = new SqlCommand(@"SELECT User_id FROM Table_xyz WHERE Reference_id = '0'", nCon);
SqlDataAdapter Sda = new SqlDataAdapter(gCmd);
DataTable Dt = new DataTable();
Sda.Fill(Dt);
int i = 0;
do
{
if (i < Dt.Rows.Count)
{
Response.Write(Dt.Rows[i]["User_iD"]);
i++;
}
}
while (i < Dt.Rows.Count);
}
catch (Exception e) { Response.Write(e.Message); }
finally { nCon.Close(); }
Answers (2)
1
Tapan Patel

Tapan Patel

NA 8.1k 100.9k 7y
I think you can do that in one shot (Avoiding the loop or recursion -at least not in code)
 
  1. SELECT User_id   
  2. FROM Table_xyz  
  3. WHERE User_id  
  4. IN  
  5. (SELECT DISTINCT Reference_id FROM Table_xyz)  
 so final code would be....
 
 
  1. try  
  2. {  
  3. nCon.Open();  
  4. var gCmd = new SqlCommand(@"SELECT User_id     
  5. FROM Table_xyz    
  6. WHERE User_id    
  7. IN    
  8. (SELECT DISTINCT Reference_id FROM Table_xyz) ;" , nCon);  
  9. SqlDataAdapter Sda = new SqlDataAdapter(gCmd);  
  10. DataTable Dt = new DataTable();  
  11. Sda.Fill(Dt);  
  12. int i = 0;  
  13. do  
  14. {  
  15. if (i < Dt.Rows.Count)  
  16. {  
  17. Response.Write(Dt.Rows[i]["User_iD"]);  
  18. i++;  
  19. }  
  20. }  
  21. while (i < Dt.Rows.Count);  
  22. }  
  23. catch (Exception e) { Response.Write(e.Message); }  
  24. finally { nCon.Close(); }  
1
Vincent Maverick Durano

Vincent Maverick Durano

NA 19.1k 2.2m 7y
You may need two queries to achieve that. First get all the reference ids and then loop through the result set to query the user ids. for example:
 
  1. DataTable dt = new DataTable();  
  2. DataTable dt2 = new DataTable();  
  3.     using(SqlConnection connection = new SqlConnection(GetConnectionString())){  
  4.         connection.Open();  
  5.         string sql = "SELECT Reference_id FROM YourTable";  
  6.         using(SqlCommand cmd = new SqlCommand(sql,connection)){  
  7.                 SqlDataAdapter ad = new SqlDataAdapter(cmd);  
  8.                 ad.Fill(dt);  
  9.   
  10.                 if(dt.Rows.Count > 0){  
  11.                       foreach(DataRow row in dt.Rows){  
  12.   
  13.                            string sql2 = "SELECT User_id FROM YourTable WHERE ReferenceID = @0";  
  14.                                 using(SqlCommand cmd2 = new SqlCommand(sql,connection)){  
  15.                                cmd2.Parameters.AddWithValue("@ReferenceID", Convert.ToInt32(rows[0].ToString()));  
  16.                                       SqlDataAdapter ad2 = new SqlDataAdapter(cmd2);  
  17.                                        ad2.Fill(dt2);  
  18.                      }  
  19.                 }  
  20.         }  
  21.   
  22.     }