Link to home
Start Free TrialLog in
Avatar of cdemott33
cdemott33Flag for United States of America

asked on

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

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

Avatar of hongjun
hongjun
Flag of Singapore image

What is Adapter? Is it an object?

What is InsertNewClient? Is it a method of Adapter?

Do post the code for Adapter and InsertNewClient.
Avatar of Nasir Razzaq
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.
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

ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial