C# 将数据类型“数字”转换为十进制时出错(帮助!)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18955465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:40:45  来源:igfitidea点击:

Error Converting data type 'Numeric' to Decimal (help!)

c#asp.nettsqlsql-server-2008-r2database-connection

提问by Albert Laure

Good Day Everyone,

今天是个好日子,

As of now im stuck with this error

截至目前,我坚持这个错误

Error Converting data type 'Numeric' to Decimal

this is my code

这是我的代码

AddManualItems AddReconItem = new AddManualItems();
        UserAccess user = new UserAccess();
        AddReconItem.BranchCode = BranchCodeTextBox.Text;
        AddReconItem.ForLoanMonth = YearDropDownList.SelectedValue + "/" + MonthDropDownList.SelectedValue;
        AddReconItem.ItemWeight = Convert.ToDecimal(WeightTextBox.Text);     
        AddReconItem.PrincipalAmount = Convert.ToDecimal(PrincipalTexAmTextBox.Text);
        AddReconItem.PawnTicket = PwnTicketTextBox.Text;
        AddReconItem.ItemStorageGroup = Convert.ToInt16(StorageNameDropDownList.SelectedValue);
        AddReconItem.ReconID = Convert.ToInt16(ReconTypeDropDownList.SelectedValue);
        user.UserID = Session["UserID"].ToString();

        string a = servs.AddItemRecon(user, AddReconItem); // this is where the error appears in debug mode

the code inside of the ADDitemRecon(User,AddReconItem) is this

ADDitemRecon(User,AddReconItem) 里面的代码是这样的

 using (SqlConnection reportsConn = new SqlConnection(sqlConnWriter))
            {
                reportsConn.Open();
                SqlCommand AddReconItem = new SqlCommand();
                AddReconItem.Connection = reportsConn;
                AddReconItem.CommandType = CommandType.StoredProcedure;
                AddReconItem.CommandText = "Updater.usp_AddReconcileItems";
                AddReconItem.Parameters.AddWithValue("@ITEMWEIGHT", Convert.ToDecimal( items.ItemWeight));
                AddReconItem.Parameters.AddWithValue("@ITEMPRINCIPALAMT", Convert.ToDecimal(items.PrincipalAmount));
                AddReconItem.Parameters.AddWithValue("@FORLOANMONTH", Convert.ToDateTime(items.ForLoanMonth));
                AddReconItem.Parameters.AddWithValue("@STORAGEGROUPID", items.ItemStorageGroup);
                AddReconItem.Parameters.AddWithValue("@BRANCHCODE", items.BranchCode);
                AddReconItem.Parameters.AddWithValue("RECONID", items.ReconID);
                AddReconItem.Parameters.AddWithValue("@PAWNTIX",items.PawnTicket);
                AddReconItem.Parameters.AddWithValue("@CREATEDBY", user.UserID.ToString());
                AddReconItem.ExecuteNonQuery();
            }

my property for ItemWeight is

我的 ItemWeight 属性是

 private decimal itemWeight;

    public decimal ItemWeight
    {
        get { return itemWeight; }
        set { itemWeight = value; }
    }

i bet the error is in the item weight because when i input in the item weight 12345.12 it works fine, but when i input 1234 instead of treating it as 1234.00 it treats it as 1234 only making it numeric..

我打赌错误出在物品重量上,因为当我输入物品重量 12345.12 时它工作正常,但是当我输入 1234 而不是将其视为 1234.00 时,它会将其视为 1234 仅使其成为数字..

any help? i do not know if my conversions are wrong

有什么帮助吗?我不知道我的转换是否错误

By the way my field in database is

顺便说一下,我在数据库中的字段是

fld_ItemWeight (decimal (38,6), not null

EDIT** Is there any bugs known in Decimal?? im using VS 2005 as of now.

编辑**十进制中是否有任何已知的错误??我现在使用的是 VS 2005。

采纳答案by Albert Laure

Found The answer! my stored Procedure is wrong i have decimal(9,6) in my stored procedured making it accept 3 digits or less !changed it to (18,2)\

找到答案了!我的存储过程是错误的,我的存储过程中有十进制(9,6),使其接受 3 位或更少!将其更改为(18,2)\

回答by Sain Pradeep

when you are calling a procedure and it has a parameter with a decimal datatype then you can pass only in fractions. For more details please refer Error converting data type numeric to decimal

当你调用一个过程并且它有一个十进制数据类型的参数时,你只能传递分数。有关更多详细信息,请参阅将数据类型数字转换为十进制的错误

回答by Ramesh Rajendran

I don't know what the proper solution for this issue , But i have one idea. Like

我不知道这个问题的正确解决方案是什么,但我有一个想法。喜欢

Change your ItemWeightproperty DataTypedecimal to string

将您的ItemWeight财产DataType十进制更改为字符串

  decimal xxxyyy = Convert.ToDecimal(WeightTextBox.Text); 

    if(!xxxyyy .ToString().Contains(".00))
    {
      AddReconItem.ItemWeight=xxxyyy.ToString() +".00";
    }
    else
    {
     AddReconItem.ItemWeight=xxxyyy.ToString() ;
    }

Edit :

编辑 :

Try this another way for use math.Round()

试试这个另一种使用方式 math.Round()

AddReconItem.ItemWeight = Convert.ToDecimal(WeightTextBox.Text);    
AddReconItem.ItemWeight=Math.Round(AddReconItem.ItemWeight,2);

回答by Richardissimo

Although the original poster has found his solution, for the benefit of other people, I thought I would post what caused it for me.

虽然原发帖人已经找到了他的解决方案,但为了其他人的利益,我想我会发布对我来说是什么原因。

System.Data.SqlClient.SqlException : Error converting data type numeric to decimal.

System.Data.SqlClient.SqlException:将数据类型数字转换为十进制时出错。

Sadly the message doesn't say which parameter it is having the problem with. So I changed all of the parameters that I was passing through to have a value of 0 (which is a suitable value for most SQL types, you may need to use other values), this made the error go away. I could then put them back to normal, one by one, and each time, I re-tested. That's how I worked out which parameter had the problem.

可悲的是,该消息没有说明哪个参数有问题。因此,我将传递的所有参数更改为 0(这是适合大多数 SQL 类型的值,您可能需要使用其他值),这使错误消失了。然后我可以将它们一个一个地恢复正常,并且每次都重新测试。这就是我如何确定哪个参数有问题。

It turned out that for one of the parameters the valuethat I had in my code (a C# decimal) was too large to go in the decimal(7,2) stored procedure parameter.

事实证明,对于其中一个参数,我在代码中的(C# 十进制)太大而无法放入 decimal(7,2) 存储过程参数。

回答by Lalitha1729

Try to round your value in set.

尝试在集合中舍入您的值。

private decimal itemWeight;

私有小数项重量;

public decimal ItemWeight
{
    get { return itemWeight; }
    set { itemWeight = Math.Round(value,yourPrecisionHere); }
}

回答by Radheshyam Gavhane

Change Sql column to Decimal(18,2), Same in store procedure and take SqlDbType.Decimal at ado.net end

将 Sql 列更改为 Decimal(18,2),在存储过程中相同,并在 ado.net 端取 SqlDbType.Decimal