Error While inserting

S

sunilthk

Hi All,
I'm getting following error while executing insert query using
SqlCommand object with parameters
Insert Failed :Disallowed implicit conversion from data type
nvarchar to data type money

I'm adding Parameters using following syntax
SqlCommand cmd= new SqlCommand();
---
---
string myValue = "20"
cmd.Parameters.Add(new SqlParameter("@MyMoneyColName",myValue))
-----
cmd.ExecuteNonQry();

there is a Constructor of SqlParameter available
which takes SqlDbType as parameter, but the problem is that i don't
have parameter info, coz i'm writing these qry at run time.
Can some one please help me on this
Thanks
SunilT
 
S

sunilthk

Hi Justin,
you said "Don't declare myval as a string , use decimal myval = 20;"
but the problem is that i don't know the datatype of the parameter
being added to SQLParameters collection it could be money,varchar,date
etc.

Thanks
SunilT
 
S

S. Justin Gengo

Sunil,

In that case cast the the variable to a decimal as it's added.

cmd.Parameters.Add("@MyMoneyColName", SqlDbType.Money).Value = (decimal)
myValue;

Regards,


--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top