sum / add两个文本框 [英] sum/add two textboxes

查看:104
本文介绍了sum / add两个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在添加两个文本框中的值时遇到了一些问题。



textbox 1 =从sql获取值(例如24.00或2.00或4.00,...)

textbox 2 =用户输入值(例如24.00或2.00或4.00,......)



这是我得到的错误:

I have a little issue with adding values from two textboxes.

textbox 1 = gets value from sql (ex. 24.00 or 2.00 or 4.00,...)
textbox 2 = user enters value (ex. 24.00 or 2.00 or 4.00,...)

This is the error i get:

Input string was not in a correct format.





我尝试了很多不同的东西,转换,十进制,tryparse,我不能figuer我可以做些什么来解决这个问题。



请帮忙。

谢谢。



更新:修复了值。添加或我有,。





I have tried many different things, convert, decimal, tryparse, and i cant figuer out what i can do to fix this.

Please help.
Thank You.

UPDATE: fixed the values. Added "or" where i had ",".

int totalAccwPrev = Convert.ToInt32(txtPrevRecd.Text) + Convert.ToInt32(txtAQty.Text);
int qtyOrd = Convert.ToInt32(txtQtyOrd.Text);

if (totalAccwPrev > qtyOrd)
{
    if (ddRCodeList.SelectedValue == "")
    {
        ShowPopUpMsg("Please select appropriate Rejected Code.");
        return;
    }
}

推荐答案

如果您的数据库返回的字面值

If your database is returning values which are literally
24.00,2.00,4.00



那么你有多个值,你不能将它们存储在一个变量中 - =你需要为每个数字单独存储一个在序列中。

处理此问题的一种方法是使用Split:


Then you have multiple values, and you can't store those in a single variable -= you need a separate one for each number in the sequence.
One way to handle this is to use Split:

string s1 = "24.00,2.00,4.00";
string s2 = "14.00,12.00,8.00";
string[] parts1 = s1.Split(',');
string[] parts2 = s2.Split(',');
for (int i = 0; i < Math.Min(parts1.Length, parts2.Length); i++)
    {
    float f1, f2;
    if (float.TryParse(parts1[i], out f1) && float.TryParse(parts2[i], out f2))
        {
        Console.WriteLine("{0} + {1} = {2}", f1, f2, f1 + f2);
        }
    }



但您需要查看数据库并将其更改为不存储逗号分隔值!


But you need to look at your DB and change it to not store comma separated values!

你好..



尝试首先将文本框值转换为十进制然后转换为整数...



Hello..

Try to first convert textboxes value into decimal then into integer...

int totalAccwPrev = Convert.ToInt32(Convert.ToDecimal("5.00") + Convert.ToDecimal("2.00"));
           int qtyOrd = Convert.ToInt32(Convert.ToDecimal("4.00"));





转换为整数只得到'之前的十进制ir值的整数部分' 。



希望这会有所帮助。

谢谢



convert to integer get only integer part from the decimal i.r value before '.'

Hope this helps.
Thanks


textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());


这篇关于sum / add两个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆