Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

25 Experts available now in Live!

Get 1:1 Help Now