Results 1 to 7 of 7

Thread: Put a <NULL> in a SQL database

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354

    Put a <NULL> in a SQL database

    I am building a front end for a shrink-wrapped accounting program. I have one feild in my app that can sometimes contain a value and sometimes not. The problem that I am having is that the shrink-wrapped program will not accept an empty string in the field of the database.

    There has to be a value or it has to be a <NULL>. Empty strings such as "" cause it to error. Any ideas of how to accomplish this from my form?

    In my AddRecord Function I have this but none of these worked.
    VB Code:
    1. Dim strCityTax As String
    2.  
    3.         If txtCityTax.Text = "" Then
    4.             'strCityTax = vbNullString
    5.             'strCityTax = Convert.DBNull
    6.             'strCityTax = DBNull
    7.             'strCityTax = vbNullString.ToString
    8.         Else
    9.             strCityTax = txtCityTax.Text
    10.         End If
    Last edited by BukHix; Apr 7th, 2004 at 08:51 AM.

  2. #2
    Member
    Join Date
    Mar 2004
    Posts
    39
    You dont mention of what type strCityType is, but I guess that it is a string. If so, offcourse a string cannot be Null, a field in a database record can.

    What kind of datahandling do you use? ADO .NET?

    If you use ADO .NET, then a field in a DataTable can be nullable, and you should be able to set the value to Null if it is.

    /Nisse

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    I am using Imports System.Data.SqlClient, which I assume means that my data handling is ADO.Net.

    strCityType is a string but if the field is left blank I dont want anything for that field sent to the database. That is unless I can send something that will tell the database to keep the field a <NULL>. Because an empty string will not work for the application.

    Off the top of my head the only thing I can think of to get around this is to have two different Insert Statements and then test for a value in the txtCityTax text box. If there is something in that box send the Insert Statement with the txtCityTax data. If not send the Insert Statement without the data included. For instrance:

    VB Code:
    1. If txtcity.text = "" Then
    2.             With cmd
    3.                 .CommandText = "Insert Into myTable (" & _
    4.                     "Co, " & _
    5.                     "BatchTransType" & _
    6.                     ") Values(" & _
    7.                     intCo & ", " & _
    8.                     "'" & strBatchTransType & "')"
    9.                 .ExecuteNonQuery()
    10.             End With
    11.         Else
    12.             With cmd
    13.                 .CommandText = "Insert Into myTable (" & _
    14.                     "Co, " & _
    15.                      "BatchTransType, " & _
    16.                     "LocalCode" & _
    17.                     ") Values(" & _
    18.                     intCo & ", " & _
    19.                     "'" & strBatchTransType & "', " & _
    20.                     "'" & strCityTax & "'" & ")"
    21.                 .ExecuteNonQuery()
    22.             End With
    23.         End If
    It just seems like that is a horrible way to accomplish what I am trying to do. Is there an alternative?

  4. #4
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    try setting the variable to NOTHING( strCityTax = nothing)
    in the if block.

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    or try this :

    strCityTax = convert.DBNull

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    Thanks. Everything suggested made the field appear as an empty string in the database or cause it not to work inside the application.

    I went with the 2 Insert Statements, which seems to be working out ok.

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Yes, there are a few ways to do this.

    I often work with Stored Procedures, that default to null, so if you DO NOT explicity pass the value of a parameter to the procedure, it becomes null on the DB side.
    VB Code:
    1. If Not strCity.Text = String.Empty Then SqlCommand1.Parameters(0).Value = strCity.Text

    If there's something in the string, it get passed, if there is not, the stored proc defaults it null on the DB side.

    To me, I wouldn't want to write explicit commands inside the application code, which may be subject to SQL injection attacks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width