Windows: 7 Home Premium
Visual Studio: 2012
Language: C#
SQL: Server 2008
I am binding some labels and an Image to a DataReader, which is reading from a local DataBase.
The header label, and the Image URL of the image are binding properly, but the rest of the labels only get populated after I refresh the page.
The first image shows the page after I clicked on the user profile link. Notice that the 3 labels in the page have incorrect information:

After a full page refresh, the data is displayed properly:
The code goes as follows
private static string _acceptingChallenges = "0";
private static string _curUser;
private static string _website = "[None]";
private static string _dateJoined = DateTime.Now.ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["user"]))
{
_curUser = (Request.QueryString["user"]);
pnlProfilePublic.Visible = true;
pnlProfilePrivate.Visible = false;
PopulatePublic();
}
else
{
if (!string.IsNullOrEmpty((string) Session["Nickname"]))
{
_curUser = Session["Nickname"].ToString();
pnlProfilePublic.Visible = false;
pnlProfilePrivate.Visible = true;
PopulatePrivate();
}
else
{
Response.Redirect("~/Default.aspx");
}
}
InitiateData();
}
private void InitiateData()
{
if (Master == null) return;
Label lblTitle = (Label)Master.FindControl("lblTitle");
const string strSql = "SELECT * FROM vwGetProfileDetails WHERE memberNickname=@member";
var sqlComm = new SqlCommand(strSql, DataConn.Connect()) { CommandType = CommandType.Text };
sqlComm.Parameters.Add(new SqlParameter("@member", SqlDbType.VarChar, 20)).Value = _curUser;
var rdr = sqlComm.ExecuteReader();
int count = 0;
while (rdr.Read())
{
imgProfile.ImageUrl = rdr["memberAvatarLocation"].ToString();
lblTitle.Text = _curUser + "'s Profile Page";
_acceptingChallenges = rdr["memberAcceptingChallenge"].ToString();
_website = rdr["memberWebsite"].ToString();
_dateJoined = rdr["dateAdded"].ToString();
count = count + 1;
}
rdr.Close();
DataConn.Disconnect();
Response.Write(count);
}
PopulatePublic and PopulatePrivate are called before InitiateData… So they will be stored in the static strings and only populated next time the page is called.
If you are using ASP.Net controls then auto view state will probably be enabled so on postback the controls will be repopulated. So you can do lblWebsite.Text = rdr[“memberWebsite”].ToString(); and let ASP.NET manage viewstate.. no need for static strings..