Saturday, 13 May 2017

How to bind drop down list through database in ASP.Net


1. DDLWebForm.aspx File

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DDLWebForm.aspx.cs" Inherits="DemoClassExample.DDLWebForm" %>

<!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%;
        }
        .auto-style1 {
            height: 30px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <table bgcolor="#CCFF99" class="style1">
            <tr>
                <td style="font-family: Georgia; font-size: small; font-weight: bold; font-style: normal; font-variant: normal">
                    UserName</td>
                <td>
                    <asp:DropDownList ID="ddlName" runat="server" Font-Bold="True"
                        AutoPostBack="True" onselectedindexchanged="ddlName_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">
                    <asp:Label ID="lblMsg" runat="server" ForeColor="Red" Text="Message"
                        Font-Bold="True"></asp:Label>
                </td>
                <td class="auto-style1">
                    </td>
                <td align="center" class="auto-style1">
                    <asp:Button ID="btnSave" runat="server" BackColor="#FFCCFF" Font-Bold="True"
                        ForeColor="Maroon" Text="Save" onclick="btnSave_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
 
    </div>
    </form>
</body>
</html>

2. DDLWebForm.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;
using System.Data.SqlClient;

namespace DemoClassExample
{
    public partial class DDLWebForm : System.Web.UI.Page
    {
        string StrCon = "Server=PRADEEP-HP;Database=Decofloore2;User Id=User_006;password=user006";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindContrydropdown();
            }
        }
        /// <summary>
        /// Bind COuntrydropdown
        /// </summary>
        protected void BindContrydropdown()
        {
            SqlConnection SqlCon = new SqlConnection(StrCon);
            SqlCon.Open();
            string SqlQuery = null;
            SqlQuery = "Select RegistrationId,Name from ShowOnePage";
            SqlCommand SqlComm = new SqlCommand(SqlQuery, SqlCon);
            //SqlDataAdapter da = new SqlDataAdapter(SqlComm);
            //DataSet ds = new DataSet();
            //da.Fill(ds);
            SqlDataReader dr;
            dr = SqlComm.ExecuteReader();
            ddlName.DataSource = dr;
            ddlName.DataTextField = "Name";
            ddlName.DataValueField = "RegistrationId";
            ddlName.DataBind();
            ddlName.Items.Insert(0, new ListItem("---Select---", "0"));
            //lblMsg.Text = "Your Name is " + dr["Name"].ToString();
            SqlCon.Close();
        }
       
        protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection SqlCon = new SqlConnection(StrCon);
            SqlCon.Open();
            string SqlQuery = null;
            SqlQuery = "Select RegistrationId,Name from ShowOnePage where Name='" + ddlName.SelectedItem.ToString() + "'";
            SqlCommand SqlComm = new SqlCommand(SqlQuery, SqlCon);
            SqlDataReader dr;
            dr = SqlComm.ExecuteReader();
            if (dr.Read())
            {
                lblMsg.Text = "Your Name is " + dr["Name"].ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

        }
    }
}

0 comments

Post a Comment