Question

i've hit a minor problem. My application so far verifies a users login credentials using a sql server datbase and redirects them to a welcome.aspx page. I've also created a 'PlayerDetails.aspx' page which I'd like to use for displaying their personal details (obtained during the registration process) in txt boxes.

I feel like I'm having a very obvious blank moment here but how do a create a player.cs object at the login stage, and use that object on the PlayerDetails.aspx page, to populate the fields?

My code snippets are below, but if you require further clarification please let me know.

thanks Jason

Login Page

protected void btnLogin_Click(object sender, EventArgs e)
    {
        userlogin = new userlogin(txtUserName.Text, txtPassword.Text);

        var exists = login.ValidateLogin();
        if (exists == 1)
        {         
            //shall I create a new 'Player.cs' object here??
            FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);
        }
        else
        {

            lblMessage.Text = "Invalid username and/or password";
        }
    }

Player.cs

My database contains the values per unique GUID

public class Player : Credentials
{
    public Guid AccountID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

PlayerDetails.aspx

<asp:Content ID="Content1" runat="server" contentplaceholderid="ContentPlaceHolder1">
<table>
    <tr>
        <td>First Name</td>
        <td><asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td>
    </tr>

    <tr>
        <td>Last Name</td>
        <td><asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td>
    </tr>

    <tr>
        <td>Last Name</td>
        <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
    </tr>
</table>

PlayerDetails.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
    {
        //id like to set the values of the txt boxes to those in my Player object, not yet created
    }
Was it helpful?

Solution

There are a couple of approaches, with varying trade-offs. Probably the two most common are:

Fetch the object from backing storage when you need it. Basically on Page_Load you'd get the data you need using the identifier you have:

protected void Page_Load(object sender, EventArgs e)
{
    var player = SomePlayerRepository.GetPlayer(User.Identity.Name);
    txtFirstName.Text = player.FirstName;
    // etc.
}

The SomePlayerRepository can be implemented in any number of ways. It can include in-memory caching so that it doesn't have to hit the database over and over again, for example.

Or:

Fetch the object initially and store it somewhere local to the application. This is very similar to the "caching" mentioned above, it's just more explicit and less abstracted. For example, you might store it in Session:

var exists = login.ValidateLogin();
if (exists == 1)
{         
    Session["CurrentPlayer"] = SomePlayerRepository.GetPlayer(txtUserName.Text);
    FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);
}

then in Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
    var player = (Player)Session["CurrentPlayer"];
    txtFirstName.Text = player.FirstName;
    // etc.
}

Other options could include storing it in the user's cookie.

Personally I prefer the first approach because it separates the fetching/caching/etc. logic from the page logic, providing a common application-wide (or domain-wide perhaps) interface for the pages to use. This decouples different components of logic allowing you to modify/replace components with minimal effort.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top