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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a drop down list to a class and databinding 1

Status
Not open for further replies.

gregburningham

Programmer
Jul 12, 2000
40
GB
Hi all

I am having trouble doing a simple task - whilst I have managed to bind a simple drop-down list from a query in other .NET apps I have written, I have not managed to do it in a class using array lists ....

This almost works but for some reason I cannot assign the values of "code" to the dropdown list ?

PLEASE CAN YOU HELP .... !!!! Thanks Greg B

Public Sub BuildArrayList(ByVal blankLine As Boolean, ByVal oraConnectionStr As String, ByVal sqlStr As String _
, ByVal vDropDownList As DropDownList, ByVal listValue As String)
' attempt to pass in and return the ListItem Object itself

Dim newArrayList As New ArrayList()
Dim newArrayListValue As New ArrayList()
' Dim newArrayList As MyArrayList
Dim createArrayException As Exception
Dim createArrayListCommand As New OleDb.OleDbCommand()
Dim oraConnection As New OleDb.OleDbConnection()
Dim arrayListDataReader As OleDb.OleDbDataReader


Try
'Get the Oracle connection string.
oraConnection.ConnectionString = oraConnectionStr
createArrayListCommand.Connection = oraConnection

'Open the Oracle connection.
oraConnection.Open()

'Build the Status LOV from a SQL statement.
createArrayListCommand.CommandText = sqlStr
createArrayListCommand.CommandType = CommandType.Text

arrayListDataReader = createArrayListCommand.ExecuteReader()

'Add a blank line to the array list if required.
If blankLine = True Then
newArrayList.Add("")
End If

While arrayListDataReader.Read
newArrayList.Add(arrayListDataReader.Item("code_desc"))
newArrayListValue.Add(arrayListDataReader.Item("code"))
End While

arrayListDataReader.Close()
vDropDownList.DataSource = newArrayList
vDropDownList.DataBind()

' set item selected
If listValue = "" Then
listValue = newArrayListValue.Item(0)
End If

Dim itemCount As Integer
For itemCount = 0 To newArrayListValue.Count - 1
vDropDownList.Items(itemCount).Selected = False
If newArrayListValue(itemCount) = listValue Then
vDropDownList.Items(itemCount).Selected = True
End If
vDropDownList.Items(itemCount).Value = newArrayListValue(itemCount)
Next

Catch createarrayException
exceptionMessageStr = createArrayException.Message.ToString()

Finally
'Close the Oracle connection.
oraConnection.Close()
End Try
End Sub







 
Try simplifying the code:

vDropDownList.DataSource = arrayListDataReader
vDropDownList.DataTextField = "code_desc"
vDropDownList.DataValueField = "code"
vDropDownList.DataBind()


 
Thanks bboffin for the quick response, I have tried this code out and it works fine - !

I have another question - I would also like to put in a blank line into the dropdown list but I want to avaoid a query with a UNION statement in it that adds the line (with NULL values in it) !

Please could you let me know if I can do this through the code rather than through the query.







 
I may be wrong but I think once you DataBind to a DataSource you're pretty well stuck with it.

You could create a New DataTable and add two columns to it (code_desc and code)
Then add the first row explicitly containing nulls or spaces.
Then loop round the DataReader adding a row from it to the DataTable each time.

Close the DataReader and use the DataTable as the DataSource instead of the DataReader.
 

And put an If-Then check in before building the query to check that the user hasn't selected the blank line (in the change event handler of the dropdown).

-Stephen Paszt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top