473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Fill a DataSet with DataTable questions?

Hello,

I am new to .net and am trying to build a report application that
queries 4 different tables based on a id, and I need to return them in
the same table for easy viewing.

Basically, I have one querie that grabs all of the id's I need for the
other 4 queries, but I am not sure how to get them into a DataTable or
DataSet, or if that is the best way to do this. Seperately the queries
all work with no problems.

The 4 reporting queries do a count on a field and return a result based
on other criteria, one of which is the id which is first gathered.

Right now I have it working where they can select the ID to view, and
then I pass it to the 4 queries which all return the results in their
own datagrid accordingly. My question is can this be done where I can
have one table which shows all the results based on the id.

Thanks in advance,

Kevyn

May 3 '06 #1
10 6479
Sure, however, you will need to run four separate queries because a query
returns one table irrespective of the complexity of the query. Since
datatables are addititve, you can fire four separate queries and add the
results to a dataset.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Professional VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------

<da******@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
Hello,

I am new to .net and am trying to build a report application that
queries 4 different tables based on a id, and I need to return them in
the same table for easy viewing.

Basically, I have one querie that grabs all of the id's I need for the
other 4 queries, but I am not sure how to get them into a DataTable or
DataSet, or if that is the best way to do this. Seperately the queries
all work with no problems.

The 4 reporting queries do a count on a field and return a result based
on other criteria, one of which is the id which is first gathered.

Right now I have it working where they can select the ID to view, and
then I pass it to the 4 queries which all return the results in their
own datagrid accordingly. My question is can this be done where I can
have one table which shows all the results based on the id.

Thanks in advance,

Kevyn

May 3 '06 #2
Thanks for the response.

Is there an example you can show me where you take the results of one
query, mainly the id, and then pass that as a variable to the other
queries and then put them all in a dataset?

There are about 40 or so id's which will need to pass and return the
results, so then I can put them in a dataset for displaying.

Thanks in advance!

Kevyn

May 4 '06 #3
Basically..Here is my code...can you assist with merging?

I am creating a dataset for each query, the only common field is
track_id, which I need to group the results by for each track_id.

protected void Page_Load(object sender, EventArgs e)
{
string dataBaseName;
string startDate;
string endDate;

dataBaseName = (string) Session["repositoryID"];
startDate = (string) Session["startDate"];
endDate = (string) Session["endDate"];

dataBaseName = dataBaseName.Trim();

// create & configure the connection to database
string strConnString =
ConfigurationManager.ConnectionStrings["" + dataBaseName +
""].ConnectionString;
SqlConnection theConnection = new SqlConnection(strConnString);

//create dataSet for Clicks
DataSet dsClicks = new DataSet();
DataTable clickTable = new DataTable("clicks");

//create the column(s)
DataColumn trackID = clickTable.Columns.Add("track_id",
typeof(string));
DataColumn uniqueClicks =
clickTable.Columns.Add("unique_clicks", typeof(string));
DataColumn totalClicks =
clickTable.Columns.Add("total_clicks", typeof(string));

//set the primary key
clickTable.PrimaryKey = new DataColumn[] { trackID };

//add the table
dsClicks.Tables.Add(clickTable);

//create the command and adapter
SqlDataAdapter clickAdapter = new SqlDataAdapter();
SqlCommand clickCommand = new SqlCommand(
" SELECT track_id, count(distinct ip) as unique_clicks,
count(ip) as total_clicks "
+ "FROM tracking (NOLOCK) "
+ "WHERE event_type = 'C' "
+ "AND record_created >= '" + startDate + "' "
+ "AND record_created <= '" + endDate + "' "
+ "GROUP BY track_id "
+ "ORDER BY track_id", theConnection);
clickAdapter.SelectCommand = clickCommand;

//fill the dataTable
clickAdapter.Fill(dsClicks, "unique_clicks");

//create dataSet for Leads
DataSet dsLeads = new DataSet();
DataTable leadsTable = new DataTable("leads");

//create the column(s)
DataColumn trackIDLead = leadsTable.Columns.Add("track_id",
typeof(string));
DataColumn leads = leadsTable.Columns.Add("leads",
typeof(string));

//set column(s) properties

//set the primary key
leadsTable.PrimaryKey = new DataColumn[] {trackIDLead};

//add the table
dsLeads.Tables.Add(leadsTable);

//create the command and adapter
SqlDataAdapter leadsAdapter = new SqlDataAdapter();
SqlCommand leadsCommand = new SqlCommand(
"SELECT track_id, count(distinct ip) as leads "
+ "FROM tracking (NOLOCK) "
+ "WHERE event_type = 'L' "
+ "AND record_created >= '" + startDate + "' "
+ "AND record_created <= '" + endDate + "' "
+ "GROUP BY track_id "
+ "ORDER BY track_id", theConnection);
leadsAdapter.SelectCommand = leadsCommand;

//fill the dataTable
leadsAdapter.Fill(dsLeads, "leads");
}

May 4 '06 #4
Can't you just put the queries together using UNION ALL?

da******@gmail.com wrote:
Hello,

I am new to .net and am trying to build a report application that
queries 4 different tables based on a id, and I need to return them in
the same table for easy viewing.

Basically, I have one querie that grabs all of the id's I need for the
other 4 queries, but I am not sure how to get them into a DataTable or
DataSet, or if that is the best way to do this. Seperately the queries
all work with no problems.

The 4 reporting queries do a count on a field and return a result based
on other criteria, one of which is the id which is first gathered.

Right now I have it working where they can select the ID to view, and
then I pass it to the 4 queries which all return the results in their
own datagrid accordingly. My question is can this be done where I can
have one table which shows all the results based on the id.

Thanks in advance,

Kevyn

May 4 '06 #5
Hmmm....I thought that when you did UNION's that all the fields had to
be the same?

May 5 '06 #6
Yes, they have to. Can't you add values so that the queries conform to
the same set of fields?

da******@gmail.com wrote:
Hmmm....I thought that when you did UNION's that all the fields had to
be the same?

May 5 '06 #7
Won't realy work in my case, as each of the queries represents a new
column, and union's only add rows it appears.

May 5 '06 #8
So in that case, the easier way to go is simply to keep your code as is.
Then remove this line:
//create dataSet for Leads
DataSet dsLeads = new DataSet(); and replace it with this line
//add the table
dsClicks.Tables.Add(leadsTable);

So now, you will have a single dataset, dsClicks that contains two
datatables - clicks, and leads. You can now efficiently loop thru the
dataset picking whatever datatable is required. Also, now that you just have
one dataset to manipulate, the code is less resource intensive.

foreach(datatable dt in dsClicks)
{
//this will allow you to iterate your tables
foreach(datarow dr in dt)
{
//this will allow you to loop thru the rows in each datatable
}
}

for direct access, simply use this:
dsClicks.Datatables[1].Rows[0][1].Tostring() returns column 1 of row 1
or
dsClicks.Datatables["clicks"].Rows[0][1].Tostring() returns column 1 of row
1

Now, the other path forward would be to not use the query to go grab
results - for instance a count of the rows - you can just simply use a
filter to count the rows or examine it like so:
dsClicks.Datatables["clicks"].Rows.Count. This saves you an expensive sql
query. Another thing you can do
is sum or total/apply formulas and filters to these tables instead of doing
it thru a query.
int size = clickTable.Compute( "COUNT(column_name_to_count)",
"" ).ToString() ;

There are a lot of functions, roughly equivalent to SQL functions, that the
dataset/datatable can use as well.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Professional VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------

<da******@gmail.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com... Basically..Here is my code...can you assist with merging?

I am creating a dataset for each query, the only common field is
track_id, which I need to group the results by for each track_id.

protected void Page_Load(object sender, EventArgs e)
{
string dataBaseName;
string startDate;
string endDate;

dataBaseName = (string) Session["repositoryID"];
startDate = (string) Session["startDate"];
endDate = (string) Session["endDate"];

dataBaseName = dataBaseName.Trim();

// create & configure the connection to database
string strConnString =
ConfigurationManager.ConnectionStrings["" + dataBaseName +
""].ConnectionString;
SqlConnection theConnection = new SqlConnection(strConnString);

//create dataSet for Clicks
DataSet dsClicks = new DataSet();
DataTable clickTable = new DataTable("clicks");

//create the column(s)
DataColumn trackID = clickTable.Columns.Add("track_id",
typeof(string));
DataColumn uniqueClicks =
clickTable.Columns.Add("unique_clicks", typeof(string));
DataColumn totalClicks =
clickTable.Columns.Add("total_clicks", typeof(string));

//set the primary key
clickTable.PrimaryKey = new DataColumn[] { trackID };

//add the table
dsClicks.Tables.Add(clickTable);

//create the command and adapter
SqlDataAdapter clickAdapter = new SqlDataAdapter();
SqlCommand clickCommand = new SqlCommand(
" SELECT track_id, count(distinct ip) as unique_clicks,
count(ip) as total_clicks "
+ "FROM tracking (NOLOCK) "
+ "WHERE event_type = 'C' "
+ "AND record_created >= '" + startDate + "' "
+ "AND record_created <= '" + endDate + "' "
+ "GROUP BY track_id "
+ "ORDER BY track_id", theConnection);
clickAdapter.SelectCommand = clickCommand;

//fill the dataTable
clickAdapter.Fill(dsClicks, "unique_clicks");

//create dataSet for Leads
DataSet dsLeads = new DataSet();
DataTable leadsTable = new DataTable("leads");

//create the column(s)
DataColumn trackIDLead = leadsTable.Columns.Add("track_id",
typeof(string));
DataColumn leads = leadsTable.Columns.Add("leads",
typeof(string));

//set column(s) properties

//set the primary key
leadsTable.PrimaryKey = new DataColumn[] {trackIDLead};

//add the table
dsLeads.Tables.Add(leadsTable);

//create the command and adapter
SqlDataAdapter leadsAdapter = new SqlDataAdapter();
SqlCommand leadsCommand = new SqlCommand(
"SELECT track_id, count(distinct ip) as leads "
+ "FROM tracking (NOLOCK) "
+ "WHERE event_type = 'L' "
+ "AND record_created >= '" + startDate + "' "
+ "AND record_created <= '" + endDate + "' "
+ "GROUP BY track_id "
+ "ORDER BY track_id", theConnection);
leadsAdapter.SelectCommand = leadsCommand;

//fill the dataTable
leadsAdapter.Fill(dsLeads, "leads");
}

May 5 '06 #9
Thank you for all your help in this. I was able to pull all of the
trackID's into its own dataSet, then iterate over those results and
perform the other queries I needed into a new dataTable and have the
results show the way I needed.

May 10 '06 #10
It sounds like you figured out your problem, but in case you run into
it again, I know of a tool that may be able to help. It's called the
DataSet Toolkit, and it helps to manage fills and updates of multiple
tables in DataSets. It takes care of a lot of the details of
organizing the operations so that you don't have to. Check it out and
let me know if it works for you.

http://www.hydrussoftware.com

John B.
http://johnsbraindump.blogspot.com

May 26 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tee | last post by:
Hi, I need some help about filling dataset, I am not sure how to describe it, so I give the example here. da = new OleDbDataAdapter("SELECT * FROM Path WHERE PathID=1", cnn); ...
0
by: Seiche V via DotNetMonster.com | last post by:
hy. i want to fill a data table from a dataset using SqlDataAdapter.fill(myTable), but i get the error: System.NullReferenceException: Object reference not set to an instance of an object. ...
4
by: Andre | last post by:
I am ruinning this in the global.asa VIA Visual Studio VB .NET (framework 1.1) I have a access database with a table called tblCounters with a feild called Counters with on row of data in it...
2
by: MDB | last post by:
Hello All, I have a data grid that I fill using a dataset. The results of the query has around 15 columns and 500 rows (and growing). The reason I am using the datagrid is so the end users can...
0
by: dalaeth | last post by:
I have searched Google high and low and haven't found anything that works. Here's my problem, hopefully someone will be able to help! I'm using 1.1 Framework, and ODP.NET 9.5.0.7 on a Windows...
5
by: Monty M. | last post by:
Hello; I was wondering if anyone can assist me with this problem. Here are the tools I am using: Language: C# Database: MS SQL Server 2000 Application: Visual Studio 2005 1. I have a...
0
by: mike1402 | last post by:
Hi ! I get the error below sometimes when retrieving a big amount of data using Datadapter.Fill(dataset,"table"). But when I send the command Fill again, there is no error. Is it a fault of...
3
by: ASPnewb1 | last post by:
I am currently filling a dataTable then adding this table to a dataset, setting the dataset to the Gridview's datasource. If I set the Gridview to generate columns automatically it will fill the...
1
by: David | last post by:
Hi, Using .NET 1.1, C# I am using a provider factory so that my app can use various database types. I am currently using the DataAdapter from the provider to fill a dataset, however, I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.