Input string was not in a correct format (Decimal

2019-08-17 23:52发布

am trying to convert a decimal to a string which I have done sucesfully in the past but for some reason its deciding not to work now. I really can't get my head around it, I have set it to decimal in SQL Management Studio and used linq to entites to pass it through but for some bizarre reason unknown to mankind it thinks am asking for a datetime.

Code:

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                tblTest t = new tblTest();

                t.tDecimal = Convert.ToDecimal(tbxDecimal.ToString());

                t.Add(t);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
            }
        }

Can someone help me out here?

5条回答
你好瞎i
2楼-- · 2019-08-18 00:10

Perhaps it is because the string uses a dot instead of a coma, or the other way around.

查看更多
再贱就再见
3楼-- · 2019-08-18 00:12

To expand on Fredrik's answer: is there a CultureInfo/Localization mismatch?

查看更多
干净又极端
4楼-- · 2019-08-18 00:12

Why do you think it's datetime? Could you post the actual string?

Pay attention to decimal separator - '.' vs. ','

查看更多
淡お忘
5楼-- · 2019-08-18 00:17

First, you should always use System.Decimal.TryParse() instead of Convert.ToDecimal()

Second, assuming you're using some form of hungrian notation, and tbx means "textbox" in your notation, you're trying to convert a textbox, not the text IN the textbox.

instead of

tbxDecimal.ToString() 

you need

tbxDecimal.Text.ToString() 

Finally, have you put a breakpoint in to find out what the value you're trying to convert really is? It may be different than you're expecting.

查看更多
戒情不戒烟
6楼-- · 2019-08-18 00:22

Perhaps tbxDecimal.ToString()== string.Empty ? Decimal.Parse will throw an exception if the string is empty

查看更多
登录 后发表回答