Casting problem String -> Guid
-
I have a DropDownList where the ValueField is a guid as a string. I then need to pass this as a guid into a stored procedure. If I try and cast a string to a guid I get the following error: Value of type 'String' cannot be converted to 'System.Guid'. What do I need to do to get this to work? Jim
-
I have a DropDownList where the ValueField is a guid as a string. I then need to pass this as a guid into a stored procedure. If I try and cast a string to a guid I get the following error: Value of type 'String' cannot be converted to 'System.Guid'. What do I need to do to get this to work? Jim
The String object can't convert a string to a GUID because it doesn't know how. However, the GUID object does know how to parse up certain format strings into GUID's. The following code segment demonstrates this:
Dim guidString As String = "936DA01F-9ABD-4d9d-80C7-02AF85C822A8" Dim objGUID As Guid objGUID = New System.Guid(guidString) Debug.WriteLine("String GUID: " & guidString) Debug.WriteLine("GUID object: " & objGUID.ToString)
Documentation on the GUID object can be found here[^]. This same documentation can also be found in the Visual Studio help, in the Index, under 'Guid'. RageInTheMachine9532
-
The String object can't convert a string to a GUID because it doesn't know how. However, the GUID object does know how to parse up certain format strings into GUID's. The following code segment demonstrates this:
Dim guidString As String = "936DA01F-9ABD-4d9d-80C7-02AF85C822A8" Dim objGUID As Guid objGUID = New System.Guid(guidString) Debug.WriteLine("String GUID: " & guidString) Debug.WriteLine("GUID object: " & objGUID.ToString)
Documentation on the GUID object can be found here[^]. This same documentation can also be found in the Visual Studio help, in the Index, under 'Guid'. RageInTheMachine9532
Great! Thanks for you help, I was going down the wrong track trying to cast a string. I managed to get this to work but I guess the datakeys are already in the correct format. Dim FileUID As Guid = CType(dgMyDataGrid.DataKeys(e.Item.ItemIndex), System.Guid) Jim