Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Gridview问题对象不能从DBNull转换为其他类型_C#_Asp.net_Gridview - Fatal编程技术网

C# Gridview问题对象不能从DBNull转换为其他类型

C# Gridview问题对象不能从DBNull转换为其他类型,c#,asp.net,gridview,C#,Asp.net,Gridview,嗨,我正在尝试在gridview中显示购物车详细信息 在我的gridview中,我有一个名为imagedelete的图像按钮,如果我点击这个记录,它将被删除。该功能运行良好。在RowDataBound事件的同一个gridview中,我还计算一列的总量并将其显示在页脚中,但当我删除一条记录时,它会给出一些异常 下面是gridview的代码 受保护的void gvDetails_RowDataBound(对象发送方,GridViewRowEventArgs e) { if (e.Ro

嗨,我正在尝试在gridview中显示购物车详细信息 在我的gridview中,我有一个名为imagedelete的图像按钮,如果我点击这个记录,它将被删除。该功能运行良好。在RowDataBound事件的同一个gridview中,我还计算一列的总量并将其显示在页脚中,但当我删除一条记录时,它会给出一些异常

下面是gridview的代码 受保护的void gvDetails_RowDataBound(对象发送方,GridViewRowEventArgs e) {

        if (e.Row.RowType == DataControlRowType.DataRow)
            {
                total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Total"));
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lblamount = (Label)e.Row.FindControl("lblTotal");
                if (total != 0)
                {
                    lblamount.Text = total.ToString();
                }
                else
                {
                    lblamount.Visible = false;
                }
            }
        //}
    }
        int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["CartId"].ToString());


        con.Open();

        SqlCommand cmd = new SqlCommand("delete from tblCart where CartId='" + userid + "'", con);


        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            Response.Write("Success");
            BindGridView(visitorId);

        }
    }

  void BindGridView(string visitor)
    {
        cmd = new SqlCommand("select c.CartId,c.EmailId, c.ProductId,p.ProductName,p.ProductImage,v.VariantId,v.VariantName,c.Quantity,v.ProductCost,(v.ProductCost*c.Quantity) as Total from tblCart as c join tblProducts as p on c.ProductId=p.ProductId join tblVariant as v on c.VariantId=v.VariantId where c.EmailId='" + visitor + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            int columncount = gvDetails.Rows[0].Cells.Count;
            gvDetails.Rows[0].Cells.Clear();
            gvDetails.Rows[0].Cells.Add(new TableCell());
            gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvDetails.Rows[0].Cells[0].Text = "No Records Found";
        }
    }
受保护的void gvDetails\u行删除(对象发送方,GridViewDeleteEventArgs e) {

        if (e.Row.RowType == DataControlRowType.DataRow)
            {
                total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Total"));
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lblamount = (Label)e.Row.FindControl("lblTotal");
                if (total != 0)
                {
                    lblamount.Text = total.ToString();
                }
                else
                {
                    lblamount.Visible = false;
                }
            }
        //}
    }
        int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["CartId"].ToString());


        con.Open();

        SqlCommand cmd = new SqlCommand("delete from tblCart where CartId='" + userid + "'", con);


        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            Response.Write("Success");
            BindGridView(visitorId);

        }
    }

  void BindGridView(string visitor)
    {
        cmd = new SqlCommand("select c.CartId,c.EmailId, c.ProductId,p.ProductName,p.ProductImage,v.VariantId,v.VariantName,c.Quantity,v.ProductCost,(v.ProductCost*c.Quantity) as Total from tblCart as c join tblProducts as p on c.ProductId=p.ProductId join tblVariant as v on c.VariantId=v.VariantId where c.EmailId='" + visitor + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            int columncount = gvDetails.Rows[0].Cells.Count;
            gvDetails.Rows[0].Cells.Clear();
            gvDetails.Rows[0].Cells.Add(new TableCell());
            gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvDetails.Rows[0].Cells[0].Text = "No Records Found";
        }
    }
下面是我试图删除记录时遇到的错误 对象不能从DBNull强制转换为其他类型。在此行 总计+=转换为32(datainder.Eval(如Row.DataItem,“总计”)


有人能帮我解决这个问题吗。

您的数据库字段包含空值,因此不能转换为其他类型(在您的实例中为Int)。请阅读。

您好,我引用您建议的链接,并添加这行代码f(DBNull.Value.Equals(sender)| sender==NULL |然后集成我的行绑定代码来计算列总数。如果(!DBNull.Value.Equals(datainder.Eval(e.row.DataItem,“total”))那么我仍然有相同的问题