0

我有以下代码:

 If Page.IsPostBack = False Then
        ' If prospect is coming from unique url
        Dim prospect_url As String = Page.RouteData.Values("value")
        ' Save prospect_url into session variable
        Session("prospect_url") = prospect_url
        Using dbContext As IRFEntities = New IRFEntities
            ' Prepopulate the states dropdown.
            Dim getStates = (From p In dbContext.IRF_States _
                             Order By p.name _
                            Select p)
            ddlState.DataSource = getStates
            ddlState.DataTextField = "name"
            ddlState.DataValueField = "id"
            ddlState.DataBind()
            ' Grab info. about prospect based on unique url.
            Dim getProspect = (From p In dbContext.IRF_Prospects _
                              Where p.url = prospect_url _
                              Select p).FirstOrDefault
            ' If they have a record...
            If getProspect IsNot Nothing Then
                'If IsDBNull(getProspect.user_id) Then
                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.
                    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 If
        End Using
    End If

然后我直接在 ddlState.DataBind() 下面添加了以下内容:

  ' Prepopulate the programs dropdown.
             Dim getPrograms = (From p In dbContext.IRF_Program _
                              Order By p.name _
                             Select p)
            ddlProgram.DataSource = getPrograms
            ddlProgram.DataTextField = "name"
            ddlProgram.DataValueField = "id"
            ddlState.DataBind()

现在我得到错误:

ObjectContext 实例已被释放,不能再用于需要连接的操作

如果我注释掉插入的代码,代码就可以工作。为什么这段代码会导致问题?

4

1 回答 1

1

你丢失了这个对象:

dbContext

那就是这个对象的资源(包括连接操作)已经被处理和清理了。如果您需要数据绑定另一个下拉列表,您可以重新创建对象。

Dim dbContext as IRFEntities=Nothing

 Using dbContext= New IRFEntities
   //perform first databind
 End Using

 Using dbContext = New IRFEntities
  //code to perform second databind
 End Using
于 2012-09-11T18:19:10.163 回答