Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-23
2
Medium Priority
?
273 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:Murray Brown
2 Comments
 
LVL 16

Accepted Solution

by:
Rose Babu earned 2000 total points
ID: 37756625
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:Murray Brown
ID: 37756748
Thanks very much
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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