views:

59

answers:

2

Hello all,

I'm using linq2sql in my asp.net app. When using it with linq2sqldatasource object everything works, i mean i bind it without no code to a detailsview control. My idea is when i click a row in the detailscontrol e will load/add to the same page a customwebcontrol that will permit edit the data. For that i need to load some items to fill the dropdowns in that customcontrol, and in its load event i have the follwoing code that doesn't work and i can't see why. It raises a object null reference exception.

example:


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //loads combobox with  organizations
                using (MyDataContext cdc = new MyDataContext())
                {
                    var queryOrgs = from p in cdc.orgUnits
                                    select p;

                    //Organizations
                    dropDownOrgs.DataSource = queryOrgs.ToList();
                    dropDownOrgs.DataValueField = "orgUnitID";
                    dropDownOrgs.DataTextField = "orgUnitName";
                    dropDownOrgs.DataBind();
                }
            }
        }

Anyone know what is happenning? Looks like when i want to bind all by myself manually something do not work :(

Hope you can help me.

Thanks. Teixeira

+1  A: 

@Chalkey is correct. I have run into this error myself, where due to the fact that LINQ to SQL does "lazy" querying, it waits till the last minute to actually perform the query.

This means that it may wait until after the page_load function to do the query (and therefore outside of the using statement).

Therefore, return the data as a list with .ToList() to force it to run the query immediately.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
     //loads combobox with  organizations
     using (MyDataContext cdc = new MyDataContext())
     {
      List<orgUnit> queryOrgs = (
       from p in cdc.orgUnits
       select p
       ).ToList();

      //Organizations
      dropDownOrgs.DataSource = queryOrgs.ToList();
      dropDownOrgs.DataValueField = "orgUnitID";
      dropDownOrgs.DataTextField = "orgUnitName";
      dropDownOrgs.DataBind();
     }
    }
}
Michael La Voie
A: 

Hi,

Thanks both guys! Actually the ToList() gets the results from the database, because I can watch the queryObject and it contains the records, but in other hand it continues raising null reference exception... Any further ideas? The strange of all is that with linq2sql datasourse control it binds ok.

[EDITED] Actually is the dropdownlist control that is null....it shouldn't...usually in the load event the controls should be instantiated right?

br, Teixeira

byte_slave
i commented the code, um put it in a separate method and call it differently and it binds, so the issue is that the dropdownlist for any reason is not instatiated when the load event is fired....but another question/doubt arises: Wasn't suppose any webcontrol or in this case mycustomcontrol when have the property visible=false not be processed in the server side? In this case it shouldn't even fire the load event...Thanks.br,Teixeira
byte_slave
Ok.I found the bikg little details that messed up everything. I'm pretty new or back again to the asp.net and i just found that the vs2008 refactor do not refactors inside the markup, so basically i renamed the dropdownlists and the markup still had the original names thats why the dropdown control was never instantiated. Problem solved. Thanks guys for spending the time help me.br,Teixeira
byte_slave