Friday, 28 February 2014

DropDownList Operation using ASP.NET(Example of State-City)

Take two DDL controls one is for State and another for City.While select particular State on One DDL automatically City would be selected on Second DDL.

Ans:-

In Default.aspx:-

<%@ 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>Select State and See City Operation</title>
</head>
<body>
    <form id="form1" runat="server">
   
    <div>
    <center>
    States :
    <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true"
            onselectedindexchanged="ddlState_SelectedIndexChanged">
    </asp:DropDownList>
    &nbsp;
    City :
    <asp:DropDownList ID="ddlCity" runat="server">
    </asp:DropDownList>
    </center>
   
    </div>
    </form>
</body>
</html>

In Default.aspx.cs:-

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;



public partial class _Default : System.Web.UI.Page
{
    static SqlConnection cn;
    static SqlCommand cmd;
    static SqlDataAdapter da;
    static string conn = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDDLstateData();
        }
   
    }
    void BindDDLstateData()
    {
        cn = new SqlConnection(conn);
        cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "spGetStateData";
        da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds,"state");

        ddlState.DataValueField = "sid";
        ddlState.DataTextField = "sname";
       
        ddlState.DataSource = ds;
        ddlState.DataBind();

        //ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
        //ddlCity.Items.Insert(0, new ListItem("--Select--", "0"));
        //OR

        ddlState.Items.Insert(0, "--Select--");
        ddlCity.Items.Insert(0, "--Select--"); 

    }
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        cn = new SqlConnection(conn);
        cn.Open();
        cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "spGetCityfromSelectingState";

        //cmd.Parameters.Add("@sid", SqlDbType.Int).Value = ddlState.SelectedValue.ToString();
        //OR

        cmd.Parameters.AddWithValue("@sid", SqlDbType.Int).Value = ddlState.SelectedValue.ToString() ?? "";

        da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "city");
        
        ddlCity.DataValueField = "cid";
        ddlCity.DataTextField = "cname";
       
         ddlCity.DataSource = ds;
         ddlCity.DataBind();

         ddlCity.Items.Insert(0, "--Select--");

        cn.Close();
    }
}

In web.config:-

<connectionStrings>
<add name="conStr" connectionString="server=.;database=subsdb;user id=sa;pwd=123"/>
</connectionStrings>

Create Table in Sql.Server:-

1.create table state(sid int constraint pr_key2 primary key,sname varchar(30))

2.create table city(cid int constraint pr_key3 primary key,cname varchar(30),sid int constraint fr_key foreign key references state(sid))

Insert Data in Table:-

1.insert into state(sid,sname)values(7,'gujurat')

2.insert into city(cid,cname,sid)values(106,'bangaluru',6)

Create Stored Procedures:-

1.create proc spGetStateData
as
begin
select sname,sid from state
end

2.create proc spGetCityfromSelectingState(@sid int)
as
begin
select cname from city where sid in(select sid from state where sid=@sid)
end

exec spGetCityfromSelectingState 1

No comments:

Post a Comment