0

I have DAL code in asp.net c# for select all user of my table_users in DB sql server.

my DAL :

public class Users
{
    private string _UserName;

    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

`

3
  • You really need to understand loops better. Look what you're looping over in your DAL. You're looping the rows in the table, but then on each iteration you're going back to the table, to the 0th row. You should reference the row that's declared in your loop. It's very similar to the issue Jaquez pointed out to you in binding the usersList repeatedly to the GridView. Commented Sep 27, 2017 at 20:36
  • 1
    Security Alert! Are you storing plaintext passwords in the database? That's a big security violation! Passwords should be one-way hashed and salted. Additionally, your Users class should just be called User represents a single user, not a collection. And you should look into auto properties which can cut down on the unnecessary code. Commented Sep 27, 2017 at 20:44
  • hi thanks. for reply . especially for your Security Alert! Commented Sep 27, 2017 at 21:04

1 Answer 1

2

Get rid of the foreach loop. You are going through usersList once for each user and binding the entire list to GridView1. You only need to do it once.

public List<Users> usersList = null;
UsersHadler userDB = new UsersHadler();
usersList = userDB.GetAllUsers();
GridView1.DataSource = usersList;
GridView1.DataBind();

You also probably want to call the GetAllUsers method from your BLL instead of calling the one from the DAL directly in your view. Otherwise, what was the point of the BLL?

Your problem is in the DAL version of GetAllUsers. You are referring only to Rows[0] in the foreach loop.

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

Replace the foreach loop in the DAL copy of GetAllUsers with this:

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

    user.UserName = row["UserName"].ToString();
    user.Pass = row["Pass"].ToString();
    user.Name = row["Name"].ToString();
    user.Family = row["Family"].ToString();
    user.Mobile = row["Mobile"].ToString();

    usersList.Add(user);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Are you still getting the same result?
@mason you stole the words right off of my keyboard.
Well, technically you did that to me. I had my answer all typed up but you got it there faster.
what should i do?
sorry. I cant speak english well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.