Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 112223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:30:39+00:00 2026-05-11T02:30:39+00:00

I am currently creating an e-commerce site using C# ASP.NET MVC and have just

  • 0

I am currently creating an e-commerce site using C# ASP.NET MVC and have just come across a problem. On pages such as product pages and search results pages, I have to pass Lists of data from my controller to the ViewPage and that works just fine. However, a null reference exception occurs if the Viewdata equals null. It happens inside the viewpage when it loops through the ViewData and displays products or reviews.

//ProductController.cs

public ActionResult Products_Sub(string category, int page) { ViewData['Products'] = database.GetByCategory(category, page); return View(); } 

//ViewPage.cs — product loop

 <ul> foreach (E_Store.Models.Product product in ViewData['Products'] as    List<e_store.models.product>)  {%>   <li>  <img alt='<%= product.Title%>' src='<%= product.Thumbnail %>' />   <a href='/<%=product.Category %>/<%= product.SubCategory %>/<%= product.ASIN %>/1'>  <%=product.Title%></a>  </li>  }%>  </ul>     

The Null Reference Exception occurs when the following piece of code is reached:

  <ul> foreach (E_Store.Models.Product product in ViewData['Products'] as 

What I would like to know is the best way to catch this type of error if it does happen, without resorting to if statements that check to see if it is null.

If anyone knows of a good way of doing this I would really love to know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:30:40+00:00Added an answer on May 11, 2026 at 2:30 am

    In your case I would have your database.GetByCategory(category, page) method simply return an empty list rather than null, this way your foreach statement just won’t have any data to loop through but you won’t get a null exception.

    In the case that you’re not dealing with a list and the item is null, we use

    Html.Encode(ViewData.Eval('Field')) 

    To get the item’s HTML value, it will return ” if the item is null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently inherited a database driven e-commerce site written in C# ASP.Net, with an
I am currently creating a custom e-commerce site (in php, but that's not really
Im currently creating a webportal with ASP.NET which relies heavily on database usage. Basically,
I'm currently creating a WinForm in VB.NET bound to an access database. Basically what
Currently I have the function CreateLog() for creating a a log4net Log with name
I am currently in the process of creating my own blog and I have
I've been playing around with creating an e-commerce site with Codeigniter, and am trying
Im currently creating a tile-based game for android. Using java via dalvik JVM. im
I'm currently creating an explicit reference to this in the outer class so that
I'm currently creating an application for a customer that will allow them to automatically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.