输入的字符串格式不正确(从十进制到字符串) [英] Input string was not in a correct format (Decimal to String)

查看:58
本文介绍了输入的字符串格式不正确(从十进制到字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

am试图将十进制转换为我过去已经成功完成的字符串,但是由于某种原因,它决定现在不工作.我真的无法理解它,我已经在SQL Management Studio中将其设置为十进制,并使用linq来诱使它通过它,但是由于人类未知的某些奇怪原因,它认为要求日期时间.

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.

代码:

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);
            }
        }

有人可以帮我吗?

推荐答案

首先,您应始终使用

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

第二,假设您使用某种形式的匈牙利表示法,并且tbx在您的表示法中表示文本框",那么您尝试转换的是文本框,而不是文本框中的文本.

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.

代替

tbxDecimal.ToString() 

您需要

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.

这篇关于输入的字符串格式不正确(从十进制到字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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