using Vb.Net commit a null to a sql database using linq to SQL

In the code below, cusfile is a sql table and I am using linq to SQL

the line of code for .addr_2 compiles (I haven't tested yet to see if it works)
the line of code for .contact does not compile saying "value of type DBNull cannot be converted to string.

contact at the database level is allowed to be null

What am I doing wrong?

Thanks.
Dim cusN As cusfil_SQL

With cusN
    .addr_2 = CStr(IIf(String.IsNullOrEmpty(cusRec.add2), DBNull.Value, cusRec.add2))
    .contact = DBNull.Value
End With

dc.cusfil_SQLs.InsertOnSubmit(cusN)

Open in new window

LVL 4
g_johnsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel WilsonCommented:
The error is in your VB, not anything to do with the SQL. In line 4, you are passing DBNull.Value to CStr().

How does this work for you?
Dim cusN As cusfil_SQL

With cusN
    if String.IsNullOrEmpty(cusRec.add2) .addr_2 = DBNull.Value else .addr_2 = cusRec.Add2
    .contact = DBNull.Value
End With

dc.cusfil_SQLs.InsertOnSubmit(cusN)

Open in new window

0
g_johnsonAuthor Commented:
I now get the same error on both lines "value of type DBNull cannot be converted to string."
0
CodeCruiserCommented:
Try using the nullable of string.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

g_johnsonAuthor Commented:
I'm a C# person of intermediate skill only, and, unfortuantely don't know what you mean.  How do I code that?
0
CodeCruiserCommented:
0
g_johnsonAuthor Commented:
the link is to C# so don't know how to translate to VB.net, but I thought strings were nullable by default, i.e., we don't have to declare them like string? strSomething

and I think in c# we can just go:
if strSomething = null
     code here

I can't find the equivalent for VB.Net
0
CodeCruiserCommented:
>I'm a C# person of intermediate skill only
Hence I posted a C# link. I had this problem with LINQ2SQL and I had to set all my int columns to default to 0 to avoid such type cast errors. I am sure DBNull can not be translated to string.
0
käµfm³d 👽Commented:
Have you tried assigning Nothing to the member?

Dim cusN As cusfil_SQL

With cusN
    .addr_2 = CStr(IIf(String.IsNullOrEmpty(cusRec.add2), DBNull.Value, cusRec.add2))
    .contact = Nothing
End With

dc.cusfil_SQLs.InsertOnSubmit(cusN)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.