Solved

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

Posted on 2014-09-22
4
362 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
ID: 40338595
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
ID: 40338707
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
ID: 40338747
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
ID: 40345365
Just set Adapter.AllowDbNull = True (no need IF...Then statement - adapter will convert Nothing to DBNull itself)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

821 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