0
 protected void EditButton_Click(object sender, EventArgs e)
    {

        TextBox tdcd = (TextBox)FormViewDiagnostic.FindControl("DIAG_COMPL_DATETextBox");
        TextBox tdrcd = (TextBox)FormViewDiagnostic.FindControl("DIAG_REVIEW_COMPL_DATETextBox");
        RadioButtonList rbl =(RadioButtonList)FormViewDiagnostic.FindControl("DIAG_LL_APPROVALRadioButtonList");
        TextBox tll = (TextBox)FormViewDiagnostic.FindControl("DIAG_LL_COMMENTSTextBox");

        //if (!"".Equals(tdcd) && !"".Equals(tdrcd))

        if (!string.IsNullOrEmpty(tdcd.Text) && !string.IsNullOrEmpty(tdrcd.Text))
        {

            //tdcd.Visible = true;
            //tdrcd.Visible = true;

            FormViewDiagnostic.FindControl("DIAG_LL_APPROVALRadioButtonList").Visible = true;
            FormViewDiagnostic.FindControl("DIAG_LL_COMMENTSTextBox").Visible = true;
        }
        else
        {
            //tdcd.Visible = false;
            //tdrcd.Visible = false;
            FormViewDiagnostic.FindControl("DIAG_LL_APPROVALRadioButtonList").Visible = false;
            FormViewDiagnostic.FindControl("DIAG_LL_COMMENTSTextBox").Visible = false;
        }
    }

Object reference not set to an instance of an object at line

if (!string.IsNullOrEmpty(tdcd.Text) && !string.IsNullOrEmpty(tdrcd.Text))
Steve B
  • 36,818
  • 21
  • 101
  • 174
Learner
  • 43
  • 1
  • 2
  • 10
  • 1
    Beautiful naming conventions... – CodesInChaos Jul 06 '12 at 10:58
  • In which `ItemTemplate` are those TextBoxes? You would normally use `FormView.ItemDataBound` with the appropriate `FormViewMode` (check the [`CurrentMode`](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.currentmode%28v=vs.100%29) property of the `FormView`). – Tim Schmelter Jul 06 '12 at 10:58
  • refer to http://stackoverflow.com/questions/11357746/how-to-hide-or-disable-radiobuttonlist-and-textbox-based-on-condition-of-another – Learner Jul 06 '12 at 11:50

5 Answers5

1

Your tdcd or tdrcd is null. That is why you are getting this exception. You may check them first for the null.

if ((tdcd != null && tdrcd!=null) && (!string.IsNullOrEmpty(tdcd.Text) && !string.IsNullOrEmpty(tdrcd.Text)))
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Showing the same error at line FormViewDiagnostic.FindControl("DIAG_LL_APPROVALRadioButtonList").Visible = false; – Learner Jul 06 '12 at 10:59
  • 1
    You are not finding the control properly. Make sure you right spellings for the controls, also what is FormViewDiagnostic ? – Habib Jul 06 '12 at 11:02
  • FormViewDiagnostic is form_id. All the textboxes and radiobuttonlist are in edititemtemplate in ForView – Learner Jul 06 '12 at 11:03
1

tdcd or tdrcd is null as your searching it and it is not guaranteed that you will always get it.

TextBox tdcd = (TextBox)FormViewDiagnostic.FindControl("DIAG_COMPL_DATETextBox");
TextBox tdrcd = (TextBox)FormViewDiagnostic.FindControl("DIAG_REVIEW_COMPL_DATETextBox");
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
1

tdcd or tdrcd is null. Which means FormViewDiagnostic.FindControl() is returning null for one of them.

This probably means either "DIAG_COMPL_DATETextBox" or "DIAG_REVIEW_COMPL_DATETextBox" is not the correct ID of the control.

Check those IDs match up to what's actually declared on the form.

tomfanning
  • 9,552
  • 4
  • 50
  • 78
0

Do a check like this

if(tdcd != null && tdrcd != null)
{
// do stuff
}
JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
0

The TextBox references(tdcd and/or tdrcd) are null.

You should use the FormView.ItemDataBound event with the appropriate FormViewMode (check the CurrentMode property of the FormView) to get a reference to your controls.

You also need to DataBind the FormView previously, what creates the controls.

For example (asuming that theTextBoxes are in The EditItemTenmplate):

protected void EditButton_Click(object sender, EventArgs e)
{
    Object someSource=null;
    FormViewDiagnostic.ChangeMode(FormViewMode.Edit);
    FormViewDiagnostic.DataSource = someSource;
    FormViewDiagnostic.DataBind();
}


protected void FormViewDiagnostic_DataBound(Object sender, EventArgs e)
{
    var view = (FormView)sender;
    if (view.CurrentMode == FormViewMode.Edit)
    {
        var txt1 = (TextBox)view.FindControl("DIAG_COMPL_DATETextBox");
        var txt2 = (TextBox)view.FindControl("DIAG_REVIEW_COMPL_DATETextBox");
        var txt3 = (TextBox)view.FindControl("DIAG_LL_COMMENTSTextBox");
        var rbl = (RadioButtonList)view.FindControl("DIAG_LL_APPROVALRadioButtonList");
        txt3.Enabled = rbl.Enabled = txt1.Text.Length != 0 && txt2.Text.Length != 0;
    }
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • How to disable radiobuttonlist and textbox3 if textbox 1 and textbox 2 are empty – Learner Jul 06 '12 at 12:24
  • protected void FormViewDiagnostic_DataBound(Object sender,EventArgs e) { if (FormViewDiagnostic.CurrentMode == FormViewMode.Edit) { var tb1 = (TextBox)FormViewDiagnostic.FindControl("DIAG_COMPL_DATETextBox"); var tb2= (TextBox)FormViewDiagnostic.FindControl("DIAG_REVIEW_COMPL_DATETextBox"); var rb1 = (RadioButtonList)FormViewDiagnostic.FindControl("DIAG_LL_APPROVALRadioButtonList"); var tb3 = (TextBox)FormViewDiagnostic.FindControl("DIAG_LL_COMMENTSTextBox"); } } – Learner Jul 06 '12 at 12:26
  • protected void EditButton_Click(object sender, EventArgs e) { Object someSource = null; FormViewDiagnostic.ChangeMode(FormViewMode.Edit); FormViewDiagnostic.DataSource = someSource; FormViewDiagnostic.DataBind(); } – Learner Jul 06 '12 at 12:27
  • Now I able to hide the control alone but label is visible – Learner Jul 06 '12 at 12:45
  • @Viswa: I have showed how to disable the controls as requested, to hide them you need to set the `Visible` property instead of the `Enabled` property. I have no clue what label you want to hide. – Tim Schmelter Jul 06 '12 at 12:49
  • Approval Approved RejectedNone  Comments – Learner Jul 06 '12 at 12:51
  • @Viswa: You should either edit your question and post that aspx markup or try to put it in plain language to explain me what you're trying to achieve. You cannot expect me to interpret these comments. Apart from that, **there is no label in your `EditItemTemplate`**. – Tim Schmelter Jul 06 '12 at 12:54