counter value not incremented !!!

G

Guest

Hi,

I have an ASP.NET page which uses C#. When the user clicks submit button,
all information goes to the SQL database & the page is postback to itself.

Now, I have a label whose text increments by 1 each time the submit button
is hit. This label is compared with a number the user enters in the txtRec
text box. But for the first hit of submit button, it dispalys 1 in the label.
But for subsequent hits of submit button, it still shows 1 !!!

I have initialized the value of label (in code) as 0. can it be aomething to
do with the controls' viewstate property?

My code is::

protected int x;
protected System.Web.UI.WebControls.TextBox txtTotalRec;
protected int y;
protected System.Web.UI.WebControls.Label lblDone;
protected int i=0;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
txtActivationDate.Text= System.DateTime.Today.Date.ToString();
}

private void btnSubmit1_Click(object sender, System.EventArgs e)
{
sqlCommand1.Parameters["@a"].Value= txtCompany.Text;
sqlCommand1.Parameters["@b"].Value=
System.DateTime.Parse(txtActivationDate.Text);
sqlCommand1.Parameters["@p"].Value= txtMrc.Text;
sqlCommand1.Parameters["@r"].Value= Int64.Parse(txtMin.Text);
sqlConnection1.Open();
sqlCommand1.ExecuteNonQuery();
sqlConnection1.Close();

i=i+1;
lblHold.Text=i.ToString();
x=int.Parse(lblHold.Text);
y= int.Parse(txtTotalRec.Text);
if(x<y)
{
fn();
}
else
{
lblDone.Text="You have entered all the records.";
}

private void fn()
{
lblConfirm.Visible= true;
txtMrc.Text=""; txtMin.Text="0";
}
 
G

Guest

Hi,

Try changing the declaration of protected int i=0; to protected static int
i=0;. This should preserve your value.

Hope this helps.
-----------------------
 
R

RadekP

Or better yet, save your counter across ViewStates when you want to make
sure you 'survive' aspnet_wp.exe recycles.

protected int Counter
{
get
{
object o = ViewState["counter"];
return o==null ? 0 : (int)o;
}
set
{
ViewState["counter"] = value;
}
}

Radek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top