Saturday, March 24, 2012

Populate dropdownlist using table data + text

I have a drop down list and the first option need to be 'ALL', then the remaining options are retrieved from a column of a table. How can this be achieved?
Thanks...I'm not sure it's the best way of doing it, but this is what I do (I'm using a DataSet, it is only mildly different with a SQLDataReader):
Dim myDS as DataSet
' Fill myDS here
Dim myDR as DataRow
myDropDownList.Items.Add(new ListItem("All"))
For Each myDR in myDS.Tables(0).Rows
myDropDownList.Items.Add(new ListItem(myDR.Items("ColumnNameOrIndexHere")))
Next
I do this because the obvious issue with using DataBind is that it flushes whatever data you already had in there. However, looking at the documentation for ListItems, it appears you can do this:
'Get data and bind it to myDropDownList (assuming you are already doing this)
myDropDownList.Items.Insert(0, new ListItem("All"))
The documentation for ListItemCollection in DropDownList (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolslistitemcollectionmemberstopic.asp) might help if neither of these are what you are looking for.

// Bind the DDL to the datasource first...
DDL.DataValueField = "value-column-name-here";
DDL.DataTextField = "text-column-name-here";
DDL.DataSource = dataTable;
DDL.DataBind();

// Add the "ALL" selection at the 1st (#0) position...
DDL.Items.Insert(0, new ListItem("ALL", "ALL"));
NC...


I think the most quicker way is to populatethe ArrayList from the Database using the SqlDataReader (In case youare using SQL Server Database).
The first element of the ArrayList will be "All" which you can simply add like this:
ArrayList a = new ArrayList();
a.Add("All");
after that you use SqlDataReader to populate the ArrayList and finally bind the list to the DropDownList.
Let me know if you need to know more about this technique!

And just how could you possibly think that going through the process of creating an ArrayList out of the data and then binding the ArrayList to the DDL be faster and better than just binding the DDL directly to the data?
NC...

It all depends on how many rows are in the Database Table and how many you are inserting in the DataSet.

In what way? Creating the ArrayList would take more time regardless, even not taking into account the GC having to be called to discard it afterwards.
NC...

DataSet is a collection of DataTables, DataRows and DataColumns so does;nt that make DataSet more heavier than an ArrayList.

Why wouldn't you just use the DataReader as the data source?
DDL.DataValueField = "value-column-name-here";
DDL.DataTextField = "text-column-name-here";
DDL.DataSource = dataReader;
DDL.DataBind();

// Add the "ALL" selection at the 1st (#0) position...
DDL.Items.Insert(0, new ListItem("ALL", "ALL"));
dataReader.Close();
Then you don't have the overhead of creating the ArrayList, or a collection of any kind. After all, you'll never need the ArrayList again after binding the DDL.
NC...


Ohh no no no!
Don't ever send the DataReader on the presentation layer. DataReaderworks while the connection is open so this means you are sending theopen connection on the presentation layer.
NEVER NEVER NEVER send the DataReader on the presentation layer. Thisis also like mixing the DataBase layer with presentation layer with noBLL in between.

azamsharp wrote:

Ohh no no no!
Don't ever send the DataReader on the presentation layer. DataReader works while the connection is open so this means you are sending the open connection on the presentation layer.
NEVER NEVER NEVER send the DataReader on the presentation layer. This is also like mixing the DataBase layer with presentation layer with no BLL in between.


Weren't you the one transferring the DataReader into an ArrayList? My data layer does nothing but produce DataTables and DataSets, so there are no Connection objects, Command objects, DataReader objects, etc in my presentation layer (nor in the entire application layer for that matter).
Here is the part that I disagree with you on:

azamsharp wrote:

I think the most quicker way is to populate the ArrayList from the Database using the SqlDataReader (In case you are using SQL Server Database).


It's no quicker to create an ArrayList than a DataTable from a DataReader (if you need the code, let me know and I'll post it). In fact, unless you create a custom class and a custom collection, you'll be very restricted with an ArrayList as you have no access to column names to attach the DataValueField and DataTextField properties of the DDL, so you'll have to use the same value for both.
NC...


>>Weren't you the one transferring the DataReader into an ArrayList?
Thats done in the business logic layer and not the presentation layer which you have demonstrated.


I would agree with azamsharp if it was an application with a static connection. However, in the case of a web page, your web server is maintaining the DB Connection either while you are populating the ArrayList from the DataReader or while performing the DataBind. No connection is maintained by the client computer, only the web server.

Well then we're talking the same thing then. My way uses a DataTable and your way uses an ArrayList. both constructed in the data layer.

The main problem, as I see it, is that using the ArrayList, you'll be restricted to one column (unless you create a custom class, stuffing that into the ArrayList, which will also take more time and code), since you have no access to column names in an ArrayList to attach to the DataValueField and DataTextField properties of the DDL, therefore, you'll have to use the same value for both.

NC...

0 comments:

Post a Comment