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.

Recommended Answers

All 3 Replies

What error are you encountering? Can you be more specific?

commented: for helping our +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)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.