1

I have a dropdownlist selection, selected value will be ID, type is GUID, currently my code for the select button is

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim Selection As String = Nothing

    If Not DropDownList3.SelectedValue Is Nothing Then Selection = DropDownList3.SelectedValue

    Session("Selected") = Selection

End Sub

then I have

Dim ID is guid
ID = Session("Selected")

then i need to execute sql such as select * from .. where ID=.. Problem happens at ID = Session("Selected"), as ID is GUID, while Session("Selected") is string I wonder if there is a way to handle it? Thanks very much for help!

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
user3344443
  • 475
  • 2
  • 11
  • 29
  • possible duplicate of [C#: Test if string is a guid without throwing exceptions?](http://stackoverflow.com/questions/104850/c-test-if-string-is-a-guid-without-throwing-exceptions) – TFD Feb 24 '14 at 00:37

1 Answers1

3

Try this method:

GUID myGuid;
object myObj = Session("Selected");

if (myObj != null && Guid.TryParse(myObj.ToString(), out myGuid))
{
   //input is good, do stuff here
}
else
{
   //input is bad, handle error
}

I hope this helps. Good luck.

kryptonkal
  • 874
  • 8
  • 23