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
`
Usersclass should just be calledUserrepresents a single user, not a collection. And you should look into auto properties which can cut down on the unnecessary code.