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