Which is a better code to avoid throwing Object reference not set to an instance of an object when using Telerik Radtextbox? Are both codes below the same? Can I set a default value to avoid nullreference from throwing?
protected void btnAddSAles_click(object sender, EventArgs e)
{
string orderName = Ordername.Text;
}
or
protected void btnAddSAles_click(object sender, EventArgs e)
{
TextBox b = item.FindControl("Ordername") as TextBox;
string box1 = b.text;
}
I am assuming
FindControlis returningnullfrom theascast you're trying to make. I assume (again) it isn't finding a control namedOrdername, hence you are trying to access aTextproperty on anullobject, which causes theNullReferenceException.What you should do is:
Ordername, as im assuming there should be oneIf the control which invoked the
Button.Clickmay not always be aTextBoxobject, add a nullity check: