0

good morning guys, i'm developing a website. whereby have to move through records..next and previous...i did something like this

public Product NextProduct(List<Product> products, Product product)
    {
        Product nextproduct = (Product)product;

        for (int i = 0; i < products.Count; i++)
        {
            if (Equals(products[i].ProductId, product.ProductId))
            {
                if (products[i + 1].ProductId == null)
                {
                    return null;
                }
                else
                {
                    nextproduct = products[i + 1];
                }
            }
        }
        return nextproduct;
    }

    public Product PrevProduct(List<Product> products, Product product)
    {
        Product prevproduct = (Product)product;

        for (int i = 0; i < products.Count; i++)
        {
            if (Equals(products[i].ProductId, product.ProductId))
            {
                if (products[i - 1].ProductId == null)
                {
                    return null;
                }
                else
                {
                    prevproduct = products[i - 1];
                }
            }
        }
        return prevproduct;
    }

this method is not working. giving error
i just wanna return the new product. how do i accomplish this...thank u in advance.

Edited by Ezzaral: Moving to correct forum.

2
Contributors
3
Replies
20
Views
4 Years
Discussion Span
Last Post by goco17
0

sorry abt that, i'm getting this particular error. i searched and it says only if a parameter is null. i have checked.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 124:        for (int i = 0; i < products.Count; i++)
Line 125:        {        
Line 126:            if (Equals(products[i].ProductId, product.ProductId))
Line 127:            {
Line 128:                if (products[i + 1].ProductId == null)
0

thank u for the help @JorgeM...i found the problem...i declared something wrong...sorry....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.