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