Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Closed Thread
 
Thread Tools Search this Thread Display Modes
  #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?

Thanks!

-Joshua

Here is the relevant code:

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
        Else
            sqlParm.Value = Fuel.SelectedValue
        End If
        objCommand.Parameters.Add("@EventID", BindingContext(EventDV).Current("EventID"))
        EventsCon.Open()
        Try
            objCommand.ExecuteNonQuery()
        Catch e As SqlException
            MsgBox(e.Message + e.ToString)
        End Try
        EventsCon.Close()

        FillDataSetAndView()
        BindFields()

        EventCM.Position = intPosition
End Sub
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:05 AM.


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