View Single Post
  #1 (permalink)  
Old June 16th, 2003, 01:51 PM
jraymond jraymond is offline
Registered User
Join Date: Jun 2003
Location: , MI, .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Updating Unique Identifier foreign key to NULL

I have a VB front-end/SQL Server backend form with a combobox on it. The combobox updates a record with a field in it that is a foreign key to another table, with datatype of uniqueidentifier. It works fine if there is a value selected in the combobox. If there is not, I get an error:

An unhandled exception of type 'System.InvalidCastException' occurred in

Additional information: Invalid cast from Microsoft.VisualBasic.VariantType to System.Guid.

How can I insert a null into my sql table?



Here is the relevant code:

Private Sub UpdateRecord()
        Dim intPosition As Integer
        Dim objCommand As SqlCommand = New SqlCommand()
        Dim sqlParm As New SqlClient.SqlParameter()

        intPosition = EventCM.Position
        objCommand.Connection = EventsCon
        objCommand.CommandText = "UPDATE vwEvents SET Fuel = @Fuel WHERE EventID = @EventID"
        objCommand.CommandType = CommandType.Text

        sqlParm = objCommand.Parameters.Add("@Fuel", SqlDbType.UniqueIdentifier)
        If Fuel.SelectedIndex = -1 Then
            sqlParm.Value = VariantType.Null
            sqlParm.Value = Fuel.SelectedValue
        End If
        objCommand.Parameters.Add("@EventID", BindingContext(EventDV).Current("EventID"))
        Catch e As SqlException
            MsgBox(e.Message + e.ToString)
        End Try


        EventCM.Position = intPosition
End Sub