Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-23
2
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

688 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