Filling Dropdown box using Code-Behinds in C#

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 30
January 01, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Code-Behinds in C#

This simple example shows how to use code-behinds in ASP.NET.   The following code fills a dropdown box selecting data from the pubs database.  This is an example method of separating presentation from business logic.

Here is the code:

The webpage.

<%@ Page Language="C#" Debug="False" Inherits="myCodeBehind" src="2.cs"%>
<html>
<head><title>Fill dropdown with C# and Sql DataReader</title></head>
       <body>
           <asp:Dropdownlist id="atMyDropDownList" DataValueField = "au_lname" DataTextField = "au_id" runat="server" />
       </body>
</html>

The Code-behind page

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class myCodeBehind : System.Web.UI.Page
{
public DropDownList atMyDropDownList;

    private void Page_Load(object sender, System.EventArgs e)
    {
        string sqlText = "select * from authors";
        atMyDropDownList.DataSource = GetDr( sqlText );
        atMyDropDownList.DataBind();
    }
    private SqlDataReader GetDr( string sqlText)
    {
        SqlDataReader dr;
        SqlConnection sqlConn = new SqlConnection(ConnectionString());
        SqlCommand sqlCmd = new SqlCommand(sqlText,sqlConn);
        sqlCmd.Connection.Open();
        dr = sqlCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
        return dr;
    }
    private string ConnectionString()
        {
            return ConfigurationSettings.AppSettings["DSN_pubs"];
        }

    private void Page_Init( object Sender, System.EventArgs e)
    {
                   
    }//end Page_Init

} //End Class

Web.Config holding application settings

<configuration>
<appSettings>
    <add key="DSN_Northwind" value="server=(local)NetSDK;database=Northwind;Trusted_Connection=yes" />
    <add key="DSN_pubs" value="server=(local)NetSDK;database=pubs;Trusted_Connection=yes" />
</appSettings>
</configuration>
blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials