473,692 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6533
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******** *************@i 39g2000cwa.goog legroups.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(objec t sender, EventArgs e)
{
string dataBaseName;
string startDate;
string endDate;

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

dataBaseName = dataBaseName.Tr im();

// create & configure the connection to database
string strConnString =
ConfigurationMa nager.Connectio nStrings["" + dataBaseName +
""].ConnectionStri ng;
SqlConnection theConnection = new SqlConnection(s trConnString);

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

//create the column(s)
DataColumn trackID = clickTable.Colu mns.Add("track_ id",
typeof(string)) ;
DataColumn uniqueClicks =
clickTable.Colu mns.Add("unique _clicks", typeof(string)) ;
DataColumn totalClicks =
clickTable.Colu mns.Add("total_ clicks", typeof(string)) ;

//set the primary key
clickTable.Prim aryKey = 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.Se lectCommand = clickCommand;

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

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

//create the column(s)
DataColumn trackIDLead = leadsTable.Colu mns.Add("track_ id",
typeof(string)) ;
DataColumn leads = leadsTable.Colu mns.Add("leads" ,
typeof(string)) ;

//set column(s) properties

//set the primary key
leadsTable.Prim aryKey = 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.Se lectCommand = leadsCommand;

//fill the dataTable
leadsAdapter.Fi ll(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(datatab le 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.Datata bles[1].Rows[0][1].Tostring() returns column 1 of row 1
or
dsClicks.Datata bles["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.Datata bles["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.Comp ute( "COUNT(column_n ame_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******** *************@e 56g2000cwe.goog legroups.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(objec t sender, EventArgs e)
{
string dataBaseName;
string startDate;
string endDate;

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

dataBaseName = dataBaseName.Tr im();

// create & configure the connection to database
string strConnString =
ConfigurationMa nager.Connectio nStrings["" + dataBaseName +
""].ConnectionStri ng;
SqlConnection theConnection = new SqlConnection(s trConnString);

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

//create the column(s)
DataColumn trackID = clickTable.Colu mns.Add("track_ id",
typeof(string)) ;
DataColumn uniqueClicks =
clickTable.Colu mns.Add("unique _clicks", typeof(string)) ;
DataColumn totalClicks =
clickTable.Colu mns.Add("total_ clicks", typeof(string)) ;

//set the primary key
clickTable.Prim aryKey = 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.Se lectCommand = clickCommand;

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

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

//create the column(s)
DataColumn trackIDLead = leadsTable.Colu mns.Add("track_ id",
typeof(string)) ;
DataColumn leads = leadsTable.Colu mns.Add("leads" ,
typeof(string)) ;

//set column(s) properties

//set the primary key
leadsTable.Prim aryKey = 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.Se lectCommand = leadsCommand;

//fill the dataTable
leadsAdapter.Fi ll(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

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

Similar topics

3
3381
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); da.Fill(DataSet, "Path");
0
1760
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. more of the error i get: at System.Data.DataTable.RecordStateChanged(Int32 record1, DataViewRowState oldState1, DataViewRowState newState1, Int32 record2, DataViewRowState oldState2, DataViewRowState newState2)
4
3204
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 with a number. When I run the following code Imports System.Data Imports System.Data.OleDb Sub Session_Start......
2
5222
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 auto sort and page so I would like to keep using the datagrid. My problem is that when I call DataAdapter.Fill, it takes a very long time and is even causing the web page to time out. Does anyone know why the fill is taking so long (15-20 sec)...
0
2140
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 2003 Server. I have a cached DataSet that I fill using an OracleDataAdapter. Changes are made to that dataset in memory and as rows are changed, they are updated back to the database using OracleDataAdapter.Update(DataRow). Every few minutes, I...
5
5356
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 table whose primary key is a varchar data type.
0
4550
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 IBM.Data.DB2.iSeries; Version: v1.0.3705 (is there an newer one for OS/400 Version ist V5R3 ?) or is it a hardware faoult (memory of as400 or pc) ? br mike Es wurde versucht, im geschützten Speicher zu lesen oder zu schreiben. Dies ist häufig ein...
3
7398
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 grid just fine, but I can't get the automated column (column index 1) to be set to readonly. (Column 0 is actually an automated column of edit buttons) here's my code: DataSet ds = new DataSet(); DataTable dt = new DataTable();...
1
3001
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 would prefer to just populate a datatable. However, when I try to fill my DataTable, I get
0
8608
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9086
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8803
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8806
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6459
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5820
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.