Tuesday, 16 December 2014

How to bind data by Country ,State and District wise from a single table Using asp.net in C#:-



Hello Friends,

              Now I am going to describe  how to bind  data  by Country ,State and District wise from  a single table in C# Using asp.net 

By  a Single  table.

Step-1:
First create a Table
Go to Server Explore ->Rightclick on Table option ->Add New Table->create Table Structure like this:-
  




Step-2:
After create Table  data should be insert  by matching of  Id and refid  like following  structure.
Now  we insert data in the table to compair id and refid   like this :
Id
refid
iname
1
0
India
2
1
Odisha
3
1
Karnataka 
4
2
Bhadrak
5
2
Balasore
6
3
Bagalkote
7
3
Belgaum
8
0
Aus 
9
8
Aus1
10
8
Aus2
11
9
Aus1.1
12
9
Aus1.2
13
10
14
10



 Step-3:
Design  frontend  like this ,copy to this code and paste in Default.aspx page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 22%;
            border: 1px solid #FF0000;
        }
        .auto-style4 {
            width: 86px;
        }
        .auto-style5 {
            width: 92%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table class="auto-style1">
            <tr>
                <td colspan="2" style="color: #FF0000; font-size: medium; font-weight: bold">Bind Data Country,State,District Wise</td>
            </tr>
            <tr>
                <td class="auto-style4" style="color: #FF00FF; font-weight: bold">CountryName</td>
                <td class="auto-style5">
                    <asp:DropDownList ID="ddlct" runat="server" OnSelectedIndexChanged="ddlct_SelectedIndexChanged1" AutoPostBack="true">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="auto-style4" style="color: #FF00FF; font-weight: bold">StateName</td>
                <td class="auto-style5">
                    <asp:DropDownList ID="ddlst" runat="server" OnSelectedIndexChanged="ddlst_SelectedIndexChanged" AutoPostBack-="true">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="auto-style4" style="color: #FF00FF; font-weight: bold">DistrictName</td>
                <td class="auto-style5">
                    <asp:DropDownList ID="ddldt" runat="server" OnSelectedIndexChanged="ddldt_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
            </tr>
            </table>
    </form>
</body>
</html>
Step-4:
Now we insert those namespace in top of the 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;
using System.Configuration;
Step-5:
Write this code in pageload:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            countrybind();
    }
Step-6:
Write this Code to Create Bind function which name is countrybind() :-

public void countrybind()
    {
        try
        {
            string sql = "select id,refid,iname from test where refid = 0";

            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            ddlct.DataSource = ds;
            ddlct.DataTextField = "iname";
            ddlct.DataValueField = "id";
            ddlct.DataBind();
            ddlct.Items.Insert(0, new ListItem("--select--", "0"));
            ddlst.Items.Insert(0, new ListItem("--select--", "0"));
            ddldt.Items.Insert(0, new ListItem("--select--", "0"));
            if (ddlct.SelectedValue == "0")
            {
                ddlst.Items.Clear();
                ddldt.Items.Clear();
                ddlst.Items.Insert(0, new ListItem("--select--", "0"));
                ddldt.Items.Insert(0, new ListItem("--select--", "0"));
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
            finally
            {
                con.Dispose();
                GC.Collect();
            }
        }

Step-7:
Doubleclick on Country  dropdownlist and write this code:-
protected void ddlct_SelectedIndexChanged1(object sender, EventArgs e)
    {
        if (ddlct.SelectedItem.Text != "--select--")
        {
            try
            {
                string sql = "select id, iname from test where refid = " + ddlct.SelectedValue.ToString();
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adp.Fill(dt);
                ddlst.DataSource = dt;
                ddlst.DataTextField = "iname";
                ddlst.DataValueField = "id";
                ddlst.DataBind();
                ddlst.Enabled = true;
                ddldt.Enabled = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Dispose();
                GC.Collect();
            }
        }
        else
        {
            ddlst.SelectedItem.Text = "--select--";
            ddlst.Enabled = false;
            ddldt.SelectedItem.Text = "--select--";
            ddldt.Enabled = false;

        }

    }
Step-8:
Double Click on State Dropdownlist and write this code:
try
        {
        SqlCommand cmd = new SqlCommand("select id,iname from test where refid = " + ddlst.SelectedValue.ToString(), con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        ddldt.DataSource = dt;
        ddldt.DataTextField = "iname";
        ddldt.DataValueField = "id";
        ddldt.DataBind();
    }
         catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Dispose();
                GC.Collect();
            }
    }


Thanks
Rabi

No comments:

Post a Comment