CONVERT c# decimal to SQL ???

  • Thread starter Thread starter trint
  • Start date Start date
T

trint

I have a decimal amount in c#:

decimal amounT = 62.75;

and I need INSERT it into a SQL record as a money value.

string strSQL2 = "INSERT INTO trvlDetail(amountfield) ""VALUES ('" +
amounT + "') ";

Should I use a CONVERT in SQL (if so, how?) or what would be the
equivelent in c#?
Thanks,
Trint
 
Hi Trint,

Use an SqlParameter, it will convert for you


decimal amounT = 62.75;

SqlParameter param = new SqlParameter(@amounT, amounT);

string strSQL2 = "INSERT INTO trvlDetail(amountfield) VALUES (@amounT)";

SqlCommand insertCommand = new SqlCommand(strSQL2, SqlConnection1);

insertCommand.Parameters.Add(param);

SqlConnection1.Open();
insertCommand.ExecuteNonQuery();
SqlConnection1.Close();
 
Ok, I did as you said and got this error:

"Disallowed implicit conversion from data type varchar to data type
money, table 'tsNess.dbo.trvlDetail', column 'amount'. Use the CONVERT
function to run this query."

..Net programmer
(e-mail address removed)
 
Back
Top