Solved

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

Posted on 2012-03-23
2
267 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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