Monday, April 7, 2014

Fetch data from database and insert it into DropDownList using 3 tier structure Asp.NET Part-1

Create a from in Visual Studio 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>Fetch items from database in DropDownList</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 style="width:22%;" align="center">
            <tr>
                <td class="style2">
                    Country :</td>
                <td class="style3">
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    State :</td>
                <td class="style4">
                    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
                    </asp:DropDownList>
                </td>
            </tr>
        </table>
 
    </div>
    </form>
</body>
</html>

Create Two Table in DataBase as follows :-


1) Country :-


2) State :-



Add details in the table whatever you want to add.

Now add two class :-


1) DataLayer.cs :-


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

/// <summary>
/// Summary description for DataLayer
/// </summary>
public class DataLayer
{
    SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;

public DataLayer()
{
        cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\C#\WebSites\DropDownList DataBase 1\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
}

    public DataSet get_country()
    {
        da = new SqlDataAdapter("Select* from Country", cn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }

    public DataSet get_state(LogicLayer li)
    {
        da = new SqlDataAdapter("Select* from State where c_id=@id", cn);
        da.SelectCommand.Parameters.AddWithValue("@id", li.country_id);

        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
}

2) LogicLayer.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for LogicLayer
/// </summary>
public class LogicLayer
{
    public string country_id { get; set; }
public LogicLayer()
{
//
// TODO: Add constructor logic here
//
}
}

Now add following code in the Default.aspx.cs :-


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

public partial class _Default : System.Web.UI.Page
{
    LogicLayer li = new LogicLayer();
    DataLayer dl = new DataLayer();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = dl.get_country();
            DropDownList1.DataTextField = "c_name";
            DropDownList1.DataValueField = "c_id";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "---Select---");
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        li.country_id = DropDownList1.SelectedValue;

        DropDownList2.DataSource = dl.get_state(li);
        DropDownList2.DataTextField = "s_name";
        DropDownList2.DataValueField = "s_id";
        DropDownList2.DataBind();
    }
}

Output :-


1) 

2) 

No comments:

Post a Comment