I´m in need to pass some results from the database around. Instead of pass a whole dataset, or a constructed sql query, I tried to pass a LinqDataSource configured object.
On the source page, I configure the LinqDataSource object properties Where and WhereParameters using the values set by the user on the controls displayed on the page (search box, combo box etc.). I pass the object around to avoid the LinqDataSource reconfiguration on the receiver page.
But, even while it worked fine on my developing system, after deploying it on a dedicated server I got an error saying the following:
Cannot access a disposed object.
Object name: 'DataContext accessed after Dispose.'.
On both pages, the source and the receiver, the LinqDataSource objects are declared as:
<asp:LinqDataSource ID="myLinqDS" runat="server"
ContextTypeName="MyProject.MyDBNamedpace.MyDataContext"
TableName="TheTable"
OrderBy="field1, field2"
</asp:LinqDataSource>
To pass the datasource object around I do this:
SetLinqDataSourceWhereAndHisParameters(); //just set Where and WhereParameters
Guid guid = Guid.NewGuid();
Session[guid.ToString()] = myLinqDS;
ScriptManager.RegisterStartupScript(this.Page,
typeof(Page),
"Redirect",
String.Format(@"window.open(""{0}"");",
this.ResolveClientUrl("~/PageToDisplayTheData.aspx?guid=" +
System.Web.HttpUtility.UrlEncode(guid.ToString())
),
true);
And to retrieve, on PageToDisplayTheData.aspx´s codebehind:
myLinqDS =
(LinqDataSource)Session[System.Web.HttpUtility.UrlDecode(Request.QueryString[guid])];
aDataList.DataSource = myLinqDS;
aDataList.DataBind();