Asp.net 是否可以将字符串转换为GUID?
我有一个下拉列表选择,选择的值将是ID,类型是GUID,当前我的选择按钮代码是Asp.net 是否可以将字符串转换为GUID?,asp.net,sql,vb.net,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 =
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")
然后我需要执行sql,比如select*from。。其中ID=…
问题发生在ID=Session(“Selected”)
,因为ID
是GUID
,而Session(“Selected”)
是string
我想知道是否有办法处理它?
非常感谢你的帮助 试试这个方法:
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
}
我希望这有帮助。祝您好运。我尝试过的ID=Guid.Parse(会话(“已选”)可能重复,但调试时出现错误,提示值不能为null。参数名称:input?第一次检查会话对象是否为空。如果它是空的,您必须检查设置它的方法。我猜默认值是空的?开始时,dropdownlist中未选择任何内容。我猜如果我将ID设置为GUID类型,那么我必须为dropdownlist设置一个notnull默认值?您可以将dd列表的初始值设置为null。当您测试它时,它为空,您知道这是初始选择,“请选择…”非常感谢您的帮助!