472,139 Members | 1,636 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 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 6377
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Tee | last post: by
reply views Thread by Seiche V via DotNetMonster.com | last post: by
4 posts views Thread by Andre | last post: by
2 posts views Thread by MDB | last post: by
1 post views Thread by David | last post: by
reply views Thread by leo001 | last post: by

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.