Summary: Create a class with input and output parameters. public static UserCollection GetUsers(Int16 userCategoryId, int pageIndex, int pageSize, out int totalUsers)
{
UserCollection UserCollection = new UserCollection();
User User = null;
SqlConnection Conn = new SqlConnection(ConnString);
SqlCommand Cmd = new SqlCommand("spGetUsersByUserCategory", Conn);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.Add(new SqlParameter("@UserCategoryId", userCategoryId));
Cmd.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
Cmd.Parameters.Add(new SqlParameter("@PageSize", pageSize));
SqlParameter paramTotal = new SqlParameter("@TotalRecords", SqlDbType.Int);
paramTotal.Direction = ParameterDirection.ReturnValue;
Cmd.Parameters.Add(paramTotal);
Cmd.Connection.Open();
SqlDataReader dr = Cmd.ExecuteReader();
while (dr.Read())
{
User = new User();
User.Id = dr["UserId"].ToString();
User.UserName = dr["UserName"].ToString();
User.Email = dr["Email"].ToString();
User.Comment = dr["Comment"].ToString();
UserCollection.Add(User);
}
dr.Close();
Cmd.Connection.Close();
Cmd.Dispose();
Conn.Dispose();
//Set return value = total number of users in this UserCategory
totalUsers = int.Parse(Cmd.Parameters["@TotalRecords"].Value.ToString());
return UserCollection;
}
Brukes slik:
protected void BindUsers(Int16 userCategoriId, int pageIndex, int pageSize)
{
int total;
UserCollection UserCollection = GetUsers(userCategoriId, pageIndex, pageSize, out total);
this.minDatagrid.DataSource = UserCollection;
this.minDatagrid.DataBind();
Response.Write(total);
} |