2 loops in a datareader or datatable RRS feed

  • Question

  • User-1770812916 posted


    I have some difficulties to create 2 loops in a datatable or datareader. The goal : create a text body for sending an email to each user.
    I explain me ..

    With a sql query I get back a table with the columns idname, username, and operation.
    With is I create a datatable or datareader and I want to loop in and send an email for each user with concatenating all the operations for each user.

    A sample of my table

    1 user1 text1
    1 user1 text2
    2 user2 text1
    2 user2 text2
    2 user2 text3
    3 user3 text1 ...

    I would like to do a first loop on the ID user and for each ID user do an another loop to concatenate the differents texts and send an email. And pass to the next user and do the same thing until the last user.
    What is the best way to do it ?

    Tanks a lot for your help.





    Je voudrais envoyer un email à dupont listant tous les types d'achat
    Et la même chose pour durant et les autres.

    Comment organiser les boucles. Faire une première boucle tant que le nom est dupont et faire une sous boucle sur le type d'achat tant que le nom est dupont et envoyer le mail. Puis faire la meme chose avec le nom suivant ..;

    Merci de votre aide


    Sunday, October 3, 2010 3:19 AM


  • User1224940435 posted

    There are two ways of doing it.

    1. Write Query such a way that it return data in valid format (Order ny UserID)

    string userID = string.Empty;
    sitrng message = string.empty;


           if(reader["UserID"] != UserID)
                SendMessage(userID , message); 
                userId = reader["UserID"];
                message = reader["Message"];             

           message = message + reader["MEssage"].ToString();


    In SendMessage check that UserID is blank or Not .

    2. Another way is load in DataTable then user DataView to sort it by UserName and Apply same logic.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 3, 2010 8:37 AM