473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

convert ArrayList object to double

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
11 1848
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
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
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
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
15
by: GTi | last post by:
If I use: ArrayList TimeScale = new ArrayList(); TimeScale.Capacity = 1000; TimeScale="test 1" The last line trow me an error: Index was out of range. Must be non-negative and less than the...
1
by: Marcus Kwok | last post by:
I wrote a little test program to demonstrate what may be a misunderstanding on my part in the behavior of ArrayList::Sort(). I defined a class (Test) that has two data members: a System::String*...
14
by: Web learner | last post by:
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;...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.