Display Data In GridView By Using DropDownList In ASP.NET Web Forms

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Configuration;

using System.Data;

using System.Drawing;


namespace PopulateDataInDropDown

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                BindDropDownList();

            }

        }


        void BindDropDownList()

        {

            SqlConnection con = new SqlConnection(cs);

            string query = "select * from employee";

            SqlDataAdapter sda = new SqlDataAdapter(query,con);

            DataTable data = new DataTable();

            sda.Fill(data);

            DropDownList1.DataSource = data;

            DropDownList1.DataTextField = "name";

            DropDownList1.DataValueField = "id";

            DropDownList1.DataBind();


            ListItem SelectItem = new ListItem("Select Employee","-1");

            SelectItem.Selected = true;

            DropDownList1.Items.Insert(0,SelectItem);

        }


        protected void Button1_Click(object sender, EventArgs e)

        {

            if (DropDownList1.SelectedValue == "-1")

            {

                Response.Write("Please select an employee");

            }

            else

            {

                //Response.Write("Selected Item Text is : " + DropDownList1.SelectedItem.Text + "<br>");

                //Response.Write("Selected Item Value is : " + DropDownList1.SelectedItem.Value + "<br>");

                //Response.Write("Selected Item Index is : " + DropDownList1.SelectedIndex + "<br>");



                SqlConnection con = new SqlConnection(cs);

                string query = "select * from employee where name = @name";

                SqlDataAdapter sda = new SqlDataAdapter(query, con);

                sda.SelectCommand.Parameters.AddWithValue("@name",DropDownList1.SelectedItem.Text);

                DataTable data = new DataTable();

                sda.Fill(data);

                GridView1.DataSource = data;

                GridView1.DataBind();

                Label1.Text = "Rows Found";

                Label1.ForeColor = Color.Green;

                Label1.Visible = true;

            }

        }

    }

}

Above File is PopulateDataInDropDown\WebForm1.aspx.cs





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


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <asp:DropDownList ID="DropDownList1" runat="server" Height="22px" Width="186px">

        </asp:DropDownList>

        <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>

        <div>

            <br />

            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />

            <br />

            <br />

            <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">

                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                <EditRowStyle BackColor="#999999" />

                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

                <SortedAscendingCellStyle BackColor="#E9E7E2" />

                <SortedAscendingHeaderStyle BackColor="#506C8C" />

                <SortedDescendingCellStyle BackColor="#FFFDF8" />

                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

            </asp:GridView>

        </div>

    </form>

</body>

</html>

Above File is PopulateDataInDropDown\WebForm1.aspx








<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization" />
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
<connectionStrings>
<add name="dbcs" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\DotNet_Programmes\PopulateDataInDropDown\PopulateDataInDropDown\App_Data\EmployeeDB.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Above File is PopulateDataInDropDown\Web.config















Comments

Popular posts from this blog

Range Validator Control In ASP.NET Web forms | Form Validation | ASP.NET Web forms

Query String In ASP.NET Web Forms | State Management | ASP.NET | Web Forms

Validation Summary Control In ASP.NET Web forms | Form Validation | ASP.NET