By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

convert ArrayList object to double

P: n/a
In the following code, I want ArrayList object x to be of type double.

How can I do that?

--Thanks

SqlConnection objConnection = new
SqlConnection("server=(local)\\SQLEXPRESS; database=Northwind; integrated
security=true;");
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);

objConnection.Open();

ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();

SqlDataReader dr = objCommand.ExecuteReader();

while (dr.Read())
{
object[] values1 = new object[0];
dr.GetValues(values1);
strLabel.Add(values1);

object[] values2 = new object[1];
dr.GetValues(values2);
x.Add(values2);
}

dr.Close();
objConnection.Close();
Mar 21 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
You need CLR 2.0 generics for that...

List<double> x = new List<double>( );

Web learner wrote:
In the following code, I want ArrayList object x to be of type double.

How can I do that?

--Thanks

SqlConnection objConnection = new
SqlConnection("server=(local)\\SQLEXPRESS; database=Northwind; integrated
security=true;");
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);

objConnection.Open();

ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();

SqlDataReader dr = objCommand.ExecuteReader();

while (dr.Read())
{
object[] values1 = new object[0];
dr.GetValues(values1);
strLabel.Add(values1);

object[] values2 = new object[1];
dr.GetValues(values2);
x.Add(values2);
}

dr.Close();
objConnection.Close();


Mar 21 '06 #2

P: n/a
I changed the code as follows.

List<string> ProductName = new List<string>();
List<double> UnitsInStock = new List<double>();

SqlDataReader dr = objCommand.ExecuteReader();
while (dr.Read())
{
ProductName.Add(dr.GetValue(0));
UnitsInStock.Add(dr.GetValue(1));
}

Now I get the following errors.

The best overloaded method match for 'System.Collections.Generic.List<string>.Add(strin g)' has some invalid arguments

The best overloaded method match for 'System.Collections.Generic.List<double>.Add(doubl e)' has some invalid arguments

Any further idea, please??

--


<ag******@gmail.com> wrote in message news:11**********************@g10g2000cwb.googlegr oups.com...
You need CLR 2.0 generics for that...

List<double> x = new List<double>( );

Web learner wrote:
In the following code, I want ArrayList object x to be of type double.

How can I do that?

--Thanks

SqlConnection objConnection = new
SqlConnection("server=(local)\\SQLEXPRESS; database=Northwind; integrated
security=true;");
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);

objConnection.Open();

ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();

SqlDataReader dr = objCommand.ExecuteReader();

while (dr.Read())
{
object[] values1 = new object[0];
dr.GetValues(values1);
strLabel.Add(values1);

object[] values2 = new object[1];
dr.GetValues(values2);
x.Add(values2);
}

dr.Close();
objConnection.Close();

Mar 21 '06 #3

P: n/a
If you know for a fact that the first item in the data row is a string,
and the second is a double, and neither of them can ever be null, then
you can do this:

ProductName.Add((string)dr.GetValue(0));
UnitsInStock.Add((double)dr.GetValue(1));

Mar 21 '06 #4

P: n/a
On 20 Mar 2006 19:30:28 -0800, "Bruce Wood" <br*******@canada.com> wrote:
If you know for a fact that the first item in the data row is a string,
and the second is a double, and neither of them can ever be null, then
you can do this:

ProductName.Add((string)dr.GetValue(0));
UnitsInStock.Add((double)dr.GetValue(1));

or:
// no casting required
ProductName.Add(dr.GetString(0));
UnitsInStock.Add(dr.GetDouble(1));

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Mar 21 '06 #5

P: n/a
The Add() methods exepct a string and double, respectively. Try this:

ProductName.Add(dr.GetString(0));
UnitsInStock.Add(dr.GetDouble(1));

or this:

ProductName.Add((string)dr.GetValue(0));
UnitsInStock.Add((double)dr.GetValue(1));

-Carl

Mar 21 '06 #6

P: n/a
Will the resulting array be in following form?

double[] UnitsInStock = { 120, 104, 112, 111 };
string[] ProductName = { "Boysenberry", "Broed", "Momo", "Taifuk" };
--The
"carl" <cz******@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
The Add() methods exepct a string and double, respectively. Try this:

ProductName.Add(dr.GetString(0));
UnitsInStock.Add(dr.GetDouble(1));

or this:

ProductName.Add((string)dr.GetValue(0));
UnitsInStock.Add((double)dr.GetValue(1));

-Carl

Mar 21 '06 #7

P: n/a
For ProductName, both of the suggested methods works.

But with
UnitsInStock.Add(objReader.GetDouble(1));
or
UnitsInStock.Add((double)objReader.GetValue(1));

I get the following error:

Exception Details: System.InvalidCastException: Specified cast is not valid.

"carl" <cz******@gmail.com> wrote in message news:11**********************@i39g2000cwa.googlegr oups.com...
The Add() methods exepct a string and double, respectively. Try this:

ProductName.Add(dr.GetString(0));
UnitsInStock.Add(dr.GetDouble(1));

or this:

ProductName.Add((string)dr.GetValue(0));
UnitsInStock.Add((double)dr.GetValue(1));

-Carl

Mar 21 '06 #8

P: n/a
Web learner <be******@learning.edu> wrote:
For ProductName, both of the suggested methods works.

But with
UnitsInStock.Add(objReader.GetDouble(1));
or
UnitsInStock.Add((double)objReader.GetValue(1));

I get the following error:

Exception Details: System.InvalidCastException: Specified cast is not valid.


Well, that suggests that column 1 isn't actually a double.

I suggest you fetch it as an object, then see what type it actually is.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 21 '06 #9

P: n/a
cast the value to the type
ProductName.Add((string)dr.GetValue(0));

Also, you will want to follow the .NET framework coding rules. I was
rather confused at what you were doing. PascalCasing is for
Properties, MethodNames, and Types. You should make your variables
camelCased as...

List<string> productName = new List<string>();
List<double> unitsInStock = new List<double>();

this will help remove confusion in the future.

Here's a link for the guidelines...
http://msdn.microsoft.com/library/de...guidelines.asp

You will definately want to buy the book....
http://www.amazon.com/gp/product/032...lance&n=283155
the most important book you will ever buy for .NET or any development.

Let me know if you have other questions...

Mar 21 '06 #10

P: n/a
Thanks for your helps. I have modified the code and attach it at the bottom of this message.

At line unitsInStock.Add((double)dr.GetValue(1)); I still get same error: Specified cast is not valid

If you comment out above code line and also Response.Write(unitsInStock[0]);
then the code works fine. That means it works for productName array.

My purpose is to populate an array from SqlDataReader as below:

double[] unitsInStock = { 120, 104, 112, 111 };
string[] productName = { "Boysenberry", "Something", "Another item", "Demo item" };
--------------------------------------------

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection objConnection = new SqlConnection("server=(local)\\SQLEXPRESS; database=Northwind; integrated security=true;");
String strSQL = "SELECT productName, unitsInStock FROM Products WHERE unitsInStock >= 100";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
objConnection.Open();

SqlDataReader dr = objCommand.ExecuteReader();

List<string> productName = new List<string>();
List<double> unitsInStock = new List<double>();

while (dr.Read())
{
productName.Add((string)dr.GetValue(0)); //No Error
unitsInStock.Add((double)dr.GetValue(1)); //ERROR : Specified cast is not valid
}
dr.Close();
objConnection.Close();

Response.Write(productName[0]);
Response.Write(unitsInStock[0]);
}
</script>

<ag******@gmail.com> wrote in message news:11**********************@z34g2000cwc.googlegr oups.com...
cast the value to the type
ProductName.Add((string)dr.GetValue(0));

Also, you will want to follow the .NET framework coding rules. I was
rather confused at what you were doing. PascalCasing is for
Properties, MethodNames, and Types. You should make your variables
camelCased as...

List<string> productName = new List<string>();
List<double> unitsInStock = new List<double>();

this will help remove confusion in the future.

Here's a link for the guidelines...
http://msdn.microsoft.com/library/de...guidelines.asp

You will definately want to buy the book....
http://www.amazon.com/gp/product/032...lance&n=283155
the most important book you will ever buy for .NET or any development.

Let me know if you have other questions...

Mar 21 '06 #11

P: n/a
Oops sorry...

unitsInStock.Add(Convert.ToDouble(dr.GetValue(1))) ;

For the other one, these would also work...
productName.Add(Convert.ToString(dr.GetValue(0)));
productName.Add(dr.GetValue(0).ToString( ));

Mar 22 '06 #12

P: n/a
Aha.... The problem is solved and I learnt too.

Thanks a lot.......

<ag******@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Oops sorry...

unitsInStock.Add(Convert.ToDouble(dr.GetValue(1))) ;

For the other one, these would also work...
productName.Add(Convert.ToString(dr.GetValue(0)));
productName.Add(dr.GetValue(0).ToString( ));

Mar 22 '06 #13

P: n/a
On Tue, 21 Mar 2006 16:37:14 -0600, "Web learner" <be******@learning.edu> wrote:
Thanks for your helps. I have modified the code and attach it at the bottom of this message.

At line unitsInStock.Add((double)dr.GetValue(1)); I still get same error: Specified cast is not valid

It seems to me, unitsInStock is NOT a double (as Jon Skeet has said). I suggest
that having a unit count column in a table declared as double would be a rare
occasion or error. Counting columns are generally declared as integers.

Why not look at the table design and see if the unitsInStock column really is a
double? The error message you are receiving is telling you it is not a double
but something else (probably an integer).

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Mar 22 '06 #14

P: n/a
On Mon, 20 Mar 2006 18:32:52 -0600, "Web learner" <be******@learning.edu> wrote:
In the following code, I want ArrayList object x to be of type double.

How can I do that?

--Thanks

SqlConnection objConnection = new
SqlConnection("server=(local)\\SQLEXPRESS; database=Northwind; integrated
security=true;");
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strSQL, objConnection);

objConnection.Open();

ArrayList strLabel = new ArrayList();
ArrayList x = new ArrayList();

SqlDataReader dr = objCommand.ExecuteReader();

while (dr.Read())
{
object[] values1 = new object[0];
dr.GetValues(values1);
strLabel.Add(values1);

object[] values2 = new object[1];
dr.GetValues(values2);
x.Add(values2);
}

dr.Close();
objConnection.Close();

In the NorthWind database the UnitsInStock column is a smallint. You must use
dr.GetInt16() to get this value.

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Mar 22 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.