[英]How Do I Use a URL Parameter twice for SQL Queries in two asp.DataLists on one Page?
No matter what I've tried, I cannot get one query to work.无论我尝试过什么,我都无法让一个查询起作用。 I need to query the SQL Server database twice, because I need the album name (URL Parameter) listed under the Album heading.我需要查询 SQL Server 数据库两次,因为我需要专辑标题下列出的专辑名称(URL 参数)。 And I need song information listed under the Songs heading.我需要歌曲标题下列出的歌曲信息。
Here is the code where I use Album
in the WHERE
clause of the two DataLists and their datasources.这是我在两个 DataList 及其数据源的WHERE
子句中使用Album
的代码。 The second WHERE
clause causes an error.第二个WHERE
子句导致错误。
<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 works but the 2nd select where
clause causes this error:第一个查询有效,但第二个 select where
子句导致此错误:
Server Error in '/' Application. “/”应用程序中的服务器错误。
Must declare the scalar variable "@Album".必须声明标量变量“@Album”。
If I take out the WHERE
clause in the second select query, I get all songs of course.如果我在第二个选择查询中取出WHERE
子句,我当然会得到所有歌曲。 But I need the WHERE
clause.但我需要WHERE
子句。
<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 so that the second WHERE
works and there is no error so that I can display only the songs for the url album parameter?如何定义标量变量,以便第二个WHERE
工作并且没有错误,以便我只能显示 url 专辑参数的歌曲?
Doesn't your Request.QueryString
have to have an index?您的Request.QueryString
不是必须有索引吗? For instance Request.QueryString["Album"].ToString()
例如Request.QueryString["Album"].ToString()
Also looks like you're missing this section (within <asp:SqlDataSource/>
):看起来您也缺少此部分(在<asp:SqlDataSource/>
):
<SelectParameters>
<asp:QueryStringParameter Name="Album" DbType = "String" Direction = "Input" QueryStringField="Album" DefaultValue="" ConvertEmptyStringToNull="True" />
</SelectParameters>
Lastly, I'm not sure if it impacts your display, but I think your RepeatColumns="3"
is off compared to what you desire to show, which is only 2 columns ( Track
, Title
).最后,我不确定它是否会影响您的显示,但我认为与您想要显示的内容相比,您的RepeatColumns="3"
已关闭,后者只有 2 列( Track
、 Title
)。 ( https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/datalist-control-in-Asp-Net-part-23/ ) ( https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/datalist-control-in-Asp-Net-part-23/ )
Here's a full example I found: https://www.aspsnippets.com/Articles/ASPNet-SqlDataSource-pass-value-to-SelectParameter-using-QueryString-Parameter-Example.aspx .这是我找到的一个完整示例: https : //www.aspsnippets.com/Articles/ASPNet-SqlDataSource-pass-value-to-SelectParameter-using-QueryString-Parameter-Example.aspx 。 (It's VB but should accomplish the same I sure). (它是 VB,但我确定应该完成相同的操作)。
Have you tried removing the 'SelectParameters' element in the second datasource?您是否尝试删除第二个数据源中的“SelectParameters”元素? -- without diving in too deep, that's the big difference I see between those 2 data sources. -- 无需深入研究,这就是我看到的这两个数据源之间的巨大差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.