指数超出范围。使用itemdatabound作为listview时,必须是非负数且小于集合的大小 [英] Index was out of range. Must be non-negative and less than the size of the collection when using itemdatabound for listview

查看:71
本文介绍了指数超出范围。使用itemdatabound作为listview时,必须是非负数且小于集合的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被严重困扰了。指数超出范围。必须是非负数且小于集合的大小。实时网站链接www.brandstik.in/products/ready-stock。在这一页上,我展示了36种产品。当我转到下一页时,它会显示一个异常页面。我试图删除我的ItemDataBound代码然后它工作正常。那么我的代码有什么问题吗?



我尝试过:



OnPageLoad

I got stucked badly in this. Index was out of range. Must be non-negative and less than the size of the collection. Live Site link www.brandstik.in/products/ready-stock. Here on one page I am showing 36 Products. When I go to next page then it shows a exception page. I tried to remove my ItemDataBound code then it works fine. So is there any problem with my code?

What I have tried:

OnPageLoad

Private Sub populatePage()
        Try
            query = "SELECT * FROM products where stock_status = @stock_status and status = @status"
            Dim conString As String = ConfigurationManager.ConnectionStrings("conio").ConnectionString
            Dim con As New MySqlConnection(conString)
            Dim cmd As New MySqlCommand(query)
            cmd.Parameters.AddWithValue("@stock_status", "Ready Stock")
            cmd.Parameters.AddWithValue("@status", "active")
            con.Open()
            Dim da As New MySqlDataAdapter()
            cmd.Connection = con
            da.SelectCommand = cmd
            Dim dt As New DataTable()
            da.Fill(dt)
            ViewState("Data") = dt
            products.DataSource = dt
            products.DataBind()
            catHeading.Text = "Products In Ready Stock"
            itemCount.Text = dt.Rows.Count.ToString
            catSliderHeader.Text = "Categories"
            Page.Title = "Ready Stock Products" + " | BrandSTIK"
            con.Close()
        Catch ex As Exception
            Response.Write(ex)
        End Try
    End Sub







Private Sub products_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles products.ItemDataBound
        Try
            If e.Item.ItemType = ListViewItemType.DataItem Then
                Dim itm As ListViewDataItem = CType(e.Item, ListViewDataItem)
                Dim productID As String = products.DataKeys(itm.DataItemIndex)("ID").ToString()
                query = "SELECT stock_status FROM products WHERE ID = '" + productID + "'"
                Dim dt As DataTable = Me.GetData(query)
                If dt.Rows.Count > 0 Then
                    CType(e.Item.FindControl("checkReadyStock"), Label).Text = dt.Rows(0)("stock_status").ToString

                    If CType(e.Item.FindControl("checkReadyStock"), Label).Text = "Ready Stock" Then
                        CType(e.Item.FindControl("readyStock"), Image).Visible = True
                    End If
                End If
            End If
        Catch ex As Exception
            Response.Write(ex)
        End Try
End Sub





ItemDataBound我曾用于显示红色图像(在现货库存中),因为当库存列值为是的,对于特定的产品,然后只显示该图像。





ItemDataBound I have used to display Red image (In Ready Stock) because when ready stock column value is yes for particular product then only that image gets displayed.

Protected Sub OnPagePropertiesChanging(sender As Object, e As PagePropertiesChangingEventArgs)
        TryCast(products.FindControl("DataPager1"), DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
        products.DataSource = ViewState("Data")
        products.DataBind()
    End Sub

推荐答案

错误消息表示您尝试访问的对象不是数组或该元素不存在。



使用调试器并检查变量是否包含您期望的内容。

如果行很复杂,将其拆分以查看哪个部分失败。

Nota: try / catch 阻止您收到错误。
The error message means that either the object you try to access is not an array or that the element does not exist.

Use the debugger and check that variables contains what you expect.
if the line is complicated, split it to see which part fail.
Nota: the try/catch prevent you from getting the error.


这篇关于指数超出范围。使用itemdatabound作为listview时,必须是非负数且小于集合的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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