逃避可空的对象必须有价值吗?

时间:2012-12-04 18:51:25

标签: 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
                    ' 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 = Nullable对象必须有值。

在以下代码行中:

txtYearEnrolling.Text = getProspect.enrolling_in

我知道为什么我收到错误 - 数据库中的值为null。我想要做的是拉取值,如果它存在,但如果它不存在,我希望它采取默认值或只是将该字段留空。实现这一目标的最佳方法是什么?

0 个答案:

没有答案