<%@ 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">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Height="28px" Width="168px"></asp:DropDownList>
<asp:Label ID="Label1" runat="server" Visible="false"></asp:Label>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"/>
<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
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()
{
try
{
using(SqlConnection con = new SqlConnection(cs))
{
con.Open();
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);
}
}
catch (SqlException ex)
{
Response.Write("Exception: " + ex.Message);
}
catch (Exception ex)
{
Response.Write("Exception: " + ex.Message);
}
}
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);
SqlConnection con = new SqlConnection(cs);
con.Open();
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
<?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=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<connectionStrings>
<add name="dbcs" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\DotNet\PopulateDataInDropDown\PopulateDataInDropDown\App_Data\EmployeeDB.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Above File is Web.config
CREATE TABLE [dbo].[employee]
(
[Id] INT NOT NULL PRIMARY KEY identity,
[name] VARCHAR(50) NOT NULL,
[gender] VARCHAR(50) NOT NULL,
[age] INT NOT NULL,
[salary] INT NOT NULL
)
INSERT INTO employee(name,gender,age,salary) VALUES('Arjun','Male',23,240000),
('Aman','Male',24,260000),
('Umesh','Male',22,210000),
('Rashmi','Female',26,290000),
('Uday','Male',21,200000),
('Naman','Male',25,340000),
('Yatrik','Male',27,350000),
('Ajay','Male',22,150000);
Comments
Post a Comment