Click here to Skip to main content
15,908,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys am trying to load a gridView using 3-tier architecture.Now am not really sure about my code but for now am getting this error "IndexOutOfRangeException productID" and its confusing me.


Below is a method in a Business Layer to get all my products

C#
public ArrayList getProducts()
        {
            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("procReturnAllProducts", con);
                cmd.CommandType = CommandType.StoredProcedure;
                ArrayList productsList = new ArrayList();
                try
                {
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Products products = new Products(Convert.ToInt32(reader["productID"]), Convert.ToString(reader["productName"]), Convert.ToInt32(reader["supplierID"]), Convert.ToInt32(reader["categoryID"]), Convert.ToString(reader["quantityPerUnit"]), Convert.ToDouble(reader["unitPrice"]), Convert.ToInt16(reader["unitInStoct"]), Convert.ToInt16(reader["unitOnOrder"]), Convert.ToInt16(reader["reOrderLevel"]), Convert.ToBoolean(reader["discontinue"]));
                        productsList.Add(products);
                    }//End while
                    reader.Close();
                }
                catch (SqlException)
                {
                    throw new ApplicationException("Error connection to database");
                }
                return productsList;
            }//End Using
        }


Now below is my Client Form

MIDL
public partial class Form1 : Form
    {
        public ProductsBL productBL;
        public ArrayList prodList;
        public Form1()
        {
            InitializeComponent();
             productBL = new ProductsBL();
             prodList = new ArrayList();
             prodList = productBL.getProducts();
             dataGridView1.DataSource = prodList;
        }
    }
Posted
Updated 28-May-18 10:17am
v2

If I am not wrong, try running the stored procedure that is executed with this code on sql server manually and check the results. Ensure that there is a column named "productID" which I think is not present :)

If I am right, the IndexOutOfRangeException is thrown when you call the property, reader["productID"], since there is no such field in the returned resultset.

And oh, just on a side note.. the method "getProducts()" always returns an ArrayList which is never going to be null. Therefore, in your client form, drop the line "prodList = new ArrayList();". Because, the object you create with this line of code is neglected in the next line when the reference to the ArrayList returned from your method is stored, instead of the one created with the above mentioned line of code.

Hope this helps

Regards, Pasan.
 
Share this answer
 
v2
Comments
Anele Ngqandu 9-Mar-11 0:37am    
K thanx, it works
It can't find your field named productID

Go to your SQL query window and execute procReturnAllProducts, does that field exist in the result set? Maybe spelt slightly differently?
 
Share this answer
 
Comments
Anele Ngqandu 9-Mar-11 0:37am    
K thanx, it works
I suppose the exception was sent because of this: reader["productID"].
Check that productID exists.
 
Share this answer
 
v2
Comments
Anele Ngqandu 9-Mar-11 0:37am    
K thanx, it works
why you use the Convert Method. The SQlDataReader provides Get Methods to convert in standard datatypes.

SQLDataReader MSDN[^]

Like this:

int a = reader.GetInt32(reader.GetOrdinal("productID"));
 
Share this answer
 
v2
Comments
Anele Ngqandu 9-Mar-11 0:38am    
K it works. just that i used "productID" instead of "ProductID"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900