HELP - Getting a Value Out of a DataTable

W

Wayne

I have code to populate 1 data table in a dataset. The data table looks something like:

ParameterName ReportName ParameterValue
Param1 Report1 1
Param2 Report1 2
Param3 Report1 3
ReportStartDate Report1 05/10/2004 00:00
ReportEndDate Report1 05/10/2004 23:59

After querying the DB and populating the dataset/data table, I now need to get the "ParameterValue" for the "ReportStartDate" and "ReportEndDate" rows to save to local variables, something like:

value in local variable rptSDate = 05/10/2004 00:00
value in local variable rprEDate = 05/10/2004 23:59

But, I'm not sure how to navigate through this data table to find the "ReportStartDate" and "ReportEndDate" rows and then get their associated "Parameter" values. Help? Any suggestions?

Thanks.
 
W

William Ryan eMVP

You have a few ways:

the first painful way is
foreach(DataRow dro in myTable.Rows){
if ((DateTime)dro[4] = '05/10/2004 23:59' ){//DoSomething;}
}

However, you can use the .Select Method, or use a DataView and use the
Rowfilter or any of a few other methods including .Find
http://www.knowdotnet.com/articles/adopartiii.html
or this http://www.knowdotnet.com/articles/dataviewspart2.html

There are a few ways. Let me knwo if you have any troubles.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

I have code to populate 1 data table in a dataset. The data table looks
something like:

ParameterName ReportName ParameterValue
Param1 Report1 1
Param2 Report1 2
Param3 Report1 3
ReportStartDate Report1 05/10/2004 00:00
ReportEndDate Report1 05/10/2004 23:59

After querying the DB and populating the dataset/data table, I now need to
get the "ParameterValue" for the "ReportStartDate" and "ReportEndDate" rows
to save to local variables, something like:

value in local variable rptSDate = 05/10/2004 00:00
value in local variable rprEDate = 05/10/2004 23:59

But, I'm not sure how to navigate through this data table to find the
"ReportStartDate" and "ReportEndDate" rows and then get their associated
"Parameter" values. Help? Any suggestions?

Thanks.
 
W

Wayne

Bill,
Thanks for the reply. Maybe my brain's not working clearly right now or something, but I can't seem to get the .Select method to work. Here's the code I have so far:

string rptStartDate = "";
string rptEndDate = "";

System.Data.DataTable tblRptParams = dsReportParameters.Tables[0];

System.Data.DataRow[] startDateRows = tblRptParams.Select ("ReportName = '" + Session["selectedReport"].ToString() + "' and ParameterName = 'ReportStartDate'");
foreach (System.Data.DataRow rowRptStartDt in startDateRows)
{
rptStartDate = rowRptStartDt["ReportStartDate"].ToString();
}

System.Data.DataRow[] endDateRows = tblRptParams.Select ("ReportName = '" + Session["selectedReport"].ToString() + "' and ParameterName = 'ReportEndDate'");
foreach (System.Data.DataRow rowRptEndDt in endDateRows)
{
rptEndDate = rowRptEndDt["ReportEndDate"].ToString();
}

Where Session["selectedReport"].ToString() is an ASP.NET session variable holding the string Report1. The code above will throw an error saying that ReportStartDate in the line rptStartDate = rowRptStartDt["ReportStartDate"].ToString(); is not a valid column or something like that.

And, note the ParameterValue for the ReportStartDate and ReportEndDate will not always be the same value; it'll change very frequently.

Thanks!



William Ryan eMVP said:
You have a few ways:

the first painful way is
foreach(DataRow dro in myTable.Rows){
if ((DateTime)dro[4] = '05/10/2004 23:59' ){//DoSomething;}
}

However, you can use the .Select Method, or use a DataView and use the
Rowfilter or any of a few other methods including .Find
http://www.knowdotnet.com/articles/adopartiii.html
or this http://www.knowdotnet.com/articles/dataviewspart2.html

There are a few ways. Let me knwo if you have any troubles.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

I have code to populate 1 data table in a dataset. The data table looks
something like:

ParameterName ReportName ParameterValue
Param1 Report1 1
Param2 Report1 2
Param3 Report1 3
ReportStartDate Report1 05/10/2004 00:00
ReportEndDate Report1 05/10/2004 23:59

After querying the DB and populating the dataset/data table, I now need to
get the "ParameterValue" for the "ReportStartDate" and "ReportEndDate" rows
to save to local variables, something like:

value in local variable rptSDate = 05/10/2004 00:00
value in local variable rprEDate = 05/10/2004 23:59

But, I'm not sure how to navigate through this data table to find the
"ReportStartDate" and "ReportEndDate" rows and then get their associated
"Parameter" values. Help? Any suggestions?

Thanks.
 
A

Andy Gaskell

Change this line: rptStartDate = rowRptStartDt["ReportStartDate"].ToString();

to: rptStartDate = rowRptStartDt["ParameterValue"].ToString();

same deal with the end date:

rptEndDate = rowRptEndDt["ParameterValue"].ToString();

Bill,
Thanks for the reply. Maybe my brain's not working clearly right now or something, but I can't seem to get the .Select method to work. Here's the code I have so far:

string rptStartDate = "";
string rptEndDate = "";

System.Data.DataTable tblRptParams = dsReportParameters.Tables[0];

System.Data.DataRow[] startDateRows = tblRptParams.Select ("ReportName = '" + Session["selectedReport"].ToString() + "' and ParameterName = 'ReportStartDate'");
foreach (System.Data.DataRow rowRptStartDt in startDateRows)
{
rptStartDate = rowRptStartDt["ReportStartDate"].ToString();
}

System.Data.DataRow[] endDateRows = tblRptParams.Select ("ReportName = '" + Session["selectedReport"].ToString() + "' and ParameterName = 'ReportEndDate'");
foreach (System.Data.DataRow rowRptEndDt in endDateRows)
{
rptEndDate = rowRptEndDt["ReportEndDate"].ToString();
}

Where Session["selectedReport"].ToString() is an ASP.NET session variable holding the string Report1. The code above will throw an error saying that ReportStartDate in the line rptStartDate = rowRptStartDt["ReportStartDate"].ToString(); is not a valid column or something like that.

And, note the ParameterValue for the ReportStartDate and ReportEndDate will not always be the same value; it'll change very frequently.

Thanks!



William Ryan eMVP said:
You have a few ways:

the first painful way is
foreach(DataRow dro in myTable.Rows){
if ((DateTime)dro[4] = '05/10/2004 23:59' ){//DoSomething;}
}

However, you can use the .Select Method, or use a DataView and use the
Rowfilter or any of a few other methods including .Find
http://www.knowdotnet.com/articles/adopartiii.html
or this http://www.knowdotnet.com/articles/dataviewspart2.html

There are a few ways. Let me knwo if you have any troubles.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

I have code to populate 1 data table in a dataset. The data table looks
something like:

ParameterName ReportName ParameterValue
Param1 Report1 1
Param2 Report1 2
Param3 Report1 3
ReportStartDate Report1 05/10/2004 00:00
ReportEndDate Report1 05/10/2004 23:59

After querying the DB and populating the dataset/data table, I now need to
get the "ParameterValue" for the "ReportStartDate" and "ReportEndDate" rows
to save to local variables, something like:

value in local variable rptSDate = 05/10/2004 00:00
value in local variable rprEDate = 05/10/2004 23:59

But, I'm not sure how to navigate through this data table to find the
"ReportStartDate" and "ReportEndDate" rows and then get their associated
"Parameter" values. Help? Any suggestions?

Thanks.
 
W

Wayne

Thanks Andy! It works!

"Andy Gaskell" <pubb AT hotmail DOT com> wrote in message Change this line: rptStartDate = rowRptStartDt["ReportStartDate"].ToString();

to: rptStartDate = rowRptStartDt["ParameterValue"].ToString();

same deal with the end date:

rptEndDate = rowRptEndDt["ParameterValue"].ToString();

Bill,
Thanks for the reply. Maybe my brain's not working clearly right now or something, but I can't seem to get the .Select method to work. Here's the code I have so far:

string rptStartDate = "";
string rptEndDate = "";

System.Data.DataTable tblRptParams = dsReportParameters.Tables[0];

System.Data.DataRow[] startDateRows = tblRptParams.Select ("ReportName = '" + Session["selectedReport"].ToString() + "' and ParameterName = 'ReportStartDate'");
foreach (System.Data.DataRow rowRptStartDt in startDateRows)
{
rptStartDate = rowRptStartDt["ReportStartDate"].ToString();
}

System.Data.DataRow[] endDateRows = tblRptParams.Select ("ReportName = '" + Session["selectedReport"].ToString() + "' and ParameterName = 'ReportEndDate'");
foreach (System.Data.DataRow rowRptEndDt in endDateRows)
{
rptEndDate = rowRptEndDt["ReportEndDate"].ToString();
}

Where Session["selectedReport"].ToString() is an ASP.NET session variable holding the string Report1. The code above will throw an error saying that ReportStartDate in the line rptStartDate = rowRptStartDt["ReportStartDate"].ToString(); is not a valid column or something like that.

And, note the ParameterValue for the ReportStartDate and ReportEndDate will not always be the same value; it'll change very frequently.

Thanks!



William Ryan eMVP said:
You have a few ways:

the first painful way is
foreach(DataRow dro in myTable.Rows){
if ((DateTime)dro[4] = '05/10/2004 23:59' ){//DoSomething;}
}

However, you can use the .Select Method, or use a DataView and use the
Rowfilter or any of a few other methods including .Find
http://www.knowdotnet.com/articles/adopartiii.html
or this http://www.knowdotnet.com/articles/dataviewspart2.html

There are a few ways. Let me knwo if you have any troubles.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

I have code to populate 1 data table in a dataset. The data table looks
something like:

ParameterName ReportName ParameterValue
Param1 Report1 1
Param2 Report1 2
Param3 Report1 3
ReportStartDate Report1 05/10/2004 00:00
ReportEndDate Report1 05/10/2004 23:59

After querying the DB and populating the dataset/data table, I now need to
get the "ParameterValue" for the "ReportStartDate" and "ReportEndDate" rows
to save to local variables, something like:

value in local variable rptSDate = 05/10/2004 00:00
value in local variable rprEDate = 05/10/2004 23:59

But, I'm not sure how to navigate through this data table to find the
"ReportStartDate" and "ReportEndDate" rows and then get their associated
"Parameter" values. Help? Any suggestions?

Thanks.
 
W

Wayne

Since my .Select method should only return one row object, do I really need to do the foreach iteration? That is, could I replace:

foreach (System.Data.DataRow rowRptStartDt in startDateRows)
{
rptStartDate = rowRptStartDt["ParameterValue"].ToString();
}

with simply:

rptStartDate = rowRptStartDt["ParameterValue"].ToString();

or some other line of code that removes the foreach but still gets me the value?

Thanks.


"Andy Gaskell" <pubb AT hotmail DOT com> wrote in message Change this line: rptStartDate = rowRptStartDt["ReportStartDate"].ToString();

to: rptStartDate = rowRptStartDt["ParameterValue"].ToString();

same deal with the end date:

rptEndDate = rowRptEndDt["ParameterValue"].ToString();

Bill,
Thanks for the reply. Maybe my brain's not working clearly right now or something, but I can't seem to get the .Select method to work. Here's the code I have so far:

string rptStartDate = "";
string rptEndDate = "";

System.Data.DataTable tblRptParams = dsReportParameters.Tables[0];

System.Data.DataRow[] startDateRows = tblRptParams.Select ("ReportName = '" + Session["selectedReport"].ToString() + "' and ParameterName = 'ReportStartDate'");
foreach (System.Data.DataRow rowRptStartDt in startDateRows)
{
rptStartDate = rowRptStartDt["ReportStartDate"].ToString();
}

System.Data.DataRow[] endDateRows = tblRptParams.Select ("ReportName = '" + Session["selectedReport"].ToString() + "' and ParameterName = 'ReportEndDate'");
foreach (System.Data.DataRow rowRptEndDt in endDateRows)
{
rptEndDate = rowRptEndDt["ReportEndDate"].ToString();
}

Where Session["selectedReport"].ToString() is an ASP.NET session variable holding the string Report1. The code above will throw an error saying that ReportStartDate in the line rptStartDate = rowRptStartDt["ReportStartDate"].ToString(); is not a valid column or something like that.

And, note the ParameterValue for the ReportStartDate and ReportEndDate will not always be the same value; it'll change very frequently.

Thanks!



William Ryan eMVP said:
You have a few ways:

the first painful way is
foreach(DataRow dro in myTable.Rows){
if ((DateTime)dro[4] = '05/10/2004 23:59' ){//DoSomething;}
}

However, you can use the .Select Method, or use a DataView and use the
Rowfilter or any of a few other methods including .Find
http://www.knowdotnet.com/articles/adopartiii.html
or this http://www.knowdotnet.com/articles/dataviewspart2.html

There are a few ways. Let me knwo if you have any troubles.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

I have code to populate 1 data table in a dataset. The data table looks
something like:

ParameterName ReportName ParameterValue
Param1 Report1 1
Param2 Report1 2
Param3 Report1 3
ReportStartDate Report1 05/10/2004 00:00
ReportEndDate Report1 05/10/2004 23:59

After querying the DB and populating the dataset/data table, I now need to
get the "ParameterValue" for the "ReportStartDate" and "ReportEndDate" rows
to save to local variables, something like:

value in local variable rptSDate = 05/10/2004 00:00
value in local variable rprEDate = 05/10/2004 23:59

But, I'm not sure how to navigate through this data table to find the
"ReportStartDate" and "ReportEndDate" rows and then get their associated
"Parameter" values. Help? Any suggestions?

Thanks.
 
A

Andy Gaskell

It'd look something like

DataRow rowRptStartDt = startDateRows[0];
rptStartDate = rowRptStartDt["ParameterValue"].ToString();

and here's what it would look like using one line:

rptStartDate = startDateRows[0]["ParameterValue"].ToString();
Since my .Select method should only return one row object, do I really need to do the foreach iteration? That is, could I replace:

foreach (System.Data.DataRow rowRptStartDt in startDateRows)
{
rptStartDate = rowRptStartDt["ParameterValue"].ToString();
}

with simply:

rptStartDate = rowRptStartDt["ParameterValue"].ToString();

or some other line of code that removes the foreach but still gets me the value?

Thanks.


"Andy Gaskell" <pubb AT hotmail DOT com> wrote in message Change this line: rptStartDate = rowRptStartDt["ReportStartDate"].ToString();

to: rptStartDate = rowRptStartDt["ParameterValue"].ToString();

same deal with the end date:

rptEndDate = rowRptEndDt["ParameterValue"].ToString();

Bill,
Thanks for the reply. Maybe my brain's not working clearly right now or something, but I can't seem to get the .Select method to work. Here's the code I have so far:

string rptStartDate = "";
string rptEndDate = "";

System.Data.DataTable tblRptParams = dsReportParameters.Tables[0];

System.Data.DataRow[] startDateRows = tblRptParams.Select ("ReportName = '" + Session["selectedReport"].ToString() + "' and ParameterName = 'ReportStartDate'");
foreach (System.Data.DataRow rowRptStartDt in startDateRows)
{
rptStartDate = rowRptStartDt["ReportStartDate"].ToString();
}

System.Data.DataRow[] endDateRows = tblRptParams.Select ("ReportName = '" + Session["selectedReport"].ToString() + "' and ParameterName = 'ReportEndDate'");
foreach (System.Data.DataRow rowRptEndDt in endDateRows)
{
rptEndDate = rowRptEndDt["ReportEndDate"].ToString();
}

Where Session["selectedReport"].ToString() is an ASP.NET session variable holding the string Report1. The code above will throw an error saying that ReportStartDate in the line rptStartDate = rowRptStartDt["ReportStartDate"].ToString(); is not a valid column or something like that.

And, note the ParameterValue for the ReportStartDate and ReportEndDate will not always be the same value; it'll change very frequently.

Thanks!



William Ryan eMVP said:
You have a few ways:

the first painful way is
foreach(DataRow dro in myTable.Rows){
if ((DateTime)dro[4] = '05/10/2004 23:59' ){//DoSomething;}
}

However, you can use the .Select Method, or use a DataView and use the
Rowfilter or any of a few other methods including .Find
http://www.knowdotnet.com/articles/adopartiii.html
or this http://www.knowdotnet.com/articles/dataviewspart2.html

There are a few ways. Let me knwo if you have any troubles.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

I have code to populate 1 data table in a dataset. The data table looks
something like:

ParameterName ReportName ParameterValue
Param1 Report1 1
Param2 Report1 2
Param3 Report1 3
ReportStartDate Report1 05/10/2004 00:00
ReportEndDate Report1 05/10/2004 23:59

After querying the DB and populating the dataset/data table, I now need to
get the "ParameterValue" for the "ReportStartDate" and "ReportEndDate" rows
to save to local variables, something like:

value in local variable rptSDate = 05/10/2004 00:00
value in local variable rprEDate = 05/10/2004 23:59

But, I'm not sure how to navigate through this data table to find the
"ReportStartDate" and "ReportEndDate" rows and then get their associated
"Parameter" values. Help? Any suggestions?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top