how can in load a DataTable to an existing table in my DataSet. something like this:
DataTable my_table = new DataTable("sale_info");
**add some row to my_table **
myDataSet.Tables.Add(my_table);
i have a table named sale_info in myDataset too and myDataSet.Tables.Add(my_table); is trying to add new one. actually i want update my table with new data from another table.
You can call myDataSet.Tables["sale_info"].Merge(my_table).
However, you should simply add the rows directly to the existing table rather than creating a new table.
Related
i am creating an Application in which i want to get only new data from database, suppose i have two records in database table and i select that data in my gridview, then next time when i load data from that table i don't want that previous 2 rows in my grid, i just want new rows if available in database table.
WHAT I HAVE DONE:
I have loaded my database table in gridview and counted rows of that grid. then store rows in int variable and next time when i load grid, i again count rows in grid and if there is new row in grid, i transfer these new rows in to new datatable and then assign them to the new grid view.
in this method i am confused because this works for if only 1 row is newly added since the data is previously loaded.
WHAT I WANT:
i want best method to load only the new record in my database so that the previous data will not displayed and processed again.
please help me !
this is the sample code i am using to load data in first dataGridView
using (var con = new SqlConnection(ConStr))
{
string query = "SELECT * FROM CHECKINOUT";
using (var cmd = new SqlCommand(query, con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
}
this is the screenshot of table from which i am selecting data.
http://imgur.com/a/eIaQ6
Add column InsertDate to your table. Maintain lastDataQueriedDate in your code..
Compare these two dates in your query.
I can't comment so I'll try posting my suggestions or maybe my answer here.
First of all, Try to fix your Select Query How can you select the newest data if you use Select *?
Second From the answer of Satish Better to add new column like DateInserted to have a identifier or clue for all data from your database.
And Lastly, If you can't modify your database design, then try creating a new table that can relate or link the data of your database and insert your identifier there, like the DateInserted.
P.S Try Satish answer. It is more easier.
I have a dataset that has a table called detail I want to sort. I know using a dataview I can sort the table with the following code..
Dim dvParsedDataset As New DataView(parsedDataset.Tables("Detail"))
dvParsedDataset.Sort = AuthorizatonConstants.Auth_ID
I want to not only sort the table but assign it back to the original table in this case parsedDataset.Tables("Detail") but when assigning the sorted view back I get a read only error
parsedDataset.Tables("Detail") = dvParsedDataset.ToTable 'READ ONLY ERROR
How do I sort the table and also override the original table with the sorted table?
You could remove the previous DataTable from the DataSet and the add the new table
DataTable detailCopy = dvParsedDataset.ToTable("Detail")
parsedDataset.Tables.Remove("Detail")
parsedDataset.Tables.Add(detailCopy)
I have a DataSet to which I need to add tables dynamically at runtime, but these tables need to reflect the existing database structures. Is there a way to pull the schema (not data) of a specific table into a DataSet at runtime.
So, for instance, I might have an Account table (along with hundreds of others) in a database. I need to create an Account table in the DataSet at runtime (based on various actions of the end users) but there are too many to manually code this for each table.
You didn't specify wich database engine you were working on, so the best generic method to do what you want to do would be to use IDataReader's GetSchemaTable method.
More info on how to do this here.
If you run a SQL query that returns a datatable, it will have the structure of the table. Thus,
select * from SourceTable where 1 = 2;
Which returns no rows, but all the columns. DataSet fill, etc. Done.
You can programatically modify and examine your data schema and data. You can add Columns programatically to the DataSet: http://msdn.microsoft.com/en-us/library/hfx3s9wd.aspx
DataTable workTable = new DataTable("Customers");
DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
workCol.AllowDBNull = false;
workCol.Unique = true;
workTable.Columns.Add("CustLName", typeof(String));
workTable.Columns.Add("CustFName", typeof(String));
workTable.Columns.Add("Purchases", typeof(Double));
You can create new tables also into the DataSet. You can examine and traverse the schema of what you have: look at the Tables property of DataSet, it contains your DataTables (DataTableCollection). The DataTables contain Columns property. Knowing that it's straight forward.
A stored procedure returns multiple tables, the result set is assigned to a DataSet. Can I access the tables in the DataSet with each table's name?
For eg.:-
DataSet ds = Select(despatch_Packing_ID);
The DataSet contains 4 tables.
I am imposed to access the tables as
DataTable dtSales = ds.Tables[0];
How can I access the DataTable as
DataTable dtSales = ds.Tables["Sales"]; // Sales is tables where from I get data
By default the table names generated by a DbDataAdapter will have the names "Table", "Table1", "Table2", ...
You can override this by specifying DataTableMappings.
For example:
DbDataAdapter adapter = ...
...
adapter.TableMappings.Add("Table", "Sales");
adapter.TableMappings.Add("Table1", "Customers");
...
adapter.Fill(myDataSet);
...
The problem is that a query in a stored procedure can span multiple tables and views or even simply return a single value.
How would the DataTable get its name then?
I suppose it doesn't work, because returned data may be produced from multiple joined tables. In such case there is no way to identify them by name. But I haven't checked myself, so it is just my guess.
I have one dataset in which there are
40 tables. Now I want to make a relation
between these tables and show
important data in grid. How do i do this?
If you're creating a typed dataset, it's easiest to create the relations in Visual Studio's dataset designer. Just right-click on the table in the designer, select Add->Relation, and specify the relation.
If you need to specify the relation in code, you can do it like this:
dataSet.Relations.Add(dataSet.Tables["Customers"].Columns["customerId"],
dataSet.Tables["Orders"].Columns["customerId"]);
Read all about it in MSDN here.
That is a large number of DataTables to have in a DataSet.
The first thing I would consider would be to reduce the number of DataTables (and eliminate the need for Relations) by populating the DataTables with queries that JOIN the database tables. For example, instead of having one DataTable for Product Category and another for Product Detail, it might be possible to combine the data for both database tables into one DataTable. Similarly, for Customer, Customer Address and Customer Phone, retrieve all of the data in one DataTable by using one query that does a JOIN on all three database tables.
Once you have minimized the number of DataTables in the DataSet, you can add Relations between DataTables if they have matching columns (even if the columns have different names). For example, there might be an Orders DataTable with a CustomerID column that matches the ID column in the Customers DataTable.
Here is the code to add a Relation to the DataSet for that situation. Assume that we have a DataSet dst containing two DataTables Customers and Orders.
DataColumn customerColumn, orderColumn;
customerColumn = dst.Tables["Customers"].Columns["ID"];
orderColumn = dst.Tables["Orders"].Columns["CustomerID"];
DataRelation dr = new DataRelation("CustomerOrders", customerColumn, orderColumn);
dst.Relations.Add(dr);
ds.Relations.Add("Products_Category",
ds.Tables("Categories").Columns("CategoryID"),
ds.Tables("Products").Columns("CategoryID"));
Did you try something like:
ds.Relations.Add("Products_Category",
ds.Tables("Categories").Columns("CategoryID"),
ds.Tables("Products").Columns("CategoryID"));
private void CreateRelation()
{
// Get the DataColumn objects from two DataTable objects
// in a DataSet. Code to get the DataSet not shown here.
DataColumn parentColumn =
DataSet1.Tables["Customers"].Columns["CustID"];
DataColumn childColumn =
DataSet1.Tables["Orders"].Columns["CustID"];
// Create DataRelation.
DataRelation relCustOrder;
relCustOrder = new DataRelation("CustomersOrders",
parentColumn, childColumn);
// Add the relation to the DataSet.
DataSet1.Relations.Add(relCustOrder);
}