An error has occurred: System.Data.SqlClient.SqlException: SQL Server does not exist or access denied. at System.Data.SqlClient.ConnectionPool.CreateConnection() at System.Data.SqlClient.ConnectionPool.UserCreateRequest() at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction) at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction) at System.Data.SqlClient.SqlConnection.Open() at ASP.dynamicdatasource_aspx.BindList() ASPAlliance.com : The #1 ASP.NET Developer Community : ASP.NET: Dynamically set Text and Value fields for a DropDownList
ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
XML for ASP.NET Developers
XML for ASP.NET Developers

Find Prices
Read Review
Read Review
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software
Click here to return to my article index

ASP.NET: Dynamically set Text and Value fields for a DropDownList

This code was written in response to a message posted on one of the ASPAlliance lists. You can sign up for one or all of the lists here.

In this article, we will look at a technique that allows us to switch the value and text represenation of the items in a DropDownList. I know, it's probably not something you will see in many applications but it is yet another example of the power of ASP.NET.

The code sample below uses a few best pratices you should understand and be aware of:
*The code below uses structured error handling techniques in the form of the Try...Catch statement. Go here to read more about the great new structured error handling we VBers now have.
*The code below also uses a connection string stored in a web.config configuration file. Go here to read more about the web.config configuration file and how it can be used to store application wide data.
*The code below also follows proper database resouce management per this best practice.

Code:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
    Sub Page_Load(sender As Object, e As EventArgs) 
	If Not IsPostBack Then
	   DropDownList1.DataTextField = "pub_name"
	   DropDownList1.DataValueField = "pub_id"
	   BindList()
        End If
    End Sub

    Sub ButtonClick(sender As Object, e As EventArgs) 
	If sender.ID = "Button1" Then
	   DropDownList1.DataTextField = "pub_id"
	   DropDownList1.DataValueField = "pub_name"
	Else
	   DropDownList1.DataTextField = "pub_name"
	   DropDownList1.DataValueField = "pub_id"
	End If

	BindList()

	Button1.Visible = "False"
	Button2.Visible = "True"
    End Sub

    Sub BindList()
	Dim myConnection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))

	Dim myCommand As SqlCommand = new SqlCommand("Select pub_id, pub_name From publishers", myConnection)
	
	Dim myDataReader As SqlDataReader

	Try
	   myConnection.Open()

	   myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

	   DropDownList1.DataSource = myDataReader
	   DropDownList1.DataBind()
	Catch myException As Exception
	   Response.Write("An error has occurred: " & myException.ToString())
	Finally
	   If Not myDataReader Is Nothing Then
	       myDataReader.Close()
	   End If
		
   	   DropDownList1.Items.Insert(0, "Select an Item")
	   DropDownList1.SelectedIndex = 0
	End Try
    End Sub

</script>
<body>
<form runat="Server">
<asp:DropDownList id="DropDownList1" runat="server" />
<asp:Button id="Button1" Text="Switch Em" OnClick="ButtonClick" runat="server" />
<asp:Button id="Button2" Text="Switch Em Back" OnClick="ButtonClick" Visible="False" runat="server" />
</form>
</body>
</html>

Result: