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