Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
VB
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


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

VB
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
Posted
Updated 27-Sep-16 18:40pm
v6
Comments
F-ES Sitecore 27-Sep-16 5:29am    
Your code makes no sense. It is VB.net code in a c# format, I don't see how it even compiles?
SuRaj Dedhia 27-Sep-16 5:42am    
Yes I have to convert it from vb.net to c# because c# gets more visibility then vb.net. Anyways I updated in VB.net code . Can you please help?
F-ES Sitecore 27-Sep-16 5:58am    
It would help if you could at least say what line the error happens on.
SuRaj Dedhia 27-Sep-16 6:00am    
I am not sure on which line exactly error is coming. But when I use ItemDataBound Like in above in my code then only it shows the bunch of exception so I guess problem is somewhere in itemdatabound event
F-ES Sitecore 27-Sep-16 6:36am    
You need to learn how to use the debugger, you can't solve problems by looking at the code and guessing. Put breakpoints in your code at the itemdatabound event and step though it

http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn

1 solution

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.
 
Share this answer
 
Comments
SuRaj Dedhia 28-Sep-16 0:37am    
I identified the issue. On Paging code I was trying to bind listview from ViewState("Data") which gets created onPageLoad but when pageIndex gets changed viewstate gets empty & it throws exception. So I Paging code I called PopulatePage Function but still when pageIndex gets changed it is not getting product ID on ItemDataBound which still creating problem. Can you tell me how this can be resolved?
SuRaj Dedhia 28-Sep-16 1:08am    
Finally by making some changes in ItemDataBound code My entire problem is solved. I need to know How can I reset PageIndex? Becuase suppose If display 18 Products in one page & I have total 100 pages & Jumped to 10th Page Then I applied any filter then it still stays on 10th page. So If products are less then 18 after applying filter then it will display it in first page but because I'm still at 10th page so I don't see any products which found after applying filter. I hope I am able to understand you what is the problem now.
Patrice T 28-Sep-16 1:26am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Or open a new question for this new problem.
SuRaj Dedhia 28-Sep-16 1:56am    
Hi. I posted new question here http://www.codeproject.com/Questions/1133946/Reset-pageindex-for-listview-after-applying-filter . Can you please help?
Patrice T 28-Sep-16 2:23am    
I knew the answer for this one, but I don't have experience on the new problem.
Other expert will see the new question.

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