索引超出范围异常。 [英] Index Out Of Range Exception .

查看:130
本文介绍了索引超出范围异常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正在尝试使用3层架构加载gridView。现在我不确定我的代码但是现在我收到此错误IndexOutOfRangeException productID并且让我感到困惑。





以下是商业层中获取所有产品的方法



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

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

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;
        }
    }

推荐答案

如果我没有错,请尝试运行用此执行的存储过程手动编写sql server上的代码并检查结果。确保有一个名为productID的列我认为不存在:)



如果我是对的,当你调用属性时会抛出IndexOutOfRangeException,读者[productID],因为返回的结果集中没有这样的字段。



哦,只是在旁边注释..方法getProducts()总是返回一个永远不会为null的ArrayList。因此,在客户端表单中,删除行prodList = new ArrayList();。因为,当存储从方法返回的ArrayList的引用时,在下一行中忽略使用这行代码创建的对象,而不是使用上述代码行创建的对象。



希望这有帮助



问候,Pasan。
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.


找不到您的字段命名产品ID



转到SQL查询窗口并执行 procReturnAllProducts ,结果集中是否存在该字段?也许拼写略有不同?
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?


我认为由于这个原因发送了异常: reader [productID]

检查 productID 是否存在。
I suppose the exception was sent because of this: reader["productID"].
Check that productID exists.


这篇关于索引超出范围异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆