Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm new to C#
I want to increment textbox when data from another table is adding .when i'm doing even after closing them number increases

What I have tried:

(data is in dataReader)
C#
string val = dataReader[0].ToString();
 int value = int.Parse(dataReader[0].ToString()) + 1;
 textBox3.Text = value.ToString();
Posted
Updated 23-May-21 22:45pm
v2
Comments
Richard MacCutchan 24-May-21 3:55am    
Not enough context in your question, as you have not shown how and when this code is invoked.

1 solution

If you are trying to do a sum in a text box, then start by not using a DataReader - they are sequential and only allow you to process through the results in order: you can't "rewind" and go back.
Instead, consider using a DataAdapter to file a DataTable (if you are accessing information from a DB for example) or a DataTable directly, and then use that to calculate your totals.
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT MyColumn1, MyColumn2 FROM myTable WHERE mySearchColumn = @SEARCH", con))
        {
        da.SelectCommand.Parameters.AddWithValue("@SEARCH", myTextBox.Text);
        DataTable dt = new DataTable();
        da.Fill(dt);
        myDataGridView.DataSource = dt;
        }
    }
If you keep numeric values in your DB, they will arrive in your DataTable as numbers as well.
Then the calculation is simple:
C#
int total = 0;
foreach (DataRow row in dt.Rows)
    {
    total += (int)row[0];
    }
myTextBox.Text = total.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900