简体   繁体   中英

Gridview issue Object cannot be cast from DBNull to other types

Hi im trying to display a cart details in a gridview In my gridview i have image button called imagedelete if i click on this record is getting deleted. That functionality is working fine. In the same gridview on RowDataBound event im also calculating the total amount of one column and displaying it in the footer but when i delete a record its giving some exception.

Following is the code for gridview protected void gvDetails_RowDataBound(object sender, 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;
                }
            }
        //}
    }

protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e) {

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

and following is the error that im facing when i try to delete a record Object cannot be cast from DBNull to other types. at this line total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Total"));

can anyone help me resolving this issue.

Your database fields contain NULL values hence they cannot be cast to other types (Int in your instance). Read here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM