简体   繁体   English

在文本框中添加一个值,并在C#中添加标签

[英]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. 说明:执行当前Web请求期间发生未处理的异常。 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 异常详细信息:System.FormatException:输入字符串的格式不正确

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. txt_rahul.Text + lbl_rahul.Text会将string串联在一起,不会将它们加起来。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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