473,466 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
14 10564
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
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
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
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
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
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
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
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
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
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
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
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 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...
6
by: mm | last post by:
Hi All, I am a newbie in Vb.net and I have some problems I have a structure called padrecord. In this structure i have X,Y len h w name typeval First I add the known values in X and Y. And put...
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*...
11
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;...
4
by: vivekian | last post by:
Hi, There is an ArrayList AppList which contains objects of the type Appliance. The type Appliance consists of a string description and a double price. In what way can an object which has been...
3
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ...
0
by: batbrandman | last post by:
Need help with this...I tihnk it would be easier to see what someones developed program looks like, I keep getting stuck on a few major parts...Thanks very much!!!!!!!!!! The data will be found in a...
5
by: blt51 | last post by:
I need to write a program that handles a bank account and does a number of transactions using an arraylist. However, I'm having trouble getting the arraylist to store all the transactions and then...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.