Arrays and dataset

  • Thread starter Thread starter Trond
  • Start date Start date
T

Trond

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
 
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,
 
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 ) said:
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 said:
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
 
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 said:
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:[email protected]...
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 said:
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
 
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 said:
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 said:
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:[email protected]...
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


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
 
Glad it helped.

--
Gerry O'Brien
Visual Basic.NET MVP


Trond said:
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 said:
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 said:
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 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


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
 
Back
Top