I want to fetch data from separate tables in sqlserver and display them together in GridView, Is that possible? if so then please suggest.
Thank you.
Yes, you can do that. Sample SQL is :
SELECT * FROM firstTable UNION ALL SELECT * FROM secondTable
Then you can get the results to datatable and bind that datatable to gridview.
Yes, that is possible, if both the returned result schemas are the same.
You would have to either join them at SQL level (with UNION) or at code level (with Enumerable.Union()).
Bottom line, you can only assign a single datasource to a control.
I can think in two ways of do it, the first is that the SQL query join the data in the desired way and return it merged.
The other is making a query that returns a DataSet with the 2 tables and merge in c# creating a third datatable via code with the columns we want and populating later from the obtained data.
I will use the first option if is posible..
Related
I writing a C# program to output t-SQL records into separate tabs in an excel spreadsheet, split by the person the records belong to.
I have seen that I can have many data tables in a single data set, and turn each into a separate tab (how to store multiple DataTables into single DataSet in c#?), so now I need to populate my data tables.
I do not have a fixed list of people, it will vary each time the program is run, and a person could have any number of records assigned to them.
Is there a way of doing this using SQL / C# using something like order or group by; or do I have to get my results, pick up the list of people, then loop each SQL query for that specific person and feed that into a new data table?
Thought I'd ask if anyone knew a short way before I did it the long way, because this can't be an uncommon thing to do; so I suspect there must be a simpler way.
Normally you get one DataTable per SELECT statement.
However, you could just select everything and then use LINQ to group the data and fill your DataTables. See if this is any help.
It depends on the table structure, as well as for the source as for the destination.
If you have multiple source tables you can append them together with the UNION statement. Which gives the distinct value of all tables. You can use UNION ALL to keep duplicate values.
SELECT customer_key, customer_name, customer_address
FROM table_1
WHERE customer_key = #Customer_key
UNION (or UNION ALL)
SELECT customer_key, customer_name, customer_address
FROM table_2
WHERE Customer_key = #Customer_key
UNION etc..
I have three DataTables in C# called MIT, Oracle and Difference. Could you please let me know how to add these three tables to a gridview as seperate columns? I know how to bind a DataTable to a gridview as given below. But i need the same to be done for several DataTables.Please help
dataGridView1.DataSource = MIT;
Here's an image of the GridView.
I think that you should combine your tables into a DataSet and then use a common column (I presume there is one!) to create DataRelations between the tables. This will allow you to bind your DataGridView to the result of a query on the DataSet.
For more info: see here.
Even if you add these three different tables in the GridView (adding columns of each DataTable). How would you populate the data in each row as all three might be different. So there must be some way to first join these tables into one based on any relationship then you can associate/bind the resultant DataTable to GridView:
EnumerableRowCollection<DataRow> result =
from t1 in dt1.AsEnumerable()
join t1 in dt2.AsEnumerable()
on t1.Field<int>("KeyId") equals
t2.Field<int>("KeyId")
select new
{
KeyId = t1.Field<int>("KeyId"),
Column1 = t1.Field<string>("Column1"),
Column2 = t2.Field<string>("Column2"),
.
.
};
dataGridView1.DataSource = result.AsDataView();
You should merge your multiple datatables into one datatable and then give it as datasource to datagridview.below link will help for merging datatables to one.
http://www.aspnettutorials.com/tutorials/database/bind-mult-table-gridview-asp4.aspx
OR
you should SELECT your data into a single table using the appropriate joins. DataGridViews display their data in a single-table format - there is no "multiple table" concept in a DataGridView.
DataSet.Merge() can help you to merge DataRows from separate objects into one.
This is what I am trying to achieve :
I have a table in a database in which there is a column named "item_code".
I need to query this table, returning ONE row at a time. Now, I need to use the value of the column "item_code" for the one row which has been returned to query ANOTHER table where I'll use it to fetch a bunch of row(s). How do I do this?
I tried using a datareader object in a while loop, fetch one row at a time and then query the other table inside this loop to fetch the rows required but I couldn't figure out how to put this data in a gridview (use datatable? if yes, how?) in such a way that the previous rows in the gridview don't get erased after each iteration of the while loop.
The only way I know for putting data into a gridview is by using .Fill() but obviously, Fill method wouldn't do in this case as it would wipe out the previous entries in the gridview.
Please help.
Your solution will work, but you are correct, Fill() will erase the contents of the table. Instead, use Merge()
var myMainTable = new DataTable();
foreach(var itemId in itemIds)
{
var currentTable = new DataTable();
// submit new query
myAdapter.Fill(currentTable)
myMainTable.Merge(currentTable);
}
I need to query this table, returning ONE row at a time. Now, I need to use the value of the column "item_code" for the one row which has been returned to query ANOTHER table where I'll use it to fetch a bunch of row(s). How do I do this?
You could use a single SQL query that joins the two tables on "item_code" and retrieves the results from the second table.
You can make it in SQL level in one step, like this:
SELECT Table2.*
FROM Table2
INNER JOIN Table1 ON Table1.item_code = Table2.item_code
ORDER BY Table2.item_code
And of course if you need to make a smaller list, you can wirte the WHERE too.
i need create query dynamically in my .cs page. Actually what happens in my application is user can pass the column name and Table names from the page. by using those parameters(columns and tables) we need build Select query . we need retrieve those values from database and show in text format to user.there may multiple table and single table have multiple columns also.
please try yo help how to do this ny using asp.net3.5 and c3.net
So you need to have a dynamic query that populates its columns as per users requirement. It is not achievable but I have a nearby solution of it.
You can specify from which tables you want data and then using system functions create dynamic query of yours and get it done.
If nothing else, just create the query string dynamically and then pass it to the ADO.NET routines.
string qry = String.Format("SELECT {0} FROM Table", colName);
Well, i am tring to do something nice (nice for me, simple for you guys), i was told i can do it, but i have no idea where to start.
I have two DDL's in a page, i need on page_load to popolate both, each one gets data from deferent table with no relaition between them (suppliers/catagories). i know how to do it with two DB connections, that is easy, but i was told that i can do it with one connection.
I was not told if it is only the connection that is united or also that SP deal with both tables in one SP (doesn't seem logical to me that i can do it with only one SP...but what do i know..lol)
thanks,
Erez
You can run both the queries in the SP:
your_sp
select * from table1;
select * from table2;
Then on C# side, you open the data reader and you can use the reader.NextResult() method to move to the next result in the result set.
while (reader.Read())
{
// process results from first query
}
reader.NextResult();
while (reader.Read())
{
// process results from second query
}
I think you could separate your SQL statements by a semicolon.
e.g. SELECT myColumns FROM Suppliers; SELECT otherColumns FROM Categories
You could open the datareader in a regular way.
Once you are done reading all the data for 1st resultset, you could make a call to NextResult which will try to execute the next statement and will get you the reader for 2nd resultset.
Note: I have not done this. But this is what I can make out of the documentation.
Not exactly the way I'd do it (i'd use 2 object data sources); but if you really only want to use 1, do this:
Create one sql statement that contains 2 select statements; and load that into a C# DataSet.
Bind your first ddl to DataSet.Tables[0].
Bind your second ddl to DataSet.Tables[1].
There ya go. 1 connection.
EDIT: If you really want to use a DataReader...
You'd probably need 2 SELECT statements, with an additional field to distinguish which DDL you're inserting into. So; something like this:
SELECT 'Table1' AS TableName, Name, Value
FROM dbo.Table1
UNION
SELECT 'Table2' AS TableName, Name, Value
FROM dbo.Table2
and then in whatever method you're using to load items into your DDL's, check the table name to see which one to add it into