1

I have a dropdownlist control.

<asp:DropDownList runat="server" ID="ddlMarketerContract" CssClass="form-control" />

I set the DataSource in the code-behind using LINQ and EF

ddlMarketerContract.DataSource = context.Table.Select(x => x.ColumnName).Distinct().ToList();

But while stepping through the debug, when I go to bind the data

ddlMarketerContract.DataBind();

I get an "Object reference not set to an instance of an object" error.

Not sure why it lets me set the DataSource, but doesn't find the object when binding the control.

I have a similar setup on another page, but that works as expected. I thought maybe it's because I don't have DataTextField/DataValueField set, but the other page didn't have those set, and it still worked fine.

Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
toadfromgrove
  • 143
  • 2
  • 14
  • Are you sure that the DataSource is being set with the list that you expect? Can we see the code with a bit more context? Are you calling DataBind immediately following the DataSource? – mclark1129 May 28 '19 at 15:28
  • Yes, the LINQ is returning the desired dataset. While debugging, after setting the datasource, if I drill down the dropdownlist control and look at it's datasource I see about 35 results. It's not immediate. I set the datasource of a few dropdownlists in a Using block then bind them after closing the block. I'll try moving the bind command into the block right after the datasource set though. – toadfromgrove May 28 '19 at 15:32
  • Moving the DataBind command right under the DataSource set line did not work. I've also ran checks like if (ddlMarketerContract != null) and if (ddlMarketerContract.DataSource != null) and both returned true so the control and the DataSource are present. I'm not sure what it's saying there is no object to. – toadfromgrove May 28 '19 at 15:34
  • Can you post the entire lifecycle method you're using? – mclark1129 May 28 '19 at 15:37
  • Once I strip out setting and binding the other dropdownlists, above is really all there is concerning the control. In the Page_Load event I declare a Using to open the context, then I set the datasource using LINQ, close the Using, then databind – toadfromgrove May 28 '19 at 15:43
  • It's hard to say what the issue is, but perhaps try binding to some mock data using a basic `List` just to see if you can determine if the problem is with the databinding or the EF. – mclark1129 May 28 '19 at 15:54
  • I think it has something to do with the dataset. When I set the dataset to the entire table and set the DataTextField/DataValueField to the column name it worked. It just doesn't make sense when the other page I don't set DataTextField/DataValueField and set the datasource to a distinct values from the column to return a list of strings and it works. – toadfromgrove May 28 '19 at 15:54
  • Ok it does appear to be the LINQ statement. I was able to bind the control to a new List { [values] }. The only odd part is the LINQ statement is a copy/paste of the other pages LINQ statement that works just fine. – toadfromgrove May 28 '19 at 16:06

1 Answers1

1

Problem was with the LINQ statement. Upon creating a generic list of string values I was able to bind the control just fine. Will modify the LINQ query to resolve the issue.

toadfromgrove
  • 143
  • 2
  • 14