Asp.net 绑定前检查输出参数是否为空

Asp.net 绑定前检查输出参数是否为空,asp.net,vb.net,Asp.net,Vb.net,在将输出参数绑定到asp文本之前,如何检查输出参数是否为null,如果为null,则只需生成文本 hname1.Text = cmd.Parameters("@hotel1").Value hname1.DataBind() hname2.Text = cmd.Parameters("@hotel2").Value hname2.DataBind() hname3.Text = cmd.Param

在将输出参数绑定到asp文本之前,如何检查输出参数是否为null,如果为null,则只需生成文本

hname1.Text = cmd.Parameters("@hotel1").Value
            hname1.DataBind()

            hname2.Text = cmd.Parameters("@hotel2").Value
            hname2.DataBind()

            hname3.Text = cmd.Parameters("@hotel3").Value
            hname3.DataBind()

            hname4.Text = cmd.Parameters("@hotel4").Value
            hname4.DataBind()

            hname5.Text = cmd.Parameters("@hotel5").Value
            hname5.DataBind()
我相信。语法可能有点毛茸茸的,因为它已经有点,因为我已经做了VB,但前提应该是相同的

这还假设
cmd.Parameters(“@hotel1”)
将始终是具有
值的可调用对象。如果这可能是空的,我们需要添加另一个比较以避免NullObjectReference异常

另外,我希望我能正确理解这个问题,你提到的“输出参数”没有子例程/函数,这让我相信语义上有点混乱

版本更改

  • 用于检查空值

从类型“DBNull”转换为类型“String”无效。这是我试图避免的错误,但在进行此更改后我仍然得到它:/Ah-ha,如果使用c#而不是VB,则等效检查为:If(cmd.Parameters(@hotel1”).Value!=System.DBNull.Value)
If Not IsDBNull(cmd.Parameters("@hotel1").Value) Then
  hname1.Text = cmd.Parameters("@hotel1").Value
  hname1.DataBind();
Else
  ' Manual binding would go here
End If