类型转换错误 [英] Type conversion Error

查看:89
本文介绍了类型转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void gvProd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        decimal total = 0;
        decimal profit = 0;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Label lblTotal=(Label)e.Row.FindControl("lbltotal");
            total = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
            string stotal = Convert.ToDecimal(total).ToString() ;
            lblTotal.Text = stotal; // Here error occurs
        }
    }





错误消息:对象引用未设置为对象的实例。



Error message: Object Reference not set to an instance of an object.

推荐答案

大多数情况下,如果您无法在页面中找到控件,则会发生此错误,如lbltotal。

请检查此。

希望这会帮助您。


如果变量值为null,请务必检查变量的值!



Always check the value of a variable if it could be null!

protected void gvProd_RowDataBound(object sender, GridViewRowEventArgs e)
{
    decimal total = 0,
            profit = 0;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        object AchievedPer = DataBinder.Eval(e.Row.DataItem, "AchievedPer");

        if(AchievedPer != null)
        {
            object obj = e.Row.FindControl("lbltotal");

            if(obj != null)
            {
                Label lblTotal = (Label)obj;
                lblTotal.Text = AchievedPer.ToString();
            }
        }
    }
}


onvert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));



您不能将字符串类型转换为十进制。


You cannot convert a string type to decimal.


这篇关于类型转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆