Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SqlDBType.Decimal in Prepared Statement C#

Tags:

c#

asp.net

am using a Prepared Statement in C#.

 SqlCommand inscommand = new SqlCommand(supInsert, connection);
 inscommand.Parameters.Add("@ordQty", SqlDbType.Decimal,18);
 inscommand.Prepare();
 u = inscommand.ExecuteNonQuery();

The above code throws below Exception:

SqlCommand.Prepare method requires parameters of type 'Decimal' have an explicitly set Precision and Scale.

EDIT: How to avoid this Exception

like image 524
Suave Nti Avatar asked Jan 06 '12 13:01

Suave Nti


1 Answers

The following would set a Decimal with Precision 18 and Scale 8 (Decimal (18,8))

SqlCommand insertCommand= new SqlCommand(supInsert, connection);
insertCommand.Parameters.Add("@ordQty", SqlDbType.Decimal,18);

insertCommand.Parameters["@ordQty"].Precision = 18;
insertCommand.Parameters["@ordQty"].Scale = 8;

insertCommand.Prepare();
u = insertCommand.ExecuteNonQuery();
like image 72
Mithrandir Avatar answered Sep 28 '22 08:09

Mithrandir