Solved

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

Posted on 2014-09-22
4
369 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 28

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…

790 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