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