Solved

ASP.net SqlDataSource - use different query based on selection in DropDownList

Posted on 2012-03-23
2
264 Views
Last Modified: 2012-08-14
Hi

I have a GridView on an ASP.net WebForm. It currently has a SqlDataSource that I set up very quickly. I want the user to be able to choose between two possibilities in a DropDownList and load the GridView with a different SQL query from the same data source.
What is the best way to do this? Should I use two SQLDataSources?
0
Comment
Question by:murbro
2 Comments
 
LVL 16

Accepted Solution

by:
Rose Babu earned 500 total points
Comment Utility
You can do that by resetting SelectCommand ( SqlDataSource1.SelectCommand = "SQL-query").

E.g.,
Protected Sub btnBindGrid_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBindGrid.Click
	'SqlDataSource1.SelectCommand = "Select * from Table1"
	
	If DropDownList1.Text = "sample1" Then
		SqlDataSource1.SelectCommand = "Select field1, field2, field3, field4 from Table1"
	End If

	If DropDownList1.Text = "sample2" Then
		SqlDataSource1.SelectCommand = "Select field1, field2, field3 from Table1"
	End If

	If DropDownList1.Text = "sample3" Then
		SqlDataSource1.SelectCommand = "Select field1, field2 from Table1"
	End If
End Sub

Open in new window

Try like this. It may work for you.
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks very much
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now