可以将字符串转换为GUID吗?

时间:2014-02-24 00:21:07

标签: asp.net sql vb.net

我有一个下拉列表选项,选中的值将是ID,类型是GUID,目前我的选择按钮的代码是

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

然后我

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

然后我需要执行select * from .. where ID=..等sql 问题发生在ID = Session("Selected")IDGUID,而Session("Selected")string 我想知道是否有办法处理它? 非常感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

试试这个方法:

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
}

我希望这会有所帮助。祝你好运。