0

I am working on a Shopping cart application. It has already been developed by some developer. But i tried to rebuild it but i am getting the following exception

An exception of type 'System.NullReferenceException' occurred in App_Web_jwfiir5n.dll but was not handled in user code Additional information: Object reference not set to an instance of an object.

The Code in my View is below:

 <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                      <td>@(Model.IndexOf(item) + 1)</td>
                      <td><a style="color:cornflowerblue" title="Click to see the product detail" href="/admin/ProductDetail?productId=@item.ProductId"> @item.Tbl_Product.ProductName</a></td>
                      <td>@(item.Tbl_Members.FirstName + " " + item.Tbl_Members.LastName)</td>
                      <td>@item.Tbl_Members.EmailId</td>
                    </tr>
                }
             </tbody>

And The Controler side is below:

 public ActionResult OrderDetail(int productId) 
{
    List<Tbl_Cart> ProductOrders = _unitOfWork.GetRepositoryInstance<Tbl_Cart>().GetListByParameter(i => i.CartStatusId == 3 && i.ProductId == productId).ToList(); 
    return View(ProductOrders); 
} 

Can anyone help me with this error.

Alex
  • 11
  • 2
  • 2
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Apr 22 '17 at 10:44

2 Answers2

2

Check for nulls

<tbody>
if(Model != null)
{

  @foreach (var item in Model)
    {
       <tr>
          <td>@(Model.IndexOf(item) + 1)</td>
          <td><a style="color:cornflowerblue" title="Click to see the product detail" href="/admin/ProductDetail?productId=@item.ProductId"> @item.Tbl_Product.ProductName</a></td>
          <td>@(item.Tbl_Members.FirstName + " " + item.Tbl_Members.LastName)</td>
          <td>@item.Tbl_Members.EmailId</td>
       </tr>
    }

}
 </tbody>
Krishna
  • 1,807
  • 1
  • 9
  • 21
-1
protected void Page_Load(object sender, EventArgs e)
{
    //String mycon = "Data Source=HP-PC\\SQLEXPRESS; Initial Catalog=CollegeData; Integrated Security=True";
    String myquery = "Select * from Employee where Emp_Id=" + Request.QueryString["Emp_Id"];
    SqlConnection conn = new SqlConnection(con);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = myquery;
    cmd.Connection = conn;
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    //DataSet ds = new DataSet();
    //da.Fill(ds);
    //if (ds.Tables[0].Rows.Count > 0)
    {
        Label1.Text = Request.QueryString["Emp_Id"].ToString();
        Label2.Text = Request.QueryString["Emp_Name"].ToString();
        Label3.Text = Request.QueryString["Emp_Address"].ToString();
        Label4.Text = Request.QueryString["Emp_Designation"].ToString();
        Label5.Text = Request.QueryString["Emp_BasicPay"].ToString();
        Label6.Text = Request.QueryString["Emp_Email"].ToString();
        Label7.Text = Request.QueryString["Emp_Mobile"].ToString();
    }
    conn.Close();
adiga
  • 28,937
  • 7
  • 45
  • 66
Ashraf
  • 1
  • 1