RE: Looking for suggestions on populating dropdown from database table
From: Cowboy (Gregory A. Beamer) - MVP (NoSpamMgbworld_at_comcast.netNoSpamM)
Date: 02/09/05
- Next message: Carlos J. Quintero [.NET MVP]: "Re: Using VS 2003 with DotNetFrameWork 2.0"
- Previous message: Mujtaba Syed: "Re: Determining .NET Install Directories"
- In reply to: pantichd: "Looking for suggestions on populating dropdown from database table"
- Next in thread: pantichd: "Re: Looking for suggestions on populating dropdown from database table"
- Reply: pantichd: "Re: Looking for suggestions on populating dropdown from database table"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 9 Feb 2005 07:09:02 -0800
DataSet or Collection, you are doing the same thing. You are pulling from a
bindable object into something else. There are times where this makes sense,
but the most efficient use of a DataReader is as follows:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
BindPropertyTypeDropDown();
}
private void BindPropertyTypeDropDown()
{
//Working with Northwind
string connString = ConfigurationSettings.AppSettings["connString"];
//Would normally have a stored procedure here
string sql = "SELECT EmployeeID, FirstName+' '+LastName AS WholeName FROM
Employees";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
//Note that you have to leave a reader open while you bind
ddlPropertyType.DataSource = dr;
ddlPropertyType.DataValueField = "EmployeeID";
ddlPropertyType.DataTextField = "WholeName";
ddlPropertyType.DataBind();
}
finally
{
if(conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
}
}
Of course, if you are running a multi-tiered application, it is more
efficient to either use business objects or set up a DataSet as a
quasi-business object. The main key here is making sure you are using the
proper interfaces or setting up specific attributes. A good primer:
http://www.codeproject.com/useritems/AspNetBindDatagridVT.asp
Check out the links at the bottom, as well.
--- Gregory A. Beamer MVP; MCP: +I, SE, SD, DBA *************************** Think Outside the Box! *************************** "pantichd" wrote: > Hello, > > I want to populate an employee name dropdown in a webform with values from a database table. I know I can use a data adapter to retrieve the whole table into a dataset and bind the table from the dataset to the dropdown. However, I thought I could do it a little more efficiently by using a datareader to retrieve the name and id columns from the table, put them in a collection and then bind the collection to the dropdown. > > Below is the method I'm using to retrieve a key/value pair from a database table. It returns a collection. Well, now I'm stuck. I can't figure out how to get the collection into the dropdown. > > When reading about dropdowns I keep running into documentation about ListItemCollection but I can't figure out how to go from collection to ListItemCollection and then how to get that bound to the dropdown. > > > Any help would be greatly appreciated. > > > David > > > Public Function getKeyValueList(ByVal sql As String) As Collection > > Dim coll As Collection = New Collection > > readerConn.Open() > > readerCmd.Connection = readerConn > > readerCmd.CommandText = sql > > reader = readerCmd.ExecuteReader > > If reader.HasRows Then > > Do While reader.Read() > > coll.Add(reader.GetString(0), reader.GetString(1)) > > Loop > > End If > > reader.Close() > > readerConn.Close() > > End Function > > >
- Next message: Carlos J. Quintero [.NET MVP]: "Re: Using VS 2003 with DotNetFrameWork 2.0"
- Previous message: Mujtaba Syed: "Re: Determining .NET Install Directories"
- In reply to: pantichd: "Looking for suggestions on populating dropdown from database table"
- Next in thread: pantichd: "Re: Looking for suggestions on populating dropdown from database table"
- Reply: pantichd: "Re: Looking for suggestions on populating dropdown from database table"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|