Monday, April 7, 2014

Fetch data from database and insert it into DropDownList Asp.NET Part-2

First we need to create 3 Tables in SQL Server Database.


1) Country Table :

2) State Table :

3) Region Table :

Now fill data in the tables as follows :-


1) Country Table :

2) State Table :

3) Region Table :

Your Default.aspx page :-


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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: 93px;
        }
        .style2
        {
            width: 93px;
            height: 26px;
        }
        .style3
        {
            height: 26px;
            width: 96px;
        }
        .style4
        {
            width: 96px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table align="center">
        <tr>
            <td>
                Country:
            </td>
            <td>
                <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"
                    onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                State:
            </td>
            <td>
                <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true"
                    onselectedindexchanged="ddlState_SelectedIndexChanged"></asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                Region:
            </td>
            <td>
                <asp:DropDownList ID="ddlRegion" runat="server"></asp:DropDownList>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

Your Default.aspx.cs Page :-


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

public partial class _Default : System.Web.UI.Page
{
    SqlConnection cn;
    protected void Page_Load(object sender, EventArgs e)
    {
        cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\C#\WebSites\DropDownList DataBase 2\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        if (!IsPostBack)
        {
            BindContrydropdown();
        }
    }

    protected void BindContrydropdown()
    {
        //conenction path for database
        cn.Open();
        SqlCommand cmd = new SqlCommand("select * from Country", cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cn.Close();
        ddlCountry.DataSource = ds;
        ddlCountry.DataTextField = "Cname";
        ddlCountry.DataValueField = "Cid";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
        ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
        ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));

    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
        cn.Open();
        SqlCommand cmd = new SqlCommand("select * from State where Cid=" + CountryID, cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cn.Close();
        ddlState.DataSource = ds;
        ddlState.DataTextField = "Sname";
        ddlState.DataValueField = "Sid";
        ddlState.DataBind();
        ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
        if (ddlState.SelectedValue == "0")
        {
            ddlRegion.Items.Clear();
            ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
        }

    }

    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        int StateID = Convert.ToInt32(ddlState.SelectedValue);
        cn.Open();
        SqlCommand cmd = new SqlCommand("select * from Region where Sid=" + StateID, cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cn.Close();
        ddlRegion.DataSource = ds;
        ddlRegion.DataTextField = "Name";
        ddlRegion.DataValueField = "Rid";
        ddlRegion.DataBind();
        ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
    }
}

Output:-





No comments:

Post a Comment