Solved

Setting string values to NULL before executing an INSERT using a TableAdapter

Posted on 2014-09-22
4
338 Views
Last Modified: 2014-10-02
I've create a function to INSERT a record into my database.  I want to check to see if any of the values passed to the function are "nothing" and if they are, set their values to NULL before executing the INSERT to my database.

My IF... THEN... code is wrong.  I know it is.  But I not sure how to fix it so I does what I want it to do.   Can you help fix my code please?

    Public Function InsertNewClient( _
            ByVal CustomerID As String, _
            ByVal CustomerName As String, _
            ByVal AddressLineOne As String, _
            ByVal AddressLineTwo As String, _
            ByVal AddressLineThree As String, _
            ByVal AddressLineFour As String, _
            ByVal TelephoneNumber As String, _
            ByVal FaxNumber As String, _
            ByVal WebsiteAddress As String) As Int32


        If CustomerName Is Nothing Then CustomerName = DBNull.Value
        If AddressLineOne Is Nothing Then AddressLineOne = DBNull.Value
        If AddressLineTwo Is Nothing Then AddressLineTwo = DBNull.Value
        If AddressLineThree Is Nothing Then AddressLineThree = DBNull.Value
        If AddressLineFour Is Nothing Then AddressLineFour = DBNull.Value
        If TelephoneNumber Is Nothing Then TelephoneNumber = DBNull.Value
        If FaxNumber Is Nothing Then FaxNumber = DBNull.Value
        If WebsiteAddress Is Nothing Then WebsiteAddress = DBNull.Value


        ' Setting the initial value of RowsAffected to 0
        Dim RowsAffected As Nullable(Of Integer) = 0


        ' Once this is called the method for the OUTPUT parameter is ByRef 
        ' thus returning the OUTPUT paramter value 
        Adapter.InsertNewClient(CustomerID, CustomerName, AddressLineOne, AddressLineTwo, AddressLineThree, AddressLineFour, TelephoneNumber, FaxNumber, WebsiteAddress, RowsAffected)


        ' We then return the RowsAffected value returned 
        ' when our stored procedure above was called.
        Return RowsAffected

    End Function

Open in new window

0
Comment
Question by:cdemott33
  • 2
4 Comments
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
What is Adapter? Is it an object?

What is InsertNewClient? Is it a method of Adapter?

Do post the code for Adapter and InsertNewClient.
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Strings can be empty but not nothing. Do you want to insert NULL when string is ""?

Otherwise, you will have to change the method to accept objects instead of strings.
0
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
Can try similar to below assuming objCmd is a SqlCommand object.

objCmd.Parameters.AddWithValue("@CustomerName", IIf(Not String.IsNullOrEmpty(CustomerName), CustomerName, System.DBNull.Value))

Open in new window

0
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
Comment Utility
Just set Adapter.AllowDbNull = True (no need IF...Then statement - adapter will convert Nothing to DBNull itself)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now