Using the ASP.NET 2.0 ListBox Control - Using the SqlDataSource control with the ListBox control
(Page 5 of 5 )
Click on Choose Data Source from the task menu of the ListBox control. The Data Source Configuration Wizard window will be displayed. From the DropDownList "Select a Data Source" choose <New Data Source> and you will be taken to the next screen shot where you can select the data source to which you are connecting.
Select database and note that the SqlDataSource1 is given as an ID for the SqlDataSource control. When you connect to a database the SqlDataSource control is used and this is its id.

Click on OK and you will be taken to the wizard where you will configure your newly added SqlDataSource control. Now you can select a Connection that the SqlDataSource control will use to communicate with the database. We didn't add any connections so click on New Connection button and you will get the following screen shot:

Choose Microsoft SQL Server as your Data Source, then click Continue. Select your Server name (if you are connecting to your local server then you can type (local) as shown in the next screen shot) and Pubs as the database, and then click on Test Connection. If you have entered correct information you will get the following screen shot:

Now that the connection is good and working click on OK, and then click OK again on the Add Connection dialog. You will be taken back to the SqlDataSource configuration wizard. The connection has been set so click Next.

Now you are asked if you want to store the Connection String to the application's Configuration file; leave the check box checked because we want this behavior. Click on Next again; after the connection we have the command. Now you can select the fields you want to retrieve from the database. We need to write a custom T-SQL Query for this example so click on Specify a Custom SQL Statement and write the following query: SELECT au_id, au_lname + ' '+ au_fname AS full_name FROM authors

Click Next and you can test the query if you want to by clicking on Test Query. Now click on the Finish button. You will be taken back to the first wizard you started with. Now that we have a data source set up, by using the SqlDataSource control, we can select which field will be used as the ListItem.Text and which will be used as ListItem.Value. Of course we will select au_id for the Value and full_name for the Text as shown in the next screenshot.

Click on OK then run the page and you will get those values from the database table to the ListBox control without writing a single line of code. Please consult my articles about ADO.NET and SqlDataSource for more information and examples about using the ListBox control, creating database connections and executing T-SQL statements against a database table.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |