问题描述
我被严重困扰了。指数超出范围。必须是非负数且小于集合的大小。实时网站链接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
推荐答案
这篇关于指数超出范围。使用itemdatabound作为listview时,必须是非负数且小于集合的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!