Inline code to switch between several SqlDataSource objects for one GridView

Hi everybody,

Though I'm new to ASP.NET, I'm creative ;)...
Can anybody tell me if is possible to write inline code to switch between several
SqlDataSources at run time, like in the attached code sample?

Thanks a lot!
<asp:DropDownList ID="ddlSearchParam" runat="server" Width="164px" AutoPostBack="false">
            <asp:ListItem>---Please select----</asp:ListItem>
            <asp:ListItem>Last name</asp:ListItem>
            <asp:ListItem>First name</asp:ListItem>
        <asp:TextBox ID="txtSearchParam" runat="server"  Enabled="False" Width="263px"></asp:TextBox>
        <asp:Button ID="btnFind" runat="server" Text="Find" /><br />
        <br />
        <%  Dim varSqlDataSource As New SqlDataSource '?Not sure here, what type should the variable be?'
            Select Case ddlSearchParam.SelectedValue
                Case "Last name"
                    varSqlDataSource = SqlDSLastName
                Case "First name"
                    varSqlDataSource = SqlDSFirstName
                Case "Section"
                    varSqlDataSource = SqlDSSection
            End Select
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
            DataSourceID="<% varSqlDataSource%>" Width="994px">
            'also on this line, how can I use the previously declared variable?'

Open in new window

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.

I don't think you could do it inline like that, but is there a reason why you'd want to? ASP.NET has a wonderful event driven programming model which is more elegant for situations such as this.

In your code behind (or in your code section if you are putting it all on one page) assuming you have the sqldatasource objects on there already:

Sub btnFind_Click(sender as object, e as eventargs) handles
           Select Case ddlSearchParam.SelectedValue
                Case "Last name"
                     Gridview1.DataSource = SrcLastName (or whatever you called it)
                Case "First name"
                     Gridview1.DataSource = SrcFirstName
            End Select
End Sub

This will execute the code when the user presses the button, hook the gridview up to the appropriate datasource and bind the data. It also keeps your logic and presentation code separate which makes things much easier in the long run and something I recommend you do as much as possible.

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
apopoviciAuthor Commented:
Thanks NazoUK,

I tried this solution in the past.
Trouble is that I found out you must define a sqlDataSource for the GridView at design time,
that conflicts with the code you have at run time...

If you have a  DataSourceID ="SqlServer1" for instance defined in for the GridView1  and you use
the code you suggested for, say SqlDataSource1 and SqlDataSource2, at run time you will get an error,
see image attached.
See also relevant portions from code.

Though I knew it would happen like this, I gave you the points and closed the question, since there was no other comment and it's been taking too long...Still the answer is not this, probably because I need to do something in addition to
that, which I don't know...

If you have an additional comment for this, it will be welcomed.
Thanks again.


Protected Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Dim strParamSearch As String = ""
        Select Case ddlSearchOption.SelectedValue
            Case "City"
                GridView1.DataSource = SqlDataSource1
            Case "Country"
                GridView1.DataSource = SqlDataSource2
        End Select
    End Sub
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" Width="994px">

Open in new window

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

From novice to tech pro — start learning today.