SIGN UP MEMBER LOGIN:    
ARTICLE

Searching Records by DropDownList in ASP.NET

Posted by Alok Pandey Articles | .NET 4.5 January 31, 2012
In this article you will learn how to show a record into a GridView according to a selected value in a dropdownlist.
Reader Level:

Introduction

I am creating a web application to search records using DropDownList. You will select a name from the DropDownList and it will show all the records on the basis of that selected name.
At first we should have some records. So we create a database and insert some records into the database table.

Create Database

CREATE
DATABASE EMP

USE
EMP

CREATE TABLE EMP_DETAIL
(

 E_ID INT PRIMARY KEY,
 E_NAME VARCHAR(30),
 E_AGE INT,
 E_CITY VARCHAR(30),
 E_DEPARTMENT VARCHAR(20)
 )
 
 INSERT INTO EMP_DETAIL VALUES(11,'ALOK KUMAR',24,'DELHI','IT')
 INSERT INTO EMP_DETAIL VALUES(12,'RAJESH TRIPATHI',22,'ALLAHABAD','SALES')
 INSERT INTO EMP_DETAIL VALUES(13,'SATISH KUMAR',23,'JHANSI','PRODUCT')
 INSERT INTO EMP_DETAIL VALUES(14,'MANOJ SINGH',22,'NOIDA','MARKETING')
 INSERT INTO EMP_DETAIL VALUES(15,'AMIT MAHESHWARI',25,'ALLIGARH','IT')
 INSERT INTO EMP_DETAIL VALUES(16,'DEEPAK DWIJ',24,'NOIDA','IT')

I am showing a screen shot of all records of an EMP_DETAIL table so that it can become easy to understand.

SELECT
* FROM EMP_DETAIL




Now, create an ASP.NET web application -> create a DropDownList control, one GridView control and two SqlDataSources. Your form will look like the following figure.



Now we specify a DataSouce for both SqlDataSources. Follow the given steps.

Step 1 : Click at smart tag of SqlDataSource1.



Step 2 : Click at Configure Data Source.



Step 3 : A new window will be opened to choose a data connection.



Step 4 : Click the New Connection button. A new window will be opened. Write the server name, user name, password and select your database. Look at the following figure.



Step 5 : Click the ok button. You will be presented the Configure DataSource wizard. Click the "Next" button. A new window will be opened and it will ask to save the connection string.



Step 6 : Select Database table and then choose column names by checking the checkboxes.



Step 7 : Then click the Next button, then Finish button.

Step 8 : Now it's time for SqlDataSource2. Repeat step 1 to step 6. At the 6th step choose all columns by checking all checkboxes.



Step 9 : Click the Where button. Select Column ( At which basis you want to show record. I have selected "E_ID" ) -> Select operator ( I have selected "=" ) -> Select source ( I have selected "Control" ) -> Select DropDownList1 ( Or ID of DropDownList ) for Control ID.



Step 10 : Click the Add button. Then click the ok button. After this you will reach the Configure Data Source wizard; click the Add button, then click the Finish button.

Step 11 : Select DropDownList and click at smart tab -> Click at "Choose Data Source". A new window will be opened. Choose Data Source, Select column to display and another column which will be the value of the DropDownList then click the ok button.



Step 12 : Select GridView and click at smart tag -> Choose Data Source - SqlDataSource2.



Step 13 : Select DropDownList and set its AutoPostBack property to "True".

Run the application.



Select another name from DropDownList. Like I am selecting a employee name "Rajesh Tripathi".


It will show all records of that name.
 

Here are some related resources.

Searching Records in ADO.NET

Searching Records using RowFilter in F#

Executing DML commands in F#

DataView Class in FSharp

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks, Aaron.

Posted by Alok Pandey Feb 01, 2012

Good Work...

Posted by Aaron Cronje Feb 01, 2012
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor