简体   繁体   中英

How to convert null to string in Event Gridview RowDeleting on Textbox

i want convert value null to string in Event Gridview RowDeleting on Textbox. But error "Object reference not set to an instance of an object."

Code Behide:

protected void gvTerm_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
TextBox txtCountryRate_Te = (TextBox)gvTerm.Rows[e.RowIndex].FindControl("txtCountryRate_Te");                        
            if (txtCountryRate_Te == null)
            {
                txtCountryRate_Te.Text = string.Empty;  //<== Error Object reference not set to an instance of an object.
            }  
}

Thanks in advance. ;)

the error exactly tells what is happening. you are trying to access and set property of null object.

to set String.Empty as text to your object. just create new instance of object with empty text.

    protected void gvTerm_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        TextBox txtCountryRate_Te = (TextBox)gvTerm.Rows[e.RowIndex].FindControl("txtCountryRate_Te");
        if (txtCountryRate_Te == null)
        {
            txtCountryRate_Te = new TextBox
            {
                Text = String.Empty
            };
        }
    }

How ever the default value is String.Empty so you can simplify it to

txtCountryRate_Te = new TextBox();

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