Background: I have a file: Users.aspx containing has a datagrid which populates with user data (id, name, email, etc) from a database. The firstName field is a HyperLinkField, which when clicked takes you to User.aspx, a page intended to edit user data and insert new users into the db.
The form on User.aspx is to be pre-populated with the user's information if a user ID exists (and if not, then it is blank in order to create a new user).
I have three .cs files involved in this functionality:
User.aspx.cs - checks for postback, determines if need to create a new user or populate the form based on userID.
User.cs - business class containing the constructor which fills the object for use in User.aspx.cs
- UserDataService.cs - contains connection/sqlcommands/sqldataadapter functionality
Through debugging it looks to me like a User object is being initialized in User.aspx.cs and makes its way over to User.cs and is successfully filled by the constructor there. All of the values are correctly added to the object. The debugger then takes me back to User.aspx.cs, where suddenly everything has become null. Obviously, as a result, my form controls are not getting populated with the data. I feel like I'm probably overlooking something really simple and just need another pair of eyes looking over it.
Here is the relevant code from each:
User.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (RouteData.Values["id"] != null)
{
if (RouteData.Values["id"] == "-1")
{
BuildNew();
}
else
{
GetUser(Convert.ToInt32(RouteData.Values["id"]));
} // end else
}
else
{
BuildNew();
} // end else
} // end if
} // end page_load
private void BuildNew()
{
lblID.Text = "";
} // end BuildNew()
private void GetUser(int id)
{
// ** PROBLEM - this method is initially creating the User object based on the id parameter (using User.cs's User(int id) constructor. The fields are all being filled in over there and then once it comes back to here, all of the values become null.**
User u = new User(id);
s lblID.Text = u.UserId.ToString();
txtUserFirstName.Text = u.First;
txtUserLastName.Text = u.Last;
txtUserEmail.Text = u.Email;
txtUserPassword.Text = u.Password;
rblUserRole.SelectedValue = u.Role;
txtUserLastLogin.Text = u.Lastlogin.ToShortDateString();
rblUserRole.SelectedValue = u.IsActive.ToString();
}
User.cs:
public User(int id) //WORKING
{
DataTable dt = UsersDataService.GetByID(id);
User u = new User();
int rowcount = dt.Rows.Count;
if (dt.Rows.Count > 0)
{
u.UserId = (Int32)dt.Rows[0]["UserID"];
u.First = (string)dt.Rows[0]["First"];
u.Last = (string)dt.Rows[0]["Last"];
u.Email = (string)dt.Rows[0]["Email"];
u.Password = (string)dt.Rows[0]["Password"];
u.Role = (string)dt.Rows[0]["Role"];
u.Lastlogin = (DateTime)dt.Rows[0]["LastLogin"];
u.Created = (DateTime)dt.Rows[0]["Created"];
u.Updated = (DateTime)dt.Rows[0]["Updated"];
u.IsActive = (bool)dt.Rows[0]["Active"];
}
else
{
u = null;
}
} // end User
UsersDataService.cs:
public static DataTable GetByID(int id) // WORKING
{
DataTable dt = new DataTable();
SqlConnection cn =
new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString.ToString());
// sqlcommand using the stored procedure, connection
SqlCommand cmd = new SqlCommand("SP_GetUsersByID", cn);
// add the id parameter
cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = id;
// declare the commandtype as a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
try
{
// open the connection
cn.Open();
// declare a data adapter
SqlDataAdapter da = new SqlDataAdapter(cmd);
// fill the datatable
da.Fill(dt);
}
catch (Exception ex)
{
DataExceptions.Add(ex.ToString());
}
finally
{
cn.Close();
}
return dt;
} // end GetByID