Results 1 to 6 of 6

Thread: [2008] DataSets, SQL Server and Null values

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    [2008] DataSets, SQL Server and Null values

    I have a strongly typed DataSet (dsContacts) that is bound to a textbox (txtFirstName). I use DataAdapters to Fill my DataSet with data from SQL Server stored procedures.

    If a user creates a contact with no First Name, it is initially Null in the database. If a user then edits the record and puts in a value, that value is in the database. If a user then removes the value and saves it back to SQL Server, the value in the database is an empty string. I want this value to be Null.

    I have a bunch of Update procedures that I would like this to happen with. Is there a way to force the DataSet to work this out rather than editing all of my SPs to have a NullIf() statement? Is there another approach that might be better?


    Thanks in advance!
    My.Settings.Signature = String.Empty

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2008] DataSets, SQL Server and Null values

    Maybe something like this
    Code:
            If txtFirstName.Text = "" Then
                dr("firstname") = DBNull.Value
            Else
                dr.firstname = txtFirstName.Text
            End If

  3. #3
    Fanatic Member Clanguage's Avatar
    Join Date
    Jan 2008
    Location
    North Carolina
    Posts
    659

    Re: [2008] DataSets, SQL Server and Null values

    In SQL you can give a default value to the column. Open the table in design view and give it a default of null.
    CLanguage;
    IF Post = HelpFull Then
    RateMe
    Else
    Say("Shut UP")
    End If
    DotNet rocks
    VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: [2008] DataSets, SQL Server and Null values

    Wild Bill,
    The control is Bound to the DataSet. This type of If...Then won't really work. Besides, I am looking for something far easier than this.

    In total including all of the SPs, there are probably over 100 places I would need to do this. I can do this in a simpler NullIf() statement in the SP.


    CLanguage,
    I just tried that as that would have been perfect. The only problem is that the DataSet passes the empty string to the SqlCommand object and not Default.


    It seems to me that it would have to be somewhere at the DataSet level. It dictates the entire interaction with the data.

    Thoughts?
    My.Settings.Signature = String.Empty

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] DataSets, SQL Server and Null values

    It feels like there should be an easier way but I don't think there is.

    1. Create a new WinForms project and add a TextBox and a Button to the form.
    2. Add this code:
    Code:
    Private table As New DataTable
    
    Private Sub Form1_Load(ByVal sender As Object, _
                           ByVal e As EventArgs) Handles MyBase.Load
        Me.table.Columns.Add("Name", GetType(String))
        Me.table.Rows.Add(DBNull.Value)
    
        Me.TextBox1.DataBindings.Add("Text", Me.table, "Name")
    End Sub
    
    Private Sub Button1_Click(ByVal sender As Object, _
                              ByVal e As EventArgs) Handles Button1.Click
        Dim value As Object = Me.table.Rows(0)("Name")
    
        If value Is DBNull.Value Then
            MessageBox.Show("Null")
        ElseIf CStr(value) = String.Empty Then
            MessageBox.Show("Empty String")
        Else
            MessageBox.Show(CStr(value))
        End If
    End Sub
    3. Run the project and click the Button.
    4. Enter some text and click the Button again.
    5. Clear the text and click the Button again.

    Note that you get "Null", "the text you entered", and "Empty String". That third message is obviously the issue.

    Now change the code to this:
    Code:
    Private table As New DataTable
    
    Private Sub Form1_Load(ByVal sender As Object, _
                           ByVal e As EventArgs) Handles MyBase.Load
        Me.table.Columns.Add("Name", GetType(String))
        Me.table.Rows.Add(DBNull.Value)
    
        Me.TextBox1.DataBindings.Add("Text", Me.table, "Name")
    
        AddHandler Me.TextBox1.DataBindings("Text").Parse, AddressOf ForceNullValueOnEmptyField
    End Sub
    
    Private Sub Button1_Click(ByVal sender As Object, _
                              ByVal e As EventArgs) Handles Button1.Click
        Dim value As Object = Me.table.Rows(0)("Name")
    
        If value Is DBNull.Value Then
            MessageBox.Show("Null")
        ElseIf CStr(value) = String.Empty Then
            MessageBox.Show("Empty String")
        Else
            MessageBox.Show(CStr(value))
        End If
    End Sub
    
    Private Sub ForceNullValueOnEmptyField(ByVal sender As Object, _
                                           ByVal e As ConvertEventArgs)
        If CStr(e.Value) = String.Empty Then
            e.Value = DBNull.Value
        End If
    End Sub
    and try the same steps as before. Tada!
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: [2008] DataSets, SQL Server and Null values

    Brilliant. Why didn't I think about using the Parse routine? I use it to handle int, bools, etc.

    Thanks. I appreciate everyone's help on this.
    My.Settings.Signature = String.Empty

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