简体   繁体   中英

Add a value in Textbox and label in C#

I've been trying to add a value in textbox and a label. the value inside label is automatically generated during page load..it can be negative, positive or zero (decimal too). When I'm trying to add it with the textbox value I get the following error. Below is the error and code.

Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format

SqlCommand cmd6 = new SqlCommand("update dues set amount = @due where person='rahul'", con);
            cmd6.Parameters.Add("@due", SqlDbType.Int);
            cmd6.Parameters["@due"].Value = int.Parse(txt_rahul.Text + lbl_rahul.Text);
            cmd6.ExecuteNonQuery();

try this

SqlCommand cmd6 = new SqlCommand("update dues set amount = @due where person='rahul'", con);
            cmd6.Parameters.Add("@due", SqlDbType.Int);
            cmd6.Parameters["@due"].Value = Convert.ToDouble(txt_rahul.Text) + Convert.ToDouble(lbl_rahul.Text);
            cmd6.ExecuteNonQuery();

txt_rahul.Text + lbl_rahul.Text would concatenate the string 's together, it wouldn't add them up.

You would need to parse each one into an integer first. As in:

int something = int.Parse(txt_rahul.Text);
int somethingElse = int.Parse(lbl_rahul.Text);
cmd6.Parameters["@due"].Value = something + somethingElse;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM