469,283 Members | 2,263 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

Arrays and dataset

I have a webservice that is returning a dataset. In dataset there is a table
with 4 columns. (LDate, LTime, LDepth and ServiceNumber)
I then try to read only 2 of the columns into array (double[][]
valuesArray;) but i cant get it to work. It is LDepth and ServiceNumber.
Both columns has numbers with decimals. As a newbee i am struggling with
arrays :-)
Best regards
Trond
Nov 16 '05 #1
5 4485
Hi,

I don;t understand well your question, are you trying to create an array
with the components of two of the columns?
if so there is no way of doing it afaik, you would have to iterate in the
datarow collection.

now, regarding your target structure double[] [] maybe is not the best way
for store them. It seems more like a construction from C , what is what you
want?

Maybe the best for you is creating either a class or a struct with two
double components and then create an array with that, lie this:

struct MyStruct
{
double ldepth ;
double service;
MyStruct( double l, double s){ ldepth = l; service=s;}
}

MyStruct[] theArray;

//create it
theArray = new MyStruct[ theDataSet.Tables[0].Rows.Count ];
int i =0;
foreach( DataRow row in theDataSet.Tables[0].Rows )
{
theArray[ i++] = new MyStruct( Convert.ToDouble( row["XXX"]),
Convert.ToDouble( row["YYY"] );
}

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Trond" <tr***@montanis.com> wrote in message
news:Om**************@TK2MSFTNGP15.phx.gbl...
I have a webservice that is returning a dataset. In dataset there is a
table
with 4 columns. (LDate, LTime, LDepth and ServiceNumber)
I then try to read only 2 of the columns into array (double[][]
valuesArray;) but i cant get it to work. It is LDepth and ServiceNumber.
Both columns has numbers with decimals. As a newbee i am struggling with
arrays :-)
Best regards
Trond

Nov 16 '05 #2
Yes I realize that I was a lil in the clouds in my post :-)
What I want is to create an array with values from one column in the
dataset. And then one more array with data from column2.
I was first thinking maybe I should create an jagged array, but I realize
that I don't have to do that. SO then my question is how can I get lets say
the third column into an array?. It is LDepth and the value of each row into
array.
If in database I have 500 rows I then want an array with 500 indexes holding
the values in LDepth :-)

Best regards
Trond

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uH**************@TK2MSFTNGP10.phx.gbl...
Hi,

I don;t understand well your question, are you trying to create an array
with the components of two of the columns?
if so there is no way of doing it afaik, you would have to iterate in the
datarow collection.

now, regarding your target structure double[] [] maybe is not the best way for store them. It seems more like a construction from C , what is what you want?

Maybe the best for you is creating either a class or a struct with two
double components and then create an array with that, lie this:

struct MyStruct
{
double ldepth ;
double service;
MyStruct( double l, double s){ ldepth = l; service=s;}
}

MyStruct[] theArray;

//create it
theArray = new MyStruct[ theDataSet.Tables[0].Rows.Count ];
int i =0;
foreach( DataRow row in theDataSet.Tables[0].Rows )
{
theArray[ i++] = new MyStruct( Convert.ToDouble( row["XXX"]),
Convert.ToDouble( row["YYY"] );
}

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Trond" <tr***@montanis.com> wrote in message
news:Om**************@TK2MSFTNGP15.phx.gbl...
I have a webservice that is returning a dataset. In dataset there is a
table
with 4 columns. (LDate, LTime, LDepth and ServiceNumber)
I then try to read only 2 of the columns into array (double[][]
valuesArray;) but i cant get it to work. It is LDepth and ServiceNumber.
Both columns has numbers with decimals. As a newbee i am struggling with
arrays :-)
Best regards
Trond


Nov 16 '05 #3
Try this;

string strSQL;
int index = 0;

string connString = "Server=(local);Initial Catalog=pubs;user=sa;password=";

SqlConnection conn = new SqlConnection(connString);

try

{

conn.Open();

strSQL = "SELECT * from authors";

SqlCommand cmd = new SqlCommand(strSQL,conn);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

int rowCount = ds.Tables[0].Rows.Count;

string[] names = new string[rowCount];

foreach(DataRow dr in ds.Tables[0].Rows)

{

names[index] = dr[0].ToString();

index++;

}

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

If you wish to use a multi-dimensional array, you will need to change the
loop to reflect the second dimension. I did not do that here as you
mentioned wanting seperate arrays.

For each column you want to insert into the array, merely change the index
value in dr[0] to the correct column number, remembering that the columns
are zero based.

--
Gerry O'Brien
Visual Basic.NET MVP
"Trond" <tr***@montanis.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Yes I realize that I was a lil in the clouds in my post :-)
What I want is to create an array with values from one column in the
dataset. And then one more array with data from column2.
I was first thinking maybe I should create an jagged array, but I realize
that I don't have to do that. SO then my question is how can I get lets
say
the third column into an array?. It is LDepth and the value of each row
into
array.
If in database I have 500 rows I then want an array with 500 indexes
holding
the values in LDepth :-)

Best regards
Trond

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:uH**************@TK2MSFTNGP10.phx.gbl...
Hi,

I don;t understand well your question, are you trying to create an array
with the components of two of the columns?
if so there is no way of doing it afaik, you would have to iterate in
the
datarow collection.

now, regarding your target structure double[] [] maybe is not the best

way
for store them. It seems more like a construction from C , what is what

you
want?

Maybe the best for you is creating either a class or a struct with two
double components and then create an array with that, lie this:

struct MyStruct
{
double ldepth ;
double service;
MyStruct( double l, double s){ ldepth = l; service=s;}
}

MyStruct[] theArray;

//create it
theArray = new MyStruct[ theDataSet.Tables[0].Rows.Count ];
int i =0;
foreach( DataRow row in theDataSet.Tables[0].Rows )
{
theArray[ i++] = new MyStruct( Convert.ToDouble( row["XXX"]),
Convert.ToDouble( row["YYY"] );
}

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Trond" <tr***@montanis.com> wrote in message
news:Om**************@TK2MSFTNGP15.phx.gbl...
>I have a webservice that is returning a dataset. In dataset there is a
>table
> with 4 columns. (LDate, LTime, LDepth and ServiceNumber)
> I then try to read only 2 of the columns into array (double[][]
> valuesArray;) but i cant get it to work. It is LDepth and
> ServiceNumber.
> Both columns has numbers with decimals. As a newbee i am struggling
> with
> arrays :-)
> Best regards
> Trond
>
>



Nov 16 '05 #4
Thanks a lot. That was it. Really appreciate that you took time to add code
to your reply.

It solved my problem :-)

Best regards

Trond

"Gerry O'Brien" <gk******@hotmail.com> wrote in message
news:Oj**************@tk2msftngp13.phx.gbl...
Try this;

string strSQL;
int index = 0;

string connString = "Server=(local);Initial Catalog=pubs;user=sa;password=";
SqlConnection conn = new SqlConnection(connString);

try

{

conn.Open();

strSQL = "SELECT * from authors";

SqlCommand cmd = new SqlCommand(strSQL,conn);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

int rowCount = ds.Tables[0].Rows.Count;

string[] names = new string[rowCount];

foreach(DataRow dr in ds.Tables[0].Rows)

{

names[index] = dr[0].ToString();

index++;

}

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

If you wish to use a multi-dimensional array, you will need to change the
loop to reflect the second dimension. I did not do that here as you
mentioned wanting seperate arrays.

For each column you want to insert into the array, merely change the index
value in dr[0] to the correct column number, remembering that the columns
are zero based.

--
Gerry O'Brien
Visual Basic.NET MVP
"Trond" <tr***@montanis.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Yes I realize that I was a lil in the clouds in my post :-)
What I want is to create an array with values from one column in the
dataset. And then one more array with data from column2.
I was first thinking maybe I should create an jagged array, but I realize that I don't have to do that. SO then my question is how can I get lets
say
the third column into an array?. It is LDepth and the value of each row
into
array.
If in database I have 500 rows I then want an array with 500 indexes
holding
the values in LDepth :-)

Best regards
Trond

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:uH**************@TK2MSFTNGP10.phx.gbl...
Hi,

I don;t understand well your question, are you trying to create an array with the components of two of the columns?
if so there is no way of doing it afaik, you would have to iterate in
the
datarow collection.

now, regarding your target structure double[] [] maybe is not the best

way
for store them. It seems more like a construction from C , what is what

you
want?

Maybe the best for you is creating either a class or a struct with two
double components and then create an array with that, lie this:

struct MyStruct
{
double ldepth ;
double service;
MyStruct( double l, double s){ ldepth = l; service=s;}
}

MyStruct[] theArray;

//create it
theArray = new MyStruct[ theDataSet.Tables[0].Rows.Count ];
int i =0;
foreach( DataRow row in theDataSet.Tables[0].Rows )
{
theArray[ i++] = new MyStruct( Convert.ToDouble( row["XXX"]),
Convert.ToDouble( row["YYY"] );
}

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Trond" <tr***@montanis.com> wrote in message
news:Om**************@TK2MSFTNGP15.phx.gbl...
>I have a webservice that is returning a dataset. In dataset there is a
>table
> with 4 columns. (LDate, LTime, LDepth and ServiceNumber)
> I then try to read only 2 of the columns into array (double[][]
> valuesArray;) but i cant get it to work. It is LDepth and
> ServiceNumber.
> Both columns has numbers with decimals. As a newbee i am struggling
> with
> arrays :-)
> Best regards
> Trond
>
>



Nov 16 '05 #5
Glad it helped.

--
Gerry O'Brien
Visual Basic.NET MVP
"Trond" <tr***@montanis.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Thanks a lot. That was it. Really appreciate that you took time to add
code
to your reply.

It solved my problem :-)

Best regards

Trond

"Gerry O'Brien" <gk******@hotmail.com> wrote in message
news:Oj**************@tk2msftngp13.phx.gbl...
Try this;

string strSQL;
int index = 0;

string connString = "Server=(local);Initial

Catalog=pubs;user=sa;password=";

SqlConnection conn = new SqlConnection(connString);

try

{

conn.Open();

strSQL = "SELECT * from authors";

SqlCommand cmd = new SqlCommand(strSQL,conn);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

int rowCount = ds.Tables[0].Rows.Count;

string[] names = new string[rowCount];

foreach(DataRow dr in ds.Tables[0].Rows)

{

names[index] = dr[0].ToString();

index++;

}

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

If you wish to use a multi-dimensional array, you will need to change the
loop to reflect the second dimension. I did not do that here as you
mentioned wanting seperate arrays.

For each column you want to insert into the array, merely change the
index
value in dr[0] to the correct column number, remembering that the columns
are zero based.

--
Gerry O'Brien
Visual Basic.NET MVP
"Trond" <tr***@montanis.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> Yes I realize that I was a lil in the clouds in my post :-)
> What I want is to create an array with values from one column in the
> dataset. And then one more array with data from column2.
> I was first thinking maybe I should create an jagged array, but I realize > that I don't have to do that. SO then my question is how can I get lets
> say
> the third column into an array?. It is LDepth and the value of each row
> into
> array.
> If in database I have 500 rows I then want an array with 500 indexes
> holding
> the values in LDepth :-)
>
> Best regards
> Trond
>
> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
> wrote
> in message news:uH**************@TK2MSFTNGP10.phx.gbl...
>> Hi,
>>
>> I don;t understand well your question, are you trying to create an array >> with the components of two of the columns?
>> if so there is no way of doing it afaik, you would have to iterate in
>> the
>> datarow collection.
>>
>> now, regarding your target structure double[] [] maybe is not the
>> best
> way
>> for store them. It seems more like a construction from C , what is
>> what
> you
>> want?
>>
>> Maybe the best for you is creating either a class or a struct with two
>> double components and then create an array with that, lie this:
>>
>> struct MyStruct
>> {
>> double ldepth ;
>> double service;
>> MyStruct( double l, double s){ ldepth = l; service=s;}
>> }
>>
>> MyStruct[] theArray;
>>
>> //create it
>> theArray = new MyStruct[ theDataSet.Tables[0].Rows.Count ];
>> int i =0;
>> foreach( DataRow row in theDataSet.Tables[0].Rows )
>> {
>> theArray[ i++] = new MyStruct( Convert.ToDouble( row["XXX"]),
>> Convert.ToDouble( row["YYY"] );
>> }
>>
>> Cheers,
>>
>> --
>> Ignacio Machin,
>> ignacio.machin AT dot.state.fl.us
>> Florida Department Of Transportation
>>
>>
>> "Trond" <tr***@montanis.com> wrote in message
>> news:Om**************@TK2MSFTNGP15.phx.gbl...
>> >I have a webservice that is returning a dataset. In dataset there is
>> >a
>> >table
>> > with 4 columns. (LDate, LTime, LDepth and ServiceNumber)
>> > I then try to read only 2 of the columns into array (double[][]
>> > valuesArray;) but i cant get it to work. It is LDepth and
>> > ServiceNumber.
>> > Both columns has numbers with decimals. As a newbee i am struggling
>> > with
>> > arrays :-)
>> > Best regards
>> > Trond
>> >
>> >
>>
>>
>
>



Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Bhavna | last post: by
2 posts views Thread by Islam Elkhayat | last post: by
2 posts views Thread by David | last post: by
4 posts views Thread by simchajoy2000 | last post: by
4 posts views Thread by Justin Emlay | last post: by
9 posts views Thread by Anil Gupte | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.