Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write a list of all users with their addresses and then compose html. I need your opinion

C#
public class User
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public List<Address> Addresses { get; set; }
}

public class Address
{
    public int ID { get; set; }
    public string Name { get; set; }
}


C#
public class UserData
{
    public static List<User> GetList()
    {
        List<User> lst = new List<User>();

        using (SqlConnection conn = new SqlConnection(Data.GetConn()))
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();

                cmd.CommandText = "select ID,Name,Surname from Users";                

                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {                       
                        User item = new User() {
                            ID = Convert.ToInt32(rdr["ID"]),                            
                            Name = rdr["Name"].ToString(),
                            Surname = rdr["Surname"].ToString(),                                                       
                        };

                        lst.Add(item);
                    }
                }
            }
        }

        return lst;
    }
    
    public static List<Address> GetListA(int UserID)
    {
        List<Address> lst = new List<Address>();

        using (SqlConnection conn = new SqlConnection(Data.GetConn()))
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
               
                cmd.CommandText = "select ID,Name from Addresses where UserID=@UserID";      
                cmd.Parameters.AddWithValue("@UserID", UserID);          
                               
                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        Address item = new Address()
                        {
                            ID = Convert.ToInt32(rdr["ID"]),
                            Name = rdr["Name"].ToString(),
                        };

                        lst.Add(item);
                    }
                }
            }
        }

        return lst;
    }
}


C#
protected void Page_Load(object sender, EventArgs e)
{
    List<User> user = new List<User>();

    user = UserData.GetList();

    foreach (User itemU in user)
    {
        Response.Write(itemU.Name);

        itemU.Addresses = UserData.GetListA(itemU.ID);

        foreach (Address itemA in itemU.Addresses)
        {
            Response.Write(itemA.Name);
        }
    }
}
Posted
Comments
Kornfeld Eliyahu Peter 26-May-14 4:58am    
Why not use data-capable ASP.NET controls to display your data?

1 solution

Correct, you should use some existing asp.net data controls rather than doing this.

Also it is better practice to get data in a chunk rather than getting row by row using datareader. Just get the data and have it in DataSet then bind the data control with the data retrieved. Only refresh data when any update or delete is happening.

Please let me know if this helps.


It also depends on the requirement, so please go through the difference here and decide as per your convenience.

Click Here[^]
 
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