Insert DBNull.Value into Sql Money field fails. (You've got to love it!)

A

ahaupt

Hi all,

I'm trying to get a null value into a sql money field through a
SqlCommand in c#.

However, I get this lovely message (which sparks loads of happy
emotions in this overworked body):

Disallowed implicit conversion from data type nvarchar to data type
money, table 'xxxx', column 'xxxx'. Use the CONVERT function to run
this query.

I tried inserting good ol' "null" values, without any luck.

Anyone ever came accross something like this?

Best,
Andre
 
G

Guest

Assuming the field accepts NULLs ... I believe all the Sql* types have a
..Null static field, so the parameter value would be SqlMoney.Null

Alternativly you could create the parameter as a SqlMoney type - one of the
overlopaded constructors - and not sets its value, so it should default to
NULL
 
D

Denny Boynton

Andre,

I haven't seen this since my classic ASP days when I used to use a lot
of embedded SQL. Every time this error came up, it's because I wanted
to set a value to NULL, but I was passing it as a string and this
caused a type mismatch error to occur.

Are you using embedded SQL or a stored procedure in this case? If
you're using embedded SQL, are you using parameterized SQL or just
building a string on the fly? If it's the later, please post a
"scrubbed" version of the SQL so we can have a look at the query you're
trying to execute.

Thanks,
Denny Boynton
 
J

Jon Skeet [C# MVP]

I'm trying to get a null value into a sql money field through a
SqlCommand in c#.

However, I get this lovely message (which sparks loads of happy
emotions in this overworked body):

Disallowed implicit conversion from data type nvarchar to data type
money, table 'xxxx', column 'xxxx'. Use the CONVERT function to run
this query.

I tried inserting good ol' "null" values, without any luck.

Anyone ever came accross something like this?

Well, how are you trying to insert it at the moment? Does your
SqlCommand parameter definitely know that it's a money field?
 
A

ahaupt

Hi all,

Thanks for all the help and suggestions.

I replaced SqlMoney.Null with DBNull.Value (in the relevant
SqlParameter in the SqlCommand), which did the trick.

Many thanks,
Andre
 

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

Top