• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 680
  • Last Modified:

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.

Eg

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])">
   Else
    ProviderName="System.Data.SqlClient" SelectCommand="SELECT ApplicationFee FROM tblApplicationFees Where (@PurchasePrice  BETWEEN [Lower] AND [Higher])">

--%>  

Is this possible?
0
Ed
Asked:
Ed
  • 4
  • 3
1 Solution
 
VelioCommented:
yes, it's possible, are you unable to get it to work or are you just asking before you get round to doing it? :)
0
 
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.


Thanks
0
 
VelioCommented:
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
        Else
            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
            DataBind()
        End If
    End Sub
 
    Public Overrides Sub DataBind()
        If (GetFlag()) Then
            dataSource2.SelectCommand = "SELECT 1 AS [Value]"
        Else
            dataSource2.SelectCommand = "SELECT 0 AS [Value]"
        End If
 
        GridView1.DataSource = dataSource2
 
        MyBase.DataBind()
    End Sub
 
End Class

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

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

Open in new window

0
 
EdAuthor Commented:
velio

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])")

========================================================================================

0
 
VelioCommented:
dataSource1.SelectParameters.Add("SchemeID", schemeIDValue)

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

Thanks

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
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now