Asp.net 转义可为null的对象必须具有值?
我有如下代码:Asp.net 转义可为null的对象必须具有值?,asp.net,vb.net,entity-framework,entity-framework-4,Asp.net,Vb.net,Entity Framework,Entity Framework 4,我有如下代码: Using dbContext as IRFEntities = New IRFEntities Dim getProspect = (From p in dbContext.IRF_Prospects _ where p.url = prospect_url _ Select p).FirstOrDefault If getProspect.user_id Is Nothing Then
Using dbContext as IRFEntities = New IRFEntities
Dim getProspect = (From p in dbContext.IRF_Prospects _
where p.url = prospect_url _
Select p).FirstOrDefault
If getProspect.user_id Is Nothing Then
' Prepopulate the form with their information.
' These must have a value, so we need to make sure that no column is null in the database.
ddlProgram.SelectedValue = getProspect.program
txtFirst.Text = getProspect.first_name
txtLast.Text = getProspect.last_name
txtAddress.Text = getProspect.address
txtCity.Text = getProspect.city
ddlState.SelectedValue = getProspect.state
txtZip.Text = getProspect.zip
txtPhone.Text = getProspect.phone
txtEmail.Text = getProspect.email_address
txtYearEnrolling.Text = getProspect.enrolling_in
Else
' Redirect them to login.
Response.Redirect("login.aspx")
End If
End Using
我得到以下错误:
用户代码未处理System.InvalidOperationException
HResult=-2146233079
Message=可为null的对象必须有一个值
在以下代码行中:
txtYearEnrolling.Text = getProspect.enrolling_in
我知道为什么会出现错误-数据库中的值为null。我想做的是,如果它存在,则提取该值,但如果它不存在,则我希望它要么采用默认值,要么将字段留空。实现这一目标的最佳方法是什么?很难说清楚,因为您没有定义什么是
getProspect。在中注册\u是一个非常困难的问题。什么是getProspect
,以及在中注册的类型是什么?在抛出异常时,该属性/字段的值是多少?我在原始问题中添加了额外的代码,希望能让事情更清楚。本质上,getProspect是针对IRF_Prospect的查询结果。如果有匹配的行,则代码重定向到登录页面(因为用户已经有帐户),如果没有,则使用信息预填充表单。我们在db里找到了他们。问题是,enrolling_in(或任何其他字段)中可能没有数据-它可能为null。如果enrolling_in是一种可为null的数据类型(看起来),则可以调用GetValueOrDefault()并指定默认值。或者使用空合并运算符(?)