Drop Down List Binding Data From MSSQL Database Asp.net,C#

Bangalore:Hi Friends today we are going to discuss about Dropdown Lists in Asp.Net web forms, This article for beginners those who just start coding in C#.
Requirements:
Development Tools: Visual Studio,Sql Server Management Studio
Create an Empty web application in Visual Studio and add a Web form to your project.
Add a Drop down box and Button inside your aspx form like this.

<form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="drpData" runat="server" Width="100px"></asp:DropDownList>
    <asp:Button id="btnShow" Text="Show" runat="server" onclick="btnShow_Click" />
    </div>
    </form>

Here In our Application on Page Load Itself Dropdown get filled and on show button click you can see which item you have selected as alert.
On Pageload itself we have called FilledDropDown Function so it will fill our dropdown box, we have called it inside (!Isostback) so the options which you have selected wont change on next page load. Here we have selected the id and firstname from the database and bind that data into our dropdown box. Id we are assigning into valuefield and Text we are assigning  to TextField
please go through the code behind c# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
namespace Drop
{
    public partial class Default : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=Tonz-PC; Integrated Security=true;  Initial Catalog=Search");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillDropDown();
            }
        }
        public void FillDropDown()
        {
            con.Open();
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand("Select id,firstname from users",con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                drpData.DataSource = dt;
                drpData.DataValueField=dt.Columns["id"].ToString();
                drpData.DataTextField = dt.Columns["firstname"].ToString();
                drpData.DataBind();
                drpData.Items.Insert(0, " ");
            }
            con.Close();
        }
        protected void btnShow_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this,GetType(),"showalert","alert('" + drpData.SelectedItem.Text +"');", true);
         
        }
    }
}

Hope that you got everything,write to us if you have any doubts.

Custom Calender Control in Asp.Net web page using Ajax

Create a Sample Webpage in Visual Studio, Download Ajax Toolkit  library from here. and add that dll into your reference and add this code in second line of you aspx page to add the Ajax control to Assembly.
<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>
Inside body of the page Inside form tag declare the toolkit script manger tag like given below.

<form id="form1" runat="server">
    <asp:ToolkitScriptManager runat="server" ID="toolscriptmgr"></asp:ToolkitScriptManager>
For Default Ajax Calender Create a TextBox and add the Calender Extender Tag Like this.

 <td nowrap="nowrap">Pic Date</td>
    <td><asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
    <asp:CalendarExtender ID="dpCalender" runat="server"  Format="yyyy/MM/dd" TargetControlID="txtDate" ></asp:CalendarExtender>
    </td>

For Calender Control with Date Picker Image add one ImageButton box also.

 <td>Pic Date
    </td>
    <td><asp:TextBox ID="txtDate1" runat="server" ></asp:TextBox>
    <asp:CalendarExtender ID="dpCalender1" runat="server" Format="yyyy/MM/dd" TargetControlID="txtDate1" PopupButtonID="calenderimage"></asp:CalendarExtender>
    <asp:ImageButton ID="calenderimage" runat="server" ImageUrl="~/images/cal.gif" ></asp:ImageButton>
    </td>

This will give you the custom calender controls.

Whole Code In Single Slot.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="calender.aspx.cs" Inherits="calender.calender" %>
<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ToolkitScriptManager runat="server" ID="toolscriptmgr"></asp:ToolkitScriptManager>
    <div>
    <table>
    <tr>
    <td nowrap="nowrap">Pic Date</td>
    <td><asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
    <asp:CalendarExtender ID="dpCalender" runat="server"  Format="yyyy/MM/dd" TargetControlID="txtDate" ></asp:CalendarExtender>
    </td>
    </tr>
    <tr>
    <td>Pic Date
    </td>
    <td><asp:TextBox ID="txtDate1" runat="server" ></asp:TextBox>
    <asp:CalendarExtender ID="dpCalender1" runat="server" Format="yyyy/MM/dd" TargetControlID="txtDate1" PopupButtonID="calenderimage"></asp:CalendarExtender>
    <asp:ImageButton ID="calenderimage" runat="server" ImageUrl="~/images/cal.gif" ></asp:ImageButton>
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>