none
tableadapter - updating a column to null RRS feed

  • Question

  • I am attempting to update a column to null in a DataTable.  I do not know, or understand the syntax for this.  I posted a piece of my code as an example of an update.  The column in question is a decimal column so it does not like an empty string.  ddlClientID & txtContactPhoneAreaCode are a drop down and text field in a web form.

     

     

    Dim adapter As New ClientTableAdapter

    adapter.GetClientByClientID(ddlClientID.SelectedItem.Value)

     

    Dim Clients As TEST.ClientDataTable

    Clients = adapter.GetClientByClientID(ddlClientID.SelectedItem.Value)

    For Each client As TEST.ClientRow In Clients

     

    Dim test As integer

    If txtContactPhoneAreaCode.Text = "" Then

    Else

    test = txtContactPhoneAreaCode.Text

    End If

    client.ContactPhoneAreaCode = test

    Next

     

     

    adapter.Update(Clients)

    Thursday, June 21, 2007 10:05 PM

All replies

  • Hi,

     

    I think you want to assign test directly to null or Nothing, it should pick that up as a null.  The following two references seem to indicate that this is how you work with nulls in queries and updates:

     

    http://msdn2.microsoft.com/en-us/library/ms233762(VS.80).aspx
    http://msdn2.microsoft.com/en-us/library/ms364060(vs.80).aspx

     

     

    Hope this helps,

     

    John (MSFT)

    Friday, June 22, 2007 12:02 AM
  • To assign NULL value to the column, you need to use DBNull.Value. But I am not sure if strongly typed DataTable allows assigning NULL values to the numeric columns. If it is then your code should look like

     

     

    Dim adapter As New ClientTableAdapter

    adapter.GetClientByClientID(ddlClientID.SelectedItem.Value)

     

    Dim Clients As TEST.ClientDataTable

    Clients = adapter.GetClientByClientID(ddlClientID.SelectedItem.Value)

    For Each client As TEST.ClientRow In Clients

     

    If txtContactPhoneAreaCode.Text = String.Empty Then

             client.ContactPhoneAreaCode = DBNull.Value

    Else

    client.ContactPhoneAreaCode = Convert.ToDecimal(txtContactPhoneAreaCode.Text)

    End If

     

    Next

     

     

    adapter.Update(Clients)

    Friday, June 22, 2007 3:07 AM
    Moderator