I have an array of drop down lists. I also have a procedure that initializes the array (at least I thought) or rather, loads the ddls if I'm saying that right. I also have a for loop that takes the data from a data table and binds it to each of the four drop down lists. When I run the program, the loadDDLs seems to work fine, the array length is right and so forth, but when it gets to the for loop to load the ddls with data, I get an Object Reference Not set to an instance of an object error.
I might also mention when I try and load a single drop down list by name, it works fine. But I was trying to avoid writing the same lines four times.
This is my relative code:
Dim theDDLs() As DropDownList = {ddlBuilding, ddlElectrical, ddlMechanical, ddlPlumbing}
______________________
Private Sub LoadDDLs()
DbProcedures.LoadDDLs(ddlBuilding, theDDLs)
DbProcedures.LoadDDLs(ddlElectrical, theDDLs)
DbProcedures.LoadDDLs(ddlMechanical, theDDLs)
DbProcedures.LoadDDLs(ddlPlumbing, theDDLs)
End Sub
______________________
Friend Sub LoadDDLs(ByVal theddl As DropDownList, ByVal theddls() As DropDownList)
Dim Index As Int32
If theddls Is Nothing Then
ReDim theddls(0)
End If
If theddls(0) Is Nothing Then
Index = 0
Else
Index = theddls.Length
End If
ReDim Preserve theddls(Index)
theddls(Index) = theddl
End Sub
____________________________
....(In Private Sub BindContractors())...
If dt.Rows.Count > 0 Then
Dim rw As DataRow = dt.Rows(0)
Dim x As Integer
For x = 0 To (theDDLs.Length - 1)
lblMessage.Text = theDDLs(x).ID
***theDDLs(x).DataSource = dt
theDDLs(x).DataValueField = "Id"
theDDLs(x).DataTextField = "Contractor"
theDDLs(x).DataBind()
Next
End If
_____________________
The line with the stars is where the error is generated.
Any suggestions would be appreciated...Perhaps I have handling the array incorrectly? Can you have an array of drop down lists and populate them through a for loop as I am attempting?
Thanks!
I might also mention when I try and load a single drop down list by name, it works fine. But I was trying to avoid writing the same lines four times.
This is my relative code:
Dim theDDLs() As DropDownList = {ddlBuilding, ddlElectrical, ddlMechanical, ddlPlumbing}
______________________
Private Sub LoadDDLs()
DbProcedures.LoadDDLs(ddlBuilding, theDDLs)
DbProcedures.LoadDDLs(ddlElectrical, theDDLs)
DbProcedures.LoadDDLs(ddlMechanical, theDDLs)
DbProcedures.LoadDDLs(ddlPlumbing, theDDLs)
End Sub
______________________
Friend Sub LoadDDLs(ByVal theddl As DropDownList, ByVal theddls() As DropDownList)
Dim Index As Int32
If theddls Is Nothing Then
ReDim theddls(0)
End If
If theddls(0) Is Nothing Then
Index = 0
Else
Index = theddls.Length
End If
ReDim Preserve theddls(Index)
theddls(Index) = theddl
End Sub
____________________________
....(In Private Sub BindContractors())...
If dt.Rows.Count > 0 Then
Dim rw As DataRow = dt.Rows(0)
Dim x As Integer
For x = 0 To (theDDLs.Length - 1)
lblMessage.Text = theDDLs(x).ID
***theDDLs(x).DataSource = dt
theDDLs(x).DataValueField = "Id"
theDDLs(x).DataTextField = "Contractor"
theDDLs(x).DataBind()
Next
End If
_____________________
The line with the stars is where the error is generated.
Any suggestions would be appreciated...Perhaps I have handling the array incorrectly? Can you have an array of drop down lists and populate them through a for loop as I am attempting?
Thanks!