locked
Help needed with FormView RRS feed

  • Question

  • User-331387351 posted

    Hi

    I am using 3-layered architecture to build my application and I am using FormsView.

    I have a small form where I am able to populate the <g class="gr_ gr_170 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="170" data-gr-id="170">dropdownlist</g> from the database within <EditItemTemplate>. However, when I call the same function to populate the drop-down list in the <InsertItemTemplate>, I get the following error

    Object reference not set to an instance of an object.

    System.NullReferenceException: Object reference not set to an instance of an object. 

    Here is a part of my code

    case "New":
    fvStandardCost.ChangeMode(FormViewMode.Insert);
    DropDownList ddlstIIEventType = (DropDownList)fvStandardCost.Row.FindControl("ddlstIIEventType");
    oDsEventType = oBusinessLogicLayerEventType.ListEventType();
    ddlstIIEventType.DataSource = oDsEventType;
    ddlstIIEventType.DataTextField = "zEventTypeName";
    ddlstIIEventType.DataValueField = "ipknEventTypeID";
    ddlstIIEventType.DataBind();
    break;

    Note that the same piece of code works when in EditItemTemplate.

    Many thanks for your help.

    Kind Regards

    Abhimanyu

    Friday, December 28, 2018 3:08 AM

All replies

  • User-943250815 posted

    Are you sure DropDownList exist on InsertTemplate with exactly same name ("ddlstIIEventType") ?

    Friday, December 28, 2018 2:41 PM
  • User-1716253493 posted

    Seem like you want to bind ddl inside formview

    Move your code to fv databound event

    based current mode, get the ddl using fv.FindControls("ddl"), bind it

    Saturday, December 29, 2018 1:51 AM
  • User-271186128 posted

    Hi Aryaman,

    Object reference not set to an instance of an object.

    System.NullReferenceException: Object reference not set to an instance of an object. 

    Here is a part of my code

    case "New":
    fvStandardCost.ChangeMode(FormViewMode.Insert);
    DropDownList ddlstIIEventType = (DropDownList)fvStandardCost.Row.FindControl("ddlstIIEventType");
    oDsEventType = oBusinessLogicLayerEventType.ListEventType();
    ddlstIIEventType.DataSource = oDsEventType;
    ddlstIIEventType.DataTextField = "zEventTypeName";
    ddlstIIEventType.DataValueField = "ipknEventTypeID";
    ddlstIIEventType.DataBind();
    break;

    Please check your code, generally, we are using the FormView ModeChanging event to change the mode, then, in the FormView databound event, we could check whether the current mode is Insert or Edit, then using the FindControl method to find the control. Please refer to the following code:

            protected void EmployeeFormView_ModeChanging(object sender, FormViewModeEventArgs e)
            {
                EmployeeFormView.ChangeMode(e.NewMode);
                bindgrid();
                if (e.NewMode == FormViewMode.Edit)
                {
                    EmployeeFormView.AllowPaging = false;
                }
                else
                {
                    EmployeeFormView.AllowPaging = true;
                }
            }
            protected void EmployeeFormView_DataBound(object sender, EventArgs e)
            {
                //check the current model and find the dropdownlist.
                if (EmployeeFormView.CurrentMode == FormViewMode.Insert)
                {
    //using FindControl method to find the dropdownlist. DropDownList ddlDesignation = (DropDownList)EmployeeFormView.FindControl("ddlDesignation"); List<Designation> deslist = new List<Designation>() { new Designation(){ ID=101, Name="VI"}, new Designation(){ ID=101, Name="VII"}, new Designation(){ ID=101, Name="VIII"}, }; ddlDesignation.DataSource = deslist; ddlDesignation.DataTextField = "Name"; ddlDesignation.DataValueField = "ID"; ddlDesignation.DataBind(); } }

    Best regards,
    Dillion

    Saturday, December 29, 2018 2:42 AM
  • User-331387351 posted

    Hi Dillion,

    Thank you so much for promptly getting back to me. I will try this and let you know how it went.

    Kind regards,

    Abhimanyu

    Saturday, December 29, 2018 4:35 AM