Im trying to retrieve a datatable that I put into a session variable, however when i do it apears that there are no columns...
I have done this before in VB.NET, I'n now using C#, and it worked perfectly, and as far as i can see there is no change in the code other than the obvious syntax changes.
EDIT:
The dt (in part 2) variable has the right data when i look in the data visulization window, but i have noticed that the other properties associated with a datatable are abscent. When i hover over a normal looking datatable with my mouse it looks like the following: " dt ----- {System.Data.DataTable}" but in the class im working on it just looks like "dt ----- {}". Also, after I return the session variable into the dt I clone it (dtclone = dt.clone(); ) and the clone is empty in the data visulizer.... what on earth!
EDIT 2:
I have now also tried converting the first datatable to a dataset, putting this in the session variable, and recoverting it back to a datatable in the class.
Am starting to wonder if it is a probelm with:
dt.Load(sqlReader);
The data does appear after this step though in the dataset visualiser, but not after being cloned.
Code below.
All help very welcome!
Thanks in advance
Chris
1) SQL command in a webhandler, the results of which populate the datatable to be put into the session variable.
DataTable dt = new DataTable();
SqlDataReader sqlReader= default(SqlDataReader);
SqlDataAdapter sqlAdapter = new SqlDataAdapter();
sqlReader = storedProc.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(sqlReader);
System.Web.HttpContext.Current.Session["ResultsTable"] = dt;
2) Part of the code in a class which performs calculations on the table:
DataTable dt = (DataTable)HttpContext.Current.Session["ResultsTable"];
did you call DataTable dispose after setting Session Var?
Because if you do this, solution is like this:
System.Web.HttpContext.Current.Session["ResultsTable"] = dt.Copy();
Related
I want to sort a DataTable within a DataSet. I have the following code:
DataTable dt = ds.Tables[0];
dt.TableName = "NEWNAME";
dt.DefaultView.ApplyDefaultSort = false;
dt.DefaultView.Sort = "COL1 desc";
dt = dt.DefaultView.ToTable();
dt.AcceptChanges(); // <-- Break Point Here
ds.AcceptChanges();
As I step through the code beyond the break point in Visual Studio, checking on dt in VS visualizer shows the sorted table, but then checking on ds in VS visualiser does not show the table data in sorted order, even though the name change is reflected. I have tried multiple ways of sorting the datatable available in a google search but the outcome remains the same.
What am I doing wrong?
DefaultView.Sort doesn't sort anything. It is just a string that will be used when you require the construction of a new table like you do in the line
dt = dt.DefaultView.ToTable();
after this point the dt reference (correctly sorted with the info taken from DefaultView.Sort) is no more pointing to the ds.Tables[0]. It is an entirely new DataTable.
The other way in which the DefaultView.Sort applies is when you loop through the DefaultView like in
foreach(DataViewRow dvr in ds.Tables[0].DefaultView)
{
// Here you get a row from the table sorted according to the property.
DataRow row = dvr.Row;
.....
}
One DataSet usually has lots of DataTable, but I'm only to target a particular DataTable which I thought it's pretty normal but apparently failed to do it?
Below were the approaches I've tried:
//Could not find an implementation of the query pattern for source type.......
DataTable dt = from table in changesDataSet.Tables
where table.TableName = "ABC"
select table;
//Surprisingly there was no method "Where" in changesDataSet.Tables
DataTable dt = changesDataSet.Tables.Where(x=>x.TableName="ABC").First();
Below was the code that able to print each and every table. I know I can do it via a loop but please tell me loop isn't the only options
foreach(DataTable table in changesDataSet.Tables)
{
Console.WriteLine(table.TableName);
}
You can access the table using an indexer on the collection of tables (DataTableCollection):
DataTable dt = changesDataSet.Tables["ABC"];
In my c# code I am trying to create an excel report with my data. I was trying some way to give my data headers. So I called my stored procedure added my dataset tables and now I want to add one of those tables to be a row in my data table. It returns one row my dataset does. example of how I want it to look:
Title
93.76% 00:23:59 00:33:41 95.56% 00:57:40 94.66%
Seems simple enough that's why I choose datatable to do so with and not dataset because I was limited to the dataset tables formatting. How can I get my data table to display the the data and not the name? Code provided below. I am also up for any other suggestions on how to accomplish this. If you need any more info or code please feel free to ask.
var dt = new DataTable("IncomingProductReport");
dt.Columns.Add("A Line Down Time");
SqlCommand cmd = new SqlCommand("L_GetTimeTotals", conn);
cmd.Parameters.Add("#startTime", SqlDbType.DateTime, 30).Value = RadDateTimePicker2.SelectedDate;
cmd.Parameters.Add("#endTime", SqlDbType.DateTime, 30).Value = RadDateTimePicker3.SelectedDate;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
var ds = new DataSet();
ds.Tables.Add("IncomingProductTotals");
ds.Tables.Add("IncomingProductTotalsA");
ds.Tables.Add("IncomingProductTotalsB");
ds.Tables.Add("IncomingProductTotals1");
ds.Tables.Add("IncomingProductTotalsA1");
ds.Tables.Add("IncomingProductTotalsB1");
da.TableMappings.Add("Table", "IncomingProductTotals");
da.TableMappings.Add("Table1", "IncomingProductTotalsA");
da.TableMappings.Add("Table2", "IncomingProductTotalsB");
da.TableMappings.Add("Table3", "IncomingProductTotals1");
da.TableMappings.Add("Table4", "IncomingProductTotalsA1");
da.TableMappings.Add("Table5", "IncomingProductTotalsB1");
da.Fill(ds);
ds.Tables.Remove("IncomingProductTotalsA");
ds.Tables.Remove("IncomingProductTotalsB");
ds.Tables.Remove("IncomingProductTotalsA1");
ds.Tables.Remove("IncomingProductTotalsB1");
dt.Rows.Add("Line 1 Totals");
dt.Rows.Add(ds.Tables["IncomingProductTotals"]);
dt.Rows.Add("Line 2 Totals");
dt.Rows.Add(ds.Tables["IncomingProductTotals1"]);
ExcelHelper.ToExcel(dt, "DownTimeTotals.xls", Page.Response);
dt.Columns.Add("A Line Down Time");
You are adding a Column to the DataTable. You do not say what type so it will be the default type and that is string.
dt.Rows.Add(ds.Tables["IncomingProductTotals"]);
Then you try to put a complete DataTable into a cell of a Column that expects strings. To get a string from the given object the system will call the ToString method of the DataTable. And ToString is implemented as returning the Tablename of a DataTable.
You need to take the data in the DataTables to put into the dt DataTable not the DataTables itself. That makes no sense. Allowed types for a column if you really wanted to put a DataTable into a DataTable are only the once described here.
I am using following code to select top 1000 rows from Datatable dt_Customers and update it with this selection. Every thing is working fine.
DataTable dt = new DataTable();
dt = dt_Customers.Rows.Cast<System.Data.DataRow>().Take(1000).CopyToDataTable();
I am not sure this is the right way or not? Is there any other way to achieve this or I am going fine ?
Thanks.
You don't need to instantiate new DataTable and assign it to dt variable. Also you can use AsEnumerable() extension:
DataTable dt = dt_Customers.AsEnumerable().Take(1000).CopyToDataTable();
I can't figure out why I am getting a "DataTable already belongs to another DataSet" exception.
Here is my code:
DataSet AlertSet = new DataSet();
DataTable generalAlertData = new DataTable("GeneralAlertData");
generalAlertData = //function that returns datatable
//throws exception
AlertSet.Tables.Add(generalAlertData)
When is another dataset being created and when is the generalAlertData datatable being added to it?
I tried using a slightly different syntax to create and add the table and got the same error:
DataSet AlertSet = new DataSet();
DataTable generalAlertData = //function that returns datatable
//throws exception
AlertSet .Tables.Add(generalAlertData);
It depends on what your function that retrieve the datatable does.
If that function use a DataAdapter and fills a dataset then you have the DataSet property of the table automatically assigned and you need to remove it before assigning a different DataSet
You could try this to remove the original DataSet and use your own
DataTable generalAlertData = GetTable();
DataSet u = generalAlertData.DataSet;
u.Tables.Remove(generalAlertData.TableName);
AlertSet.Tables.Add(generalAlertData);
So, if your hypothetical GetTable works in this way
public DataTable GetTable()
{
...
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
then you have the DataSet assigned to the returned datatable.
Instead this code doesn't assign anything to the DataSet property
public DataTable GetTable()
{
...
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
The DataTable returned from the method call, was added to a DataSet. Make sure it's not added to a DataSet; just build a stand alone DataTable.
As a side-note, returning a DataTable from a method call that assigns to this one makes the line above it irrelevant (the new).
This is an old question now but maybe this answer will help others.
I too received this error. I looked through my source code, found literally no other datasets and was puzzled as to why this was happening.
The solution to my issue was in my data access library. It didn't have a "getDataTable" routine but I did have a getDataset routine. When I needed just a datatable I called getDataset and took the tables(0) datatable instance.
The end result of this is that I got back my datatable like I wanted, but the datatable was already part of the dataset returned from my data access call. Thus, the error.
I had to add a getDatatable routine into my data access library to eliminate this problem.
I had the same problem and I solved it using Remove. In my opinion, your code could be this:
DataSet AlertSet = new DataSet();
DataTable generalAlertData = new DataTable("GeneralAlertData");
AlertSet.Tables.Add(generalAlertData);
AlertSet.Tables.Remove(generalAlertData);
Please, let me know if this code works.
My working code was like this:
DataTable myTable = new DataTable();
private void Save()
{
DataSet myDataSet = new DataSet();
myDataSet.Tables.Add(myTable);
myDataSet.Tables.Remove(myTable);//This works
myDataSet.WriteXml("myTable.xml");
}
private void buttonSave_Click(object sender, EventArgs e)
{
Save();
}
Every time I clicked the button buttonSave, the message “DataTable already belongs to another DataSet" appeared. After writing the line code myDataSet.Tables.Remove(myTable);//This works the application started running without problems and now I can click the button more times, without losing the value of myTable and without the error message.
I hope this can help.