We still have to add the code so that when a user selects an
item in the drop down list the system will retrieve the record and display the
information on the web page. This will be done in the SelectedIndexChanged
event.
1.
Switch back to Design view and double click on the Users drop down
list. This should create the SelectedIndexChanged event handler.
2.
Add the following code.
if (ddlUsers.SelectedValue == "")
{
txtFirstName.Text = "";
txtLastName.Text = "";
lblInserted.Text = "";
lblUpdated.Text = "";
}
else
{
//Get the user from the DB
using (OrderDBContainer db = new OrderDBContainer())
{
int userAccountId = Convert.ToInt32(ddlUsers.SelectedValue);
List<UserAccount> userAccounts = (from u in db.UserAccounts
where u.Id == userAccountId
select u).ToList();
if (userAccounts.Count() > 0)
{
UserAccount userAccount = userAccounts[0];
txtFirstName.Text = userAccount.FirstName;
txtLastName.Text = userAccount.LastName;
lblInserted.Text = userAccount.AuditFields.InsertDate.ToString();
lblUpdated.Text = userAccount.AuditFields.UpdateDate.ToString();
}
else
{
//Error: didn't find user.
txtFirstName.Text = "";
txtLastName.Text = "";
lblInserted.Text = "";
lblUpdated.Text = "";
}
}
}
This code uses another LINQ query to retrieve a single
record from the database based on the Id of the selected item in the drop down
list. If the record is found then the userAccounts count property would be
greater than one. You can then access the object by using the indexer. The
textboxes and labels are set to the properties of the object.
If you run the project now you should be able to pull up the
records that were added previously and then update them.