dynamically changing the datasource in codebehind file

Posted on 2014-08-01
Last Modified: 2014-08-19
I am using and C#. I am familiar with gridviews and datasources. However, i cannot figure out a way, using these controls, to dynamically filter results in the code behind from the sqldatasourse I have set up.

So if the query string has user="all", i want it populate the data grid with all records. If user equals a certain name, it will only show records associated wit the user.

I thought about making multiple datasources and swapping them in and out based on what comes in from the query string, but it doesn't work. The table I am getting data from is simple. It has two columns, filename and user. I want the data grid to only show the filename field, and only show what is dictated by the user found in the query string.

Also, very important. I need to wrap every result line in a hyperlink, that will use the filename data to build the final part of the url of the link
Question by:BobHavertyComh
    LVL 18

    Accepted Solution

    You should use parameters instead of multiple datasources. If you use multiple datasources, it will be real messy. Parameters were made for this sort of thing.

    This way you can return the results that you need.
    LVL 9

    Author Comment

    Hi Jerry, it took some reading for me, as I'm not very experienced, but I figured it out and it works, and the best part is that it doesn't bomb without query string values, it does the right thing and merely says no values to display. The problem I still have that I mentioned above, is that I need a way to wrap each of these filename values with a hyperlink or a button to be clicked that is associated with each returned record. This is a list of video files, so I plan to use a javascript click event with each of the results, that will swap out the url for the video tag in html5 to the appropriate video.
    LVL 18

    Expert Comment

    by:Jerry Miller
    In your RowDataBound event, you can do something like this;

    if (e.Row.RowType == DataControlRowType.DataRow) {
          HyperLink myLink = new HyperLink();
          myLink.Text = columnText;
          myLink.NavigateUrl = "Update.aspx?id=" + columnID;


    Where columnText is the result that you would like to display in the link and columnID is the key value that you need to navigate in the URL.
    LVL 9

    Author Closing Comment

    I finally saw what you meant when creating the datasouce inside VS. There was an option for parameters and it worked. Looks like ot grabs the value after the query string and sets up a variable to do things with for you.
    LVL 18

    Expert Comment

    by:Jerry Miller
    Awesome! Glad you were able to get things working.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction HyperText Transfer Protocol ( or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    This video teaches users how to migrate an existing Wordpress website to a new domain. provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now