473,831 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ 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=Northw ind; integrated
security=true;" );
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);

objConnection.O pen();

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

SqlDataReader dr = objCommand.Exec uteReader();

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

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

dr.Close();
objConnection.C lose();
Mar 21 '06
14 10604
Thanks for your helps. I have modified the code and attach it at the bottom of this message.

At line unitsInStock.Ad d((double)dr.Ge tValue(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 = { "Boysenberr y", "Something" , "Another item", "Demo item" };
--------------------------------------------

<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>
<%@ Import Namespace="Syst em.Web.UI" %>
<%@ Import Namespace="Syst em.Collections. Generic" %>
<script language="c#" runat="server">
private void Page_Load(objec t sender, System.EventArg s e)
{
SqlConnection objConnection = new SqlConnection(" server=(local)\ \SQLEXPRESS; database=Northw ind; integrated security=true;" );
String strSQL = "SELECT productName, unitsInStock FROM Products WHERE unitsInStock >= 100";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);
objConnection.O pen();

SqlDataReader dr = objCommand.Exec uteReader();

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

while (dr.Read())
{
productName.Add ((string)dr.Get Value(0)); //No Error
unitsInStock.Ad d((double)dr.Ge tValue(1)); //ERROR : Specified cast is not valid
}
dr.Close();
objConnection.C lose();

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

<ag******@gmail .com> wrote in message news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
cast the value to the type
ProductName.Add ((string)dr.Get Value(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.Ad d(Convert.ToDou ble(dr.GetValue (1)));

For the other one, these would also work...
productName.Add (Convert.ToStri ng(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.goo glegroups.com.. .
Oops sorry...

unitsInStock.Ad d(Convert.ToDou ble(dr.GetValue (1)));

For the other one, these would also work...
productName.Add (Convert.ToStri ng(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******@learn ing.edu> wrote:
Thanks for your helps. I have modified the code and attach it at the bottom of this message.

At line unitsInStock.Ad d((double)dr.Ge tValue(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******@learn ing.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=Northw ind; integrated
security=true; ");
String strSQL = "SELECT ProductName, UnitsInStock FROM Products
WHERE UnitsInStock >= 50";
SqlCommand objCommand = new SqlCommand(strS QL, objConnection);

objConnection.O pen();

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

SqlDataReader dr = objCommand.Exec uteReader();

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

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

dr.Close();
objConnection.C lose();

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
3259
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 storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
6
5212
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 then this structure in a Arraylist Afterwards I want to add the values of len h name and typeval to all the padrecords in the arraylist The count of items in the arraylist can be between 1 and 5000
15
1715
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 size of the collection. Parameter name: index
1
1235
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* (name) and a double value. Test implements IComparable, so I made the CompareTo() function sort first by name, then by the value of the double. When I run the code below, it sorts by name correctly, but it does not sort correctly the items...
11
1888
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; database=Northwind; integrated security=true;"); String strSQL = "SELECT ProductName, UnitsInStock FROM Products
4
1251
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 added to the ShoppingCart be removed , by only knowing the string description. public class Appliance:IAppliance { string description ;
3
8755
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; this produced an error
0
1354
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 student.txt file . A sample is shown below. I also want to implement the string tokenizer Clark Kent,190409211,1209 Lynn Lane,Burton,OH,21091 Lois Lane,524673319,57 Wilson Avenue,Wheeling,WVA,35120 Carl Sampson,451239813,401 Mercer...
5
3013
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 output them. There are 2 classes, Bank and BankAccount. I also have a tester. Any help would be appreciated. Bank package bank; import java.util.ArrayList; /** This bank contains a collection of bank accounts.
0
9794
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10497
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10212
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9319
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5788
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4420
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.