I'm doing inserts into a table where I pass a byte[] array to a
varbinary(max) column. The colums is NULLS OK.
when I have a jpg it works great. but if the byte[] is null I get an error
saying
argument @.Photo expects a value.
Why won't it accept null?
Regards,
Gary Blakely
Try passing DbNull.Value instead of a .Net null.
Hope this helps.
Dan Guzman
SQL Server MVP
"GaryDean" <GaryDean@.newsgroups.nospam> wrote in message
news:OVG7$PU2HHA.4004@.TK2MSFTNGP05.phx.gbl...
> I'm doing inserts into a table where I pass a byte[] array to a
> varbinary(max) column. The colums is NULLS OK.
> when I have a jpg it works great. but if the byte[] is null I get an
> error saying
> argument @.Photo expects a value.
> Why won't it accept null?
> --
> Regards,
> Gary Blakely
>
|||Sorry but I didn't do a very good job of stating my problem. Let me try
again...
This is my code where Photo is a byte[]...
if (photo == null)
{
cmd.Parameters.Add(new SqlParameter("@.photo", Convert.DBNull));
}
else
{
cmd.Parameters.Add(new SqlParameter("@.photo", photo));
}
I have successfully used this technique on DateTime types using
Convert.DBNull and that worked fine. But Convert.DBNull won't work for the
Varbinary. Your suggestion to use DBNull.Value yields the following error...
"Implicit conversion from data type nvarchar to varbinary(max) is not
allowed. Use the CONVERT function to run this query."
?
Gary
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:eLwHVYV2HHA.600@.TK2MSFTNGP05.phx.gbl...
> Try passing DbNull.Value instead of a .Net null.
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "GaryDean" <GaryDean@.newsgroups.nospam> wrote in message
> news:OVG7$PU2HHA.4004@.TK2MSFTNGP05.phx.gbl...
>
|||> "Implicit conversion from data type nvarchar to varbinary(max) is not
> allowed. Use the CONVERT function to run this query."
Charles already pointed out that you can specify the parameter data type in
the SqlParameter constructor to avoid implicit conversion. I want to add
that an important clue about the cause of the error is the reference to
nvarchar in the error message. IMHO, the Best Practice is to always specify
the parameter data type that matches the data type on the server.
Hope this helps.
Dan Guzman
SQL Server MVP
"Tina" <TinaMSeaburn@.nospamexcite.com> wrote in message
news:%23vGjc9t2HHA.6128@.TK2MSFTNGP02.phx.gbl...
> Sorry but I didn't do a very good job of stating my problem. Let me try
> again...
> This is my code where Photo is a byte[]...
> if (photo == null)
> {
> cmd.Parameters.Add(new SqlParameter("@.photo", Convert.DBNull));
> }
> else
> {
> cmd.Parameters.Add(new SqlParameter("@.photo", photo));
> }
> I have successfully used this technique on DateTime types using
> Convert.DBNull and that worked fine. But Convert.DBNull won't work for
> the Varbinary. Your suggestion to use DBNull.Value yields the following
> error...
>
> "Implicit conversion from data type nvarchar to varbinary(max) is not
> allowed. Use the CONVERT function to run this query."
> ?
> Gary
> "Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
> news:eLwHVYV2HHA.600@.TK2MSFTNGP05.phx.gbl...
>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment