Friday, March 16, 2012

Populating a list box

Hi, I'm new to asp.net and would like to have a few questions answered regarding list box.

1) When one populates the list box with data from a field in a database table, where exactly are the data store?

2) How are the list items of the list box linked to the data that were used to populate the list box?

Suppose I have a list box named Singers Names which has the names of several famous singers and the corresponding images of these singers are stored as blobs in a field called Singers' Pics in a database table. According to a tutorial on reading and display blobs, if I wanted to display the images stored as blobs, I would have to populate the list box with the IDs of the images from the database. What I'd like to know are where are the IDs stored once the list box is populated with them and how are they related to the list items. Are IDs and the list items linked in any way at this point? I hope I am making sense please explain or point me to a good tutorial. Thanks for your help.

everything on client side is just PLAIN TEXT interpreted and rendered by client browser.


Are you are looking for this?

string cnStr = @."Persist Security Info=False;User ID=id;password=pass; Initial Catalog=AllTest;Data Source=datasource";string qry ="Select * from yourTable";SqlConnection con =new SqlConnection(cnStr);con.Open();DataSet ds =new DataSet();SqlDataAdapter da =new SqlDataAdapter(qry, con);da.Fill(ds);ListBox1.DataSource = ds;ListBox1.DataValueField ="SingerID"; //this will associate the ID to the items in the listboxListBox1.DataTextField ="Singer"; //this will display the names of singersListBox1.DataBind();

if you do a view source after running the project you will observe something as below:

<select size="4" name="ListBox1" onchange="javascript:setTimeout('__doPostBack(\'ListBox1\',\'\')', 0)" id="ListBox1">
<option value="1">singer 1</option>
<option value="2">singer 2</option>
<option value="3">singer 3</option>
<option value="4">singer 4</option>

</select>


What I'd like to know are where are the IDs stored once the list box is populated with them and how are they related to the list items.

You may need some guild on Data Access in ado.net.The id and the pictures are all stored in the database. We can get the correspondig picture by passing a specific in the sql-query clause (if we are using some control like a data grid, we can even list all the IDs with its corresponding pictures). Take a look at following links, they should help

http://www.w3schools.com/aspnet/aspnet_dbconnection.asp

http://msdn2.microsoft.com/en-us/data/aa937699.aspx

Hope my suggestion helps

This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

0 comments:

Post a Comment