Thursday, 4 October 2012

How To Bind Dropdownlist from Database in asp.net

Please follow these steps to bind the dropdown list in asp.net
1. Open Visual Studio 2010
2.New Project
3.Asp.net Web Application

Design the Default page like this:-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 89px;
        }
        .style3
        {
            width: 89px;
            height: 41px;
        }
        .style4
        {
            height: 41px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    <asp:Label ID="Label1" runat="server" Text="Please Select"></asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" Height="22px" Width="152px">
                    </asp:DropDownList>
                &nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
                        Text="Button" />
                </td>
              
            </tr>
            <tr>
                <td class="style3">
                    </td>
                <td class="style4">
                    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                </td>
                <td class="style4">
                    </td>
                          </tr>
        </table>
   
    </div>
    </form>
</body>
</html>








After that Code for the Default.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string connectionstring = "Data Source=PANKAJ-PC\\SQLEXPRESS2008;Initial Catalog=Data;User ID=sa;Password=sa2008";
            SqlConnection con = new SqlConnection(connectionstring);
            string com1="select * from pankaj";
            SqlDataAdapter ad = new SqlDataAdapter(com1,con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
            GridView1.DataSource = dt;
            GridView1.DataBind();
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "Pid";
            DropDownList1.DataBind();
            Label2.Visible = false;
          


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string connectionstring = "Data Source=PANKAJ-PC\\SQLEXPRESS2008;Initial Catalog=Data;User ID=sa;Password=sa2008";
            SqlConnection conn = new SqlConnection(connectionstring);
            SqlCommand com = new SqlCommand("select * from pankaj where pid='"+DropDownList1.SelectedValue+"'",conn);
            SqlDataAdapter dt1 = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            dt1.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            Label2.Text = "Record Found According to Selection";
            Label2.Visible = true;
        }

      
    }
}
 

Output will look like this..............................................



No comments:

Post a Comment