SQl datasource Query - ASP.NET

I want to be abvle to change the SQL query in a SelectCommand depending on a value returned from another datasource.


If a condition is true from another sql query

If flag = true then
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT ApplicationFee FROM tblApplicationFees" + LenderID + "Where (@PurchasePrice  BETWEEN [Lower] AND [Higher])">
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT ApplicationFee FROM tblApplicationFees Where (@PurchasePrice  BETWEEN [Lower] AND [Higher])">


Is this possible?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yes, it's possible, are you unable to get it to work or are you just asking before you get round to doing it? :)
EdAuthor Commented:

No can't get it to work and wondering if it is possible before attempting to work out how to do it.

Any help is greatly appreciated.

ok, start a new website project and pop a GridView on the Default.aspx page.
below is the code-behind.

basically it's very simple, the GetFlag() function basically checks the data returned by dataSource1 for a condition (in this case if the 'Value' field of row 0 is equal to 1)

that is then used to set the Select command of dataSource2 and that is then bound to the gridview. hope it helps.
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim dataSource1 As SqlDataSource = New SqlDataSource(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString, "SELECT 0 AS [Value]")
    Dim dataSource2 As SqlDataSource = New SqlDataSource(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString, "SELECT 1 AS [Value]")
    Private Function GetFlag()
        Dim selectArguments As DataSourceSelectArguments = New DataSourceSelectArguments()
        Dim dataView As DataView = dataSource1.Select(selectArguments)
        If (Convert.ToInt32(dataView.Table.Rows(0)("Value")) = 1) Then
            Return True
            Return False
        End If
    End Function
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
        End If
    End Sub
    Public Overrides Sub DataBind()
        If (GetFlag()) Then
            dataSource2.SelectCommand = "SELECT 1 AS [Value]"
            dataSource2.SelectCommand = "SELECT 0 AS [Value]"
        End If
        GridView1.DataSource = dataSource2
    End Sub
End Class

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

also you'll have to add a valid connection string called "myConnectionString" in your web.config file, ie:

    <add name="myConnectionString" connectionString ="server=localhost\sql2005;database=ABCBooks;user=sa;pwd=sqladmin"/>

Open in new window

EdAuthor Commented:

Many thanks for this, it looks just what I need so the point are yours for this one.

Just wondering if you could help with one other thing

I'm getting a sqlexeption now saying I must ' declare the scalar variable "@schemeID"   '

@SchemeID is a session variable I have set.

How and where do I declare the scalar variable ?

I'm using the below statements.

    Dim dataSource1 As SqlDataSource = New SqlDataSource(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString, "SELECT UseLenderVal FROM [vwSchemes] WHERE ([SchemeID] = @SchemeID")
    Dim dataSource2 As SqlDataSource = New SqlDataSource(ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString, "SELECT ApplicationFee FROM tblApplicationFees Where (@PurchasePrice  BETWEEN [Lower] AND [Higher])")


dataSource1.SelectParameters.Add("SchemeID", schemeIDValue)

where schemeIDValue is a variable of type string.
EdAuthor Commented:


I still can't get this to work though....

dataSource1.SelectParameters.Add("SchemeID", schemeIDValue)

where schemeIDValue is a variable of type string.

could you show me the full statement please
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.