Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all. I have DAL code in asp.net c# for select all user of my table_users in DB sql server.

my DAL :

public string UserName
    {
        get { return _UserName; }
        set { _UserName = value; }
    }
    private string _Pass;

    public string Pass
    {
        get { return _Pass; }
        set { _Pass = value; }
    }
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }
    private string _Family;

    public string Family
    {
        get { return _Family; }
        set { _Family = value; }
    }

    private string _Mobile;

    public string Mobile
    {
        get { return _Mobile; }
        set { _Mobile = value; }
    }

public List<Users> GetAllUsers()
        {
            List<Users> usersList = null;

            using (DataTable table = SqlDBHelper.ExecuteSelectCommand("GetAllUsers", CommandType.StoredProcedure))
            {
                if (table.Rows.Count > 0)
                {
                    usersList = new List<Users>();

                    foreach (DataRow row in table.Rows)
                    {
                        Users user = new Users();

                        user.UserName = table.Rows[0]["UserName"].ToString();
                        user.Pass = table.Rows[0]["Pass"].ToString();
                        user.Name = table.Rows[0]["Name"].ToString();
                        user.Family = table.Rows[0]["Family"].ToString();
                        user.Mobile = table.Rows[0]["Mobile"].ToString();

                        usersList.Add(user);
                    }
                }
            }
            return usersList;
        }


and my BLL code for call:

public List<Users> GetAllUsers()
      {
          return userDB.GetAllUsers();
      }


and now I want to call method for display data in grid view .

public List<Users> usersList = null;
            UsersHadler userDB = new UsersHadler();
            usersList = userDB.GetAllUsers();
            foreach (Users user1 in usersList)
            {
                GridView1.DataSource = usersList;
                GridView1.DataBind();
            }


I have 3 user in data base.uer1 and user2 and user3
the problem is that I see in gridview 3 row for one user1.
user1
user1
user1
what is wrong.?
thanks for reply

What I have tried:

I change code:
GridView1.DataSource = userDB.GetAllUsers();
GridView1.DataBind();


but I have the same problem
Posted
Updated 27-Sep-17 18:05pm
v2

No need to use the loop to bind the data, however it will bind the same data again and again

foreach (Users user1 in usersList)
           {
               GridView1.DataSource = usersList;
               GridView1.DataBind();
           }


Check this Stored Procedure GetAllUsers whether it is returning duplicate records or not. other than that your code looks fine
 
Share this answer
 
Hi.

I saw your code what you have tried so far, there is nothing wrong in that but I am not sure stored procedure of GetAllUsers as Karthik told, Can you share your stored procedure here. so that we can help you find the solution.

You can call this code for binding data from List

List<Users> usersList=userDB.GetAllUsers();
GridView1.DataSource = usersList;
GridView1.DataBind();
 
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