Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
| Search | Today's Posts | Mark Forums Read
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 16th, 2003, 01:51 PM
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 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Foreign key not updating with Primary key xavier1945 BOOK: Access 2003 VBA Programmer's Reference 2 July 4th, 2007 09:48 PM
Composite Foreign Key bijgupt SQL Server 2005 6 May 4th, 2007 08:46 AM
Unique Record Identifier peace Access VBA 1 December 6th, 2005 12:03 PM
FOREIGN KEY and PRIMARY KEY Constraints junemo Oracle 10 June 15th, 2004 01:00 AM
Foreign Key Problem re_anu SQL Server 2000 1 September 26th, 2003 10:58 AM

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