Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

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

0
g_johnson
Asked:
g_johnson
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now