0
當我有以下代碼需要連接的操作:的ObjectContext的實例已被設置,並且不再能夠用於添加額外下拉列表
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()
現在我得到的錯誤:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection
如果我註釋掉櫻雪有用的代碼,代碼有效。爲什麼此代碼導致問題?
我該如何重新創建對象? – davemackey
你也可以在兩個使用語句中使用相同類型的對象兩次(或嘗試catch塊)'使用dbContext作爲IRFEntities =新IRFEntities //更多代碼...使用dbContext2 AS IRFEntities =新IRFEntities //更多ddlsites'代碼 – JonH
看到我的編輯,只有在準備好使用它時,才應該新建對象。這樣你就可以將它聲明爲一個局部變量,然後在需要時新建它,並在需要時重新創建它。 – JonH