convert ArrayList object to double

W

Web learner

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();
 
W

Web learner

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(string)' has some invalid arguments

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

Any further idea, please??

--




You need CLR 2.0 generics for that...

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

Web said:
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();
 
B

Bruce Wood

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));
 
C

carl

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
 
W

Web learner

Will the resulting array be in following form?

double[] UnitsInStock = { 120, 104, 112, 111 };
string[] ProductName = { "Boysenberry", "Broed", "Momo", "Taifuk" };


--The
 
W

Web learner

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.
 
J

Jon Skeet [C# MVP]

Web learner said:
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.
 
A

agapeton

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/d...ef/html/cpconnetframeworkdesignguidelines.asp

You will definately want to buy the book....
http://www.amazon.com/gp/product/0321246756/104-1212986-9055125?v=glance&n=283155
the most important book you will ever buy for .NET or any development.

Let me know if you have other questions...
 
W

Web learner

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>
 
A

agapeton

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( ));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,096
Latest member
ThurmanCre

Latest Threads

Top