How Can I Utilize a URL Parameter Twice to Execute SQL Queries in Two asp.DataLists on a Single Page?

I've been trying everything, but I just can't seem to get one query to function properly. My goal is to retrieve information from the SQL Server database in two separate queries - one for album names (based on URL parameter) and another for song details.

Below is the code snippet where I'm utilizing Album in the WHERE clause of both DataLists and their datasources. However, the second WHERE clause is causing an error.

<h2 class="w3-container" style="color:blue;">Album</h2>

<strong><p class="w3-container" id="albumParam" ></p></strong>

<script>
    type = "text/javascript"
    document.getElementById("albumParam").innerHTML = location.search.substring(1).substring(6, 150).replaceAll("+", " ").replaceAll("%2f", "/").replace("%26", "&").replaceAll("%2b", "+").replace("%2c", ",").replaceAll("%27", "'");
</script>

<asp:SqlDataSource ID="SqlDataSourceAlbum" runat="server" 
    ConnectionString="<%$ ConnectionStrings:FMConnectionString %>" ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT Album FROM dbo.c_mymusic_songs WHERE (Album = @Album) ORDER BY Track"></asp:SqlDataSource>
<asp:DataList class="w3-container" ID="DataListAlbum" CellSpacing="3" runat="server" DataSourceID="SqlDataSourceAlbum" ItemStyle-Wrap="false">
    <ItemTemplate>  
        <asp:HyperLink ID="HyperLinkAlbum" runat="server" Text='<%# Eval("Album") %>' />
    </ItemTemplate>
</asp:DataList>

<strong><p class="w3-container" id="AlbumParam" ></p></strong>

<h2 class="w3-container" style="color:blue;">Songs</h2>

<script>
    type = "text/javascript"
    document.getElementById("Param").innerHTML = location.search.substring(1).substring(6, 150).replaceAll("+", " ").replaceAll("%2f", "/").replace("%26", "&").replaceAll("%2b", "+").replace("%2c", ",").replaceAll("%27", "'");
</script>

<asp:SqlDataSource ID="SqlDataSourceSongs" runat="server" 
    ConnectionString="<%$ ConnectionStrings:FMConnectionString %>" ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT Artist, Album, Track, Title FROM dbo.c_mymusic_songs WHERE (Album = @Album) ORDER BY Track">
    <SelectParameters>
      <asp:Parameter Name="Album" Type="string" DefaultValue=" " />
    </SelectParameters>

</asp:SqlDataSource>

<asp:DataList ID="DataListSongs" runat="server" DataSourceID="SqlDataSourceSongs" RepeatColumns="3"  
    CellSpacing="3" RepeatLayout="Table">  
    <ItemTemplate>  
        <table class="table">  
            <tr>  
                <td>  
                    <%# Eval("Track") %><br />  
                </td>  
                <td>  
                    <%# Eval("Title")%>  
                </td>  
            </tr>  
        </table>  
    </ItemTemplate>  
</asp:DataList>  

The first query functions correctly, however, the 2nd select where clause is triggering an error:

Server Error in '/' Application.
Must declare the scalar variable "@Album".

If I remove the WHERE clause in the second select query, all songs are displayed as expected. But, I require the WHERE clause for specific filtering.

<asp:SqlDataSource ID="SqlDataSourceSongs" runat="server" 
    ConnectionString="<%$ ConnectionStrings:FMConnectionString %>" ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT Artist, Album, Track, Title FROM dbo.c_mymusic_songs ORDER BY Track">
</asp:SqlDataSource>

How can I define the scalar variable such that the second WHERE works without any errors, enabling me to display only the songs based on the url album parameter?

Answer №1

Is it necessary for your Request.QueryString to have an index? For example,

Request.QueryString["Album"].ToString()

Additionally, it appears that you are missing this section (inside <asp:SqlDataSource/>):

    <SelectParameters>
        <asp:QueryStringParameter Name="Album" DbType = "String" Direction = "Input" QueryStringField="Album" DefaultValue="" ConvertEmptyStringToNull="True" />
    </SelectParameters>

Also, I'm not sure if it affects your display, but I believe your RepeatColumns="3" may be incorrect based on your desired 2-column layout (Track,Title). ()

Here is a complete example I came across: . (It's in VB but should achieve the same result).

Answer №2

Perhaps you should consider removing the 'SelectParameters' element from the second datasource as a potential solution? It seems to be a significant distinction between the two data sources, without delving too far into it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instructions for connecting a button and an input field

How can I connect a button to an input field? My goal is to make it so that when the button is clicked, the content of the text field is added to an array (displayed below) const userTags = []; function addTags(event) { userTags.push(event.target.__ wha ...

Creating a dynamic dropdown menu using JQuery that does not automatically submit the form when a value is

Upon selecting a background color from the dropdown menu, I am generating a dynamic dropdown for text colors. The Text Color dropdown is populated correctly based on the selection of Background Color. Although the functionality works as intended, I encoun ...

Adjust the placement of the navigation to the middle of the

Currently working on a webpage using html/css, I came across a stylish navigation bar which I decided to customize. However, I'm facing difficulty in aligning the navigation bar at the center of the header. Here is a snapshot of the current layout: h ...

Explain the interaction of cookies between Angular and C# controllers

Can anyone provide clarity on how cookies are utilized between an angular application and a C# server controller? After looking through various discussions and forums, here's what I've gathered: The angular client initiates an HTTP Request (e. ...

Tracking User Visits in Codeigniter

Below is the code breakdown: (Step by step) Place counter.txt in APPPATH . 'logs/counter.txt' Set up counter_helper.php in APPPATH . 'helpers/counter_helper.php'; Autoload the newly created helper in APPPATH . 'config/autoload.p ...

Control for Star Ratings that allows decimal values to be included

I am currently working in Visual Studio 2010 and using C# for coding. I need to create a star rating control where I have values ranging from 1 to 5, and I want to display the rating visually with stars. Here is a detailed explanation of what I am looking ...

Is there a way to keep the text animation going even when I'm not hovering over it with the cursor?

Is there a way to make text continue animating on the page even when the cursor is not placed on it? I understand the hover function, but how can I ensure the text keeps animating without interruption? $(document).ready(function () { $("#start&q ...

Bordering the isotopes

Currently, I am utilizing a plugin that involves the isotope filter library. By setting the width to 33.33%, my list elements are organized into 3 columns. I am trying to specify the middle column to have side borders. However, whenever I click on the filt ...

Looking to incorporate HTML and JavaScript to create two unique forms that can be submitted separately on a single webpage

If I can't find an answer, then I ask: There are two different forms on the page: one is the "ask for call" form (which appears on every page of the site) and the other is a simple "contact form" (specifically placed on the contact page). Both forms ...

Combining TypeScript into HTML resulted in an error: Uncaught ReferenceError clickbutton is not defined

Attempting to create a basic CRUD frontend without the use of any frameworks. I am encountering an issue when trying to include a TypeScript file (index.ts) in my index.html, as the functions called within it are showing as undefined. I understand that bro ...

Minimize the entire project by compressing the .css, .js, and .html files

After recently incorporating Grunt into my workflow, I was thrilled with how it streamlined the process of minifying/concatenating .css files and minifying/uglify/concatenating .js files. With Grunt watch and express, I was able to automate compiling and ...

Create a pair of blocks that are identical in size and positioned next to each other

I am encountering difficulties with a seemingly simple task. Here is what I want to achieve: https://i.stack.imgur.com/z7ITk.png My goal is to have two red blocks of equal height, even if the content inside them varies in height. Using a table is not idea ...

Latest Chrome Update Resolves Fixed Positioning Bug

I am currently facing an issue with setting the second background image in the "Great people providing great service" section to a fixed position. You can view the website here: While the fixed position works in Safari and Firefox, it used to work in Chro ...

Tips for retrieving specific values from drop-down menus that have been incorporated into a dynamically-sized HTML table

How can I retrieve individual values from dropdown menus in HTML? These values are stored in a table of unspecified size and I want to calculate the total price of the selected drinks. Additionally, I need the code to be able to compute the price of any ne ...

What should I designate as the selector when customizing dialog boxes?

I am attempting to change the title bar color of a dialog box in CSS, but I am running into issues. Below is the HTML code for the dialog box and the corresponding CSS. <div id="picture1Dialog" title = "Title"> <p id="picture1Text"> ...

As soon as I hit the submit button on my website form, the URL is automatically refreshed with the information I provided

I am a beginner when it comes to forms and recently copied and pasted this login snippet code: <div class="login-form-1"> <form id="login-form" class="text-left"> <div class="main-login-form"> <div class="login-group"> ...

Troubleshooting problem with media queries in CSS

As a newbie to HTML and CSS, I've been encountering difficulties with media queries. My website seems to only function properly when viewed in a resolution of 1920x1080, so I attempted to implement some media queries in my CSS to cater to other resolu ...

Adjusting the width of a select box to match the size of its child table using CSS

Within my Vue application, I've implemented a select box that contains a table component. When the select box is clicked, the table becomes visible in a container. However, I'm facing an issue where I can't dynamically adjust the width of th ...

A Guide to Choosing a Window Using Selenium in C#

Currently, I am attempting to create a straightforward Selenium test that can locate a window by its title or URL, then proceed to select it and swap tabs once. Despite trying out different methods like driver.SwitchTo().Window(handle); I was successful ...

Struggling with IE7 compatibility issues regarding tables inside a div? Seeking a reliable resource to gain insights and strategies for effectively resolving IE7 problems

I am currently in the process of revamping this website: but I am facing a challenge with repeating a gradient within a div (cnews). The problem arises when the gradient is repeated on IE7, as it distorts the color. It appears as though the blue in the im ...