【问题标题】:An exception of type 'System.NullReferenceException' occurred in App_Web_jwfiir5n.dll but was not handled in user codeApp_Web_jwfiir5n.dll 中出现“System.NullReferenceException”类型的异常,但未在用户代码中处理
【发布时间】:2017-04-22 04:14:46
【问题描述】:

我正在开发一个购物车应用程序。它已经由一些开发人员开发。但我试图重建它,但我得到以下异常

App_Web_jwfiir5n.dll 中出现“System.NullReferenceException”类型的异常,但未在用户代码中处理 附加信息:对象引用未设置为对象的实例。

我认为的代码如下:

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

控制器端如下:

 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); 
} 

谁能帮我解决这个错误。

【问题讨论】:

标签: asp.net-mvc cart shopping


【解决方案1】:

检查空值

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

【讨论】:

    【解决方案2】:
    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();
    

    【讨论】:

    • 这和问题有什么关系?
    • 在下一页生成发票账单,但错误。对象引用未设置为对象实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2016-02-13
    • 2018-10-25
    • 2016-09-21
    • 2015-05-30
    相关资源
    最近更新 更多