p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB Databases Basics (http://p2p.wrox.com/forumdisplay.php?f=82)
-   -   Updating Unique Identifier foreign key to NULL (http://p2p.wrox.com/showthread.php?t=611)

jraymond June 16th, 2003 01:51 PM

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 system.data.dll

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

All times are GMT -4. The time now is 02:11 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.