Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding a DropDownList with CodeBehine

Status
Not open for further replies.

WorkerBeeJ

Programmer
Aug 6, 2002
34
US
I have an asp:DropDownList contained in an asp:table and I'm trying to populate it with items contained in a dataset. Here's the control:

<asp:TableCell Wrap=&quot;False&quot;><asp:DropDownList id=&quot;lstBusinessUnit&quot; runat=&quot;server&quot; DataValueField=&quot;BusinessUnitID&quot; DataTextField=&quot;BusinessUnitInfo&quot; Width=&quot;100&quot; ></asp:DropDownList></asp:TableCell>

And here's the codebehind where I'm trying to populate the DropDownList:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckValidSession()
Dim dsBusinessUnits As DataSet
With New BusinessUnitBus()
dsBusinessUnits = .GetActiveBusinessUnits()
End With
lstBusinessUnits.DataSource = dsBusinessUnits '**line 31
lstBusinessUnits.DataBind()
End Sub

I'm getting a NullReferenceException on line 31. Can anyone tell me where I'm going wrong? (Or let me know what else I should post?)

thanks!
 
Can you post the code for GetActiveBusinessUnits()

cheers
 
Problem solved. Here's the solution in case anyone's interested:

<asp:DropDownList id=&quot;lstBusinessUnit&quot; runat=&quot;server&quot; DataValueField=&quot;BusinessUnitID&quot; DataTextField=&quot;BusinessUnitInfo&quot; DataSource=<%# BusinessUnits %> ></asp:DropDownList>

And here's the (partial) codebehind:
Private dsBusinessUnit As DataSet

...
If Not Page.IsPostBack Then
GetBusinessUnits()
End If
...

Public Sub GetBusinessUnits()
With New BusinessUnitBus()
dsBusinessUnit = .GetActiveBusinessUnits()
End With
End Sub

Public ReadOnly Property BusinessUnits() As DataSet
Get
Return dsBusinessUnit
End Get
End Property
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top