Solved

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

Posted on 2012-03-23
2
269 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
[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 500 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:murbro
ID: 37756748
Thanks very much
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…

756 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