read data from dataset

S

suzy

i'm sure there must be a simple answer to this but i can't find it on the
net.

i have a dataset which contains data about 2 tables (tableA and tableB).

i would like to loop through records of tableA (in the dataset) and read the
values.

i have done the following, but i know its wrong because it doesn't work...
what am i doing wrong?

thanks!


for (long i = 0; i <= oDataSetMessages.Tables["tableA"].Rows.Count; i++)
{
long lngPrimaryKeyId =
Convert.ToInt64(oDataSetMessages.Tables["tableA"].Columns["PrimaryKeyId"]);
Response.Write (lngPrimaryKeyId);
}
 
V

Val Mazur

Hi,

I see only one problem. You should loop up to Count-1, since indexes start
from 0
 
A

A Jardine

May also be able to do it this way, without new row...

nYearValue = (System.Int16) myTable.Rows[nLoop]["nYear"];

-----Original Message-----
Suzy,

Not sure if this is what you are looking for - and I'm
relatively new to ADO.NET myself - but this is a section
of code that I have written to loop through records in a
DataSet and read a value. Note I have created a new row to
hold the record - I would think that you could create 2
for 2 tables.

Hope this is of help.

Alex


try
{
OleDbDataAdapter myAdptr = new OleDbDataAdapter("SELECT
* FROM tblTeams ORDER BY nYear,
nNum","provider=Microsoft.Jet.OLEDB.4.0; Data
Source=Aussie.mdb;");

// Dataset & fill adapter
DataSet mySet = new DataSet();
myAdptr.Fill(mySet,"tblTeams");

// DataTable
DataTable myTable = mySet.Tables["tblTeams"];

if(myTable.Rows.Count>0)
{
....

System.Int16 nYearValue=0;
DataRow loopRow;

for(int nLoop=0;nLoop<myTable.Rows.Count;nLoop++)
{
loopRow=myTable.Rows[nLoop];
nYearValue=(System.Int16) loopRow["nYear"];

....

}// endfor
}//endif

}// end try

catch(OleDbException ex){
MessageBox.Show(ex.Message,"Alert!");
}
catch(Exception ex2){
MessageBox.Show(ex2.Message,"Alert!");
}// endcatch




-----Original Message-----
i'm sure there must be a simple answer to this but i can't find it on the
net.

i have a dataset which contains data about 2 tables (tableA and tableB).

i would like to loop through records of tableA (in the dataset) and read the
values.

i have done the following, but i know its wrong because it doesn't work...
what am i doing wrong?

thanks!


for (long i = 0; i <= oDataSetMessages.Tables ["tableA"].Rows.Count; i++)
{
long lngPrimaryKeyId =
Convert.ToInt64(oDataSetMessages.Tables["tableA"].Columns ["PrimaryKeyId"]);
Response.Write (lngPrimaryKeyId);
}



.
.
 
R

Rajesh Tiwari

use this function to get all the data in ur data table

public void PrintTable(DataTable myTable)
{
foreach(DataRow myRow in myTable.Rows)
{
foreach(DataColumn myColumn in myTable.Columns){
Response.Write(myRow[myColumn]);
}
}
}

Rajesh
 
R

Rajesh Tiwari

here is the corrected version of ur code

for (int i = 0; i < dt.Tables["tableA"].Rows.Count; i++)
{
long lngPrimaryKeyId
=Int64.Parse(dt.Tables["tableA"].Rows["PrimaryKeyId"].ToString());
Response.Write(lngPrimaryKeyId);
}

try to see the problems in ur code by comparing it with urs
Rajesh
 
S

suzy

great, thanks for that rajesh.

one more thing though...

what do i do if i have 2 tables in my data set but i want to do a join
between the 2 (ie: if i have a table of orders and a table of customers, how
can i display the orders with their corresponding customers?
 
S

suzy

yes i thought so, i was thinking about it overnight and came to the same
conclusion!

actually i do have another question :) .......

suppose i have an application that lists orders/customers, etc. i would
like this to be a web application. i was going to create a customers and
orders class which would retrieve the data from the database and then return
a dataset back to the aspx page.

until now everything sounds fine, but if i wanted to display a list of
orders, and which customers they belong to, would the best way to do this be
to access the dataset values from the aspx page?

or is it neater to create an orders collection class , or something similar?

the reason i ask is because it doesn't seem very object orientated accessing
the dataset from the aspx page, but at the same time it seems a waste of
resources to populate a collection class on every hit of the aspx page.

thanks!


Rajesh said:
the easiest way to do this is to construct a query using join on these
tables so that this query returns the desired result set.and then use this
resultset to populate a dataset.

in case u need more help ,feel free to ask again

suzy said:
great, thanks for that rajesh.

one more thing though...

what do i do if i have 2 tables in my data set but i want to do a join
between the 2 (ie: if i have a table of orders and a table of customers, how
can i display the orders with their corresponding customers?


Rajesh Tiwari said:
use this function to get all the data in ur data table

public void PrintTable(DataTable myTable)
{
foreach(DataRow myRow in myTable.Rows)
{
foreach(DataColumn myColumn in myTable.Columns){
Response.Write(myRow[myColumn]);
}
}
}

Rajesh
 
Joined
Dec 11, 2007
Messages
1
Reaction score
0
writing data to a dataset

i want to know how to write data to a dataset and whether its possible. Im new to C#.net and want to know a method to do this asap. Any help would be really appreciated.
 

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