Results 1 to 5 of 5

Thread: Sometimes send a null value to SQL Server

  1. #1

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

    Sometimes send a null value to SQL Server

    I am sending values to my database from textboxes that are usualy all required texboxes. In one case I have a textbox that is optional but my Insert Into String looks something like the one listed below. The database requires that if there is no value in the textbox the field must be a null. How can I send a Null Value to my sql server in the cases that I need to do so?
    VB Code:
    1. With cmd
    2.     .CommandText = "Insert Into TimeGR (" & _
    3.             "EntryPerson, " & _
    4.                 "Job," & _
    5.                 "JobName," & _
    6.                 "WeekEnding," & _
    7.                 "Employee," & _
    8.                 "FirstName," & _
    9.                 "LastName," & _
    10.                 "RateTime," & _
    11.                 ") Values('" & _
    12.                 strEntryPerson & "','" & _
    13.                 strJobNumber & "','" & _
    14.                 strJobName & "','" & _
    15.                 strWeekEnd & "','" & _
    16.                 strEmp & "','" & _
    17.                 strFname & "','" & _
    18.                 strLname & "','" & _
    19.                 strTime & "','" & _
    20.                 ")"
    21.     .ExecuteNonQuery()
    22.         trn.Commit()
    23. End With
    I know that I could use an IF then along with two seperate Insert Strings but I am thinking there is probably a better way/

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Well you could
    A) depending on your database (if you are using SQL Server) create a stored procedure that checks if the value is empty ("") or not
    Code:
    CREATE PROCEDURE stp_MyProcedure
       @Value1 varchar(50),
       @Value2 varchar(50
    AS
    INSERT INTO MyTable VALUES(
       @Value1,
       Case @Value2
          When '' Then Null
          Else @Value2
       End)
    B) Create a procedure that accepts all of your parameters and allows the parameter that might be null as optional
    VB Code:
    1. Public Sub InsertValues(ByVal Value1 As String, ByVal Optional Value2 = Null)
    2.    'Run your insert, it will either insert the value passed or it will insert Null
    3. End Sub
    Or
    C) you could just write a simple If...Then..Else statment that checks the value prior to sending it to the database
    VB Code:
    1. If(Len(Value2.Text) = 0) Then
    2.    Value2 = Null
    3. Else
    4.    Value2 = Value2.Text
    5. End If
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Location
    Three Rivers, MI
    Posts
    354
    I originaly tried option C but I get some squigly lines along with this error:

    'Null' is not declared. 'Null' constant is no longer supported; use 'System.DBNull' instead.

    System.DBNull provides a similar error. Option A is above my current abilites and I think B will probably give me the same error as above.

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Your best bet is to check for zero-length strings at the SQL server.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You can only set DbNull.Value to a object that can accept it. A string can't accept a DbNull.Value.

    A parameter object can though...

    Dim param as New SqlParameter("@SomeValue", DbNull.Value)



    So, to fill this out, you can do something like this:
    Dim param as SqlParameter
    If Value2.Text.Length > 0 Then
    param = New SqlParameter("@SomeValue", Value2.Text)
    Else
    param = New SqlParameter("@SomeValue", DbNull.Value)
    End If


    You should be using stored procedures with parameters. Better, safer, and more descriptive. You can use the Data Access block from MS also to make things easier on you.
    Last edited by hellswraith; Feb 21st, 2004 at 01:57 PM.

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