I have error: DataTable already belongs to another DataSet. I use
HttpContext.Current.Session["myResults"] as DataSet
This error occures only when I try to load the same code into IE8 and Firefox at the same time.
My code:
public static class MyGlobals
{
public static DataTable dt = new DataTable();
public static int iPass = 0;
}
public class myResultsDataObject2
{
private DataSet _fResults;
public FlightResultsDataObject2()
{
if (MyGlobals.iPass == 1) //pass only on Button1_Click
{
this._fResults = HttpContext.Current.Session["myResults"] as DataSet;
if (this._fResults == null)
{
this._fResults = new DataSet();
this._fResults.Tables.Add(MyGlobals.dt);
this._fResults.DataSetName = "fResults";
GetData1();
HttpContext.Current.Session["FlightResults"] = this._fResults;
}
}
}
}
I don't want to change the way how assign a datasource to my asp:ListView. I don't want a line of code:
lvmyResults.DataSource=MyGlobals.dt;
This kind of databinding doesn't support built in sort, group, paging of asp:ListView
In my protected void btnUpdate_Click(object sender, EventArgs e) event I support trigger databinding:
myResultsDataObject2 cs = new myResultsDataObject2();
cs.Select("", "", "", null);
p.s. I just want to say again that before changing the line of code to:
this._fResults.Tables.Add(MyGlobals.dt.Clone());
everything was working fine, except that the code gived an error when using on 2 browsers at the same time. I'm sure that Clone() or Copy() methods are the way to fix things (many posts in the net are saying so), but my other code needs to much fixing which
I don't understand how to do it - or in other words i have lack of knowledge in ADO.NET databinding....
Digitborn.co...
Member
684 Points
450 Posts
Error: DataTable already belongs to another DataSet
Jan 30, 2011 10:14 AM|LINK
Hello,
I have error: DataTable already belongs to another DataSet. I use
HttpContext.Current.Session["myResults"] as DataSet
This error occures only when I try to load the same code into IE8 and Firefox at the same time.
My code:
public static class MyGlobals { public static DataTable dt = new DataTable(); public static int iPass = 0; } public class myResultsDataObject2 { private DataSet _fResults; public FlightResultsDataObject2() { if (MyGlobals.iPass == 1) //pass only on Button1_Click { this._fResults = HttpContext.Current.Session["myResults"] as DataSet; if (this._fResults == null) { this._fResults = new DataSet(); this._fResults.Tables.Add(MyGlobals.dt); this._fResults.DataSetName = "fResults"; GetData1(); HttpContext.Current.Session["FlightResults"] = this._fResults; } } } }What can I do?
Lateef045
Star
7813 Points
1541 Posts
Re: Error: DataTable already belongs to another DataSet
Jan 30, 2011 11:14 AM|LINK
Try
Digitborn.co...
Member
684 Points
450 Posts
Re: Error: DataTable already belongs to another DataSet
Jan 30, 2011 11:26 AM|LINK
If I put Clone(), i get an error:
A column named 'Price' already belongs to this DataTable.
In GetData1() i have:
public void GetData1() { MyGlobals.dt.Columns.Add("Price"); MyGlobals.dt.Columns.Add("Time"); MyGlobals.dt.Columns.Add("City"); etc... MyGlobals.dt.TableName = "fResults"; for (int x = 0; x < MyGlobals.Price1.Count; x++) { DataRow dr = MyGlobals.dt.NewRow(); dr[0] = MyGlobals.Price1[x]; dr[1] = MyGlobals.Time1[x]; dr[2] = MyGlobals.City1[x]; etc... MyGlobals.dt.Rows.Add(dr); } }Lateef045
Star
7813 Points
1541 Posts
Re: Error: DataTable already belongs to another DataSet
Jan 30, 2011 11:30 AM|LINK
This is the problem here.
before creating a column in the datatable, you should first check if it already there. If it not there, then you should add the column
if(MyGlobals.dt.Columns.Contains("Price")) { // Dont add the column } else { // Add the column MyGlobals.dt.Columns.Add("Price"); }Digitborn.co...
Member
684 Points
450 Posts
Re: Error: DataTable already belongs to another DataSet
Jan 30, 2011 11:59 AM|LINK
Now, I've got another error: DataTable must be set prior to using DataView
On codeline:
protected void btnUpdate_Click(object sender, EventArgs e) { MyGlobals.iPass = 1; myResultsDataObject2 cs = new myResultsDataObject2(); cs.Select("", "", "", null); lvmyResults.DataBind(); //asp:ListView }The error is on line lvmyResults.DataBind();
Lateef045
Star
7813 Points
1541 Posts
Re: Error: DataTable already belongs to another DataSet
Jan 30, 2011 12:22 PM|LINK
Where are you assigning the DataSource for your gridview.
First assign the DataSource for your gridview and then use DataBind()
eg.
lvmyResults.DataSource=MyGlobals.dt;
lvmyResults.DataBind();
Digitborn.co...
Member
684 Points
450 Posts
Re: Error: DataTable already belongs to another DataSet
Jan 30, 2011 01:10 PM|LINK
I assign datasource in my Default.aspx file:
<asp:ListView ID="lvmyResults" runat="server" DataSourceID="objmyResults"> <asp:ObjectDataSource ID="objmyResults" runat="server" SelectMethod="Select" TypeName="Travel_Views.myResultsDataObject2" OnSelecting="odsmyResults_Selecting"> <SelectParameters> <asp:Parameter Name="propertyName1" Type="String" /> <asp:Parameter Name="propertyName2" Type="String" /> <asp:Parameter Name="propertyValue" Type="String" /> <asp:Parameter Name="propertyValueArr" Type="Object" /> </SelectParameters> </asp:ObjectDataSource>I don't want to change the way how assign a datasource to my asp:ListView. I don't want a line of code:
lvmyResults.DataSource=MyGlobals.dt;
This kind of databinding doesn't support built in sort, group, paging of asp:ListView
In my protected void btnUpdate_Click(object sender, EventArgs e) event I support trigger databinding:
myResultsDataObject2 cs = new myResultsDataObject2();
cs.Select("", "", "", null);
p.s. I just want to say again that before changing the line of code to:
this._fResults.Tables.Add(MyGlobals.dt.Clone());
everything was working fine, except that the code gived an error when using on 2 browsers at the same time. I'm sure that Clone() or Copy() methods are the way to fix things (many posts in the net are saying so), but my other code needs to much fixing which I don't understand how to do it - or in other words i have lack of knowledge in ADO.NET databinding....
Digitborn.co...
Member
684 Points
450 Posts
Re: Error: DataTable already belongs to another DataSet
Jan 30, 2011 01:52 PM|LINK
If I only remove Clone() method in line:
this._fResults.Tables.Add(MyGlobals.dt.Clone());
everything works OK. I can click on btnUpdate , i can refresh page, and i get the results.
When Clone() is there I get this databinding problem.
Digitborn.co...
Member
684 Points
450 Posts
Re: Error: DataTable already belongs to another DataSet
Jan 30, 2011 03:25 PM|LINK
When I don't have Clone() method:
I put breakpoints and on line MyGlobals.dt.TableName = "fResults"; in:
public void GetData1()
{
if (!MyGlobals.dt.Columns.Contains("Price"))
{
MyGlobals.dt.Columns.Add("Price");
MyGlobals.dt.Columns.Add("Time");
MyGlobals.dt.Columns.Add("City");
MyGlobals.dt.TableName = "fResults";
}
}
I get my DataTabled filled with results and
this._fResults.Tables["fResults"] is not null
I also have this property in my class:
public DataTable myResultsTable
{
get { return this._fResults.Tables["fResults"]; }
}
When I have Clone() method on the same line :
this._fResults.Tables["fResults"] is null