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

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

1 Solution
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 btnFind.click
           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.
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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