10

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

 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

 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..

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.

7 Answers 7

14

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)\

7

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.

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.

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

1
  • 5
    just a reminder, (7,2) decimal means 7 is the length of the whole decimal and 2 of those is decimal, meaning to say your whole value will only consist of 5 numbers and 2 decimal places. Commented Sep 19, 2016 at 6:00
0

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

Change your ItemWeight property DataType decimal to string

  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()

AddReconItem.ItemWeight = Convert.ToDecimal(WeightTextBox.Text);    
AddReconItem.ItemWeight=Math.Round(AddReconItem.ItemWeight,2);
5
  • already tried but no luck, when converting to Decimal any 00 after the . is removed and i dont know why i just saw it in debug mode Commented Sep 23, 2013 at 10:52
  • a 112345.00 when converted returns 112345 that's the values that is being passed in the database and thats where the exception occurs maybe Commented Sep 23, 2013 at 10:53
  • @DanzaiVer Change your database column DataType decimal to nvarchar and try my solution . Commented Sep 23, 2013 at 11:17
  • if i do that what's the essence of decimal datatype? i know it would work but my database will suffer from retrieval of data expecially if i need to sum them all because i need to convert them first into decimal before i can sum them. Commented Sep 23, 2013 at 11:31
  • @DanzaiVer Okay leave that , see My Updated answer . Commented Sep 23, 2013 at 11:41
0

Try to round your value in set.

private decimal itemWeight;

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

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

0

My problem was that the column had the right precision and scale(16,2) but the variable I was using as a parameter in the procedure hadn't(14,2). So, when trying to insert something like 99999999999999 using the procedure, I was getting the error Error converting data type numeric to numeric.

-1

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

1
  • 1
    decimal (38,6) i have this, based on the articled you have given this should not generate any errors, fractions are only applicable to those who has a declaration of (2,2) same precision and same number of decimals allowed. Commented Sep 23, 2013 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.