dynamically loaded user control with llblgenprodatasource2 caused "Object reference not set to an instance of an object"

Posts   
 
    
bunzee
User
Posts: 84
Joined: 20-Mar-2007
# Posted on: 25-Sep-2008 23:54:13   

llblgen 2.0 built 7/6/2007 vs 2005

I dynamically load and unload a user control which contains llblgenprodatasource2. I run into the same problem as discussed in this thread: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=11318&HighLight=1. My understanding is that the problem is fixed in llblgenpro version 2.5.
Since it is not easy for me to upgrade this project llblgen to 2.5, is there a "later" built version of 2.0 that I am not aware of?
If not, is there a work around to avoid this problem?

Thanks

BZ

bunzee
User
Posts: 84
Joined: 20-Mar-2007
# Posted on: 26-Sep-2008 01:30:06   

Walaa,

I would like to comment your responses in this thread http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=11318&HighLight=1

in the Page_Load handler, don't add controls to the place holder, as follows: Code: if (Session["LoadPath"] != null) {

            Control control = LoadControl(Session["LoadPath"].ToString());


            userControlPlaceHolder.Controls.Clear();
            //userControlPlaceHolder.Controls.Add(control); // not needed
        }

Just comment that line and everything will work just fine.

You must dynamically add the user control here because between post back the user control hierarchy does not save the dynamically created control. However what they should do here is use this routine in the OnInit instead of the Page_Load because OnInit is invoked before LoadViewState.

public partial class WebForm1 : System.Web.UI.Page { protected override void OnInit(EventArgs e) { base.OnInit(e); if (Session["LoadPath"] != null) { Control control = LoadControl(Session["LoadPath"].ToString());

            uxPlaceHolder.Controls.Clear();
            uxPlaceHolder.Controls.Add(control);
        }
    }

    protected void uxBtn1_Click(object sender, EventArgs e)
    {
        string loadPath = "~/UserControls/WebUserControl1.ascx";
        Session["LoadPath"] = loadPath;
    }

    protected void uxBtn2_Click(object sender, EventArgs e)
    {
        string loadPath = "~/UserControls/WebUserControl2.ascx";
        Session["LoadPath"] = loadPath;
    }

    protected void uxBtn3_Click(object sender, EventArgs e)
    {
        uxLbl.Text = "Something else done";
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
                if (Session["LoadPath"] != null)
                {
                    Control control = LoadControl(Session["LoadPath"].ToString());
                    uxPlaceHolder.Controls.Clear();
                    uxPlaceHolder.Controls.Add(control);
                }
    }
}

I tried this but it does not work because when I have my uxBtn3_Click actually remove the control via uxPlaceHolder.Controls.Clear(). However before this function is invoked the OnInit was invoked and line uxPlaceHolder.Controls.Add(control); was executed!

I think the only way to do is to use the llblgenpro built that handles the error condition in llblgenprodatasource2:: OnPageLoadComplete .

BZ

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 26-Sep-2008 10:45:34   

You must dynamically add the user control here because between post back the user control hierarchy does not save the dynamically created control. However what they should do here is use this routine in the OnInit instead of the Page_Load because OnInit is invoked before LoadViewState.

Thanks for the tip.

My understanding is that the problem is fixed in llblgenpro version 2.5. Since it is not easy for me to upgrade this project llblgen to 2.5, is there a "later" built version of 2.0 that I am not aware of?

I think this was not ported back to v.2.0 I strongly recommend you to migrate to v.2.5, this should be a very easy task that won't take 15 minutes from you (including the download and install time). You would just re-generate the code using v.2.5, and then replace the references to the v.2.5 runtime libraries and the newly generated projects. And just to be inn the safe side, you might need to look at the "Breaking changes v2.5" in the "Migrating your code" section in the v.2.5 manual.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 30-Sep-2008 10:37:51   

Or v2.6 of course wink

Frans Bouma | Lead developer LLBLGen Pro