Link to home
Create AccountLog in
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

Avatar of ashley2009
ashley2009

How to pass a QueryString in ASP .NET's SelectCommand from Code Front?
Hello,

I have a question related to <asp:SqlDataSource's SelectCommand. How to pass a querystring like  <%# Eval("PhotoID") %>  in the selectCommand.

The whole frontend code is attached.Please also observe the attached image. The number 7 gets printed in the page,  and it comes from

<%# Eval("PhotoID") %>.

<%# Eval("PhotoID") %> is the current displayed picture's PhotoID.

I want to pass this PhotoID or I want to pass  <%# Eval("PhotoID") %> to the select command dynamically.

I have tried the following approaches:

1)  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnetdb %>" SelectCommand="SELECT UserName, commentDesc, uID FROM photo_comment_view Where pID = @pID">
<SelectParameters><asp:Parameter Name="pID" DefaultValue="7" /></SelectParameters>
</asp:SqlDataSource>

--The above works but it gives only information about PhotoID 7, but there are other photos; How to set pID's value to <%# Eval("PhotoID") %> in ASP .NET's frontend page?

Then I also tried,

2)  <asp:SqlDataSource ID="SqlDataSource1" runat="server"  ConnectionString="<%$ ConnectionStrings:aspnetdb %>" SelectCommand="SELECT UserName, commentDesc, uID FROM photo_comment_view Where pID =  <%#'Eval("PhotoID') %>"></asp:SqlDataSource>

---Gives Parser error: Parser Error Message: The server tag is not well formed.

What I should do so that I can pass current PhotoID to SelectCommand? Please suggest. User generated image
<%@	Page Language="VB" MasterPageFile="~/Default.master" Title="Your Name Here | Picture Details"
	CodeFile="Details.aspx.vb" Inherits="Details_aspx" %>

<asp:content id="Content1" contentplaceholderid="Main" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
	<div class="shim solid"></div>
    
   
	<div class="page" id="details">
<%--Update Panel Here--%>
<asp:UpdatePanel runat="server" ID="updatepanel">
  <ContentTemplate>
		<asp:formview id="FormView1" runat="server"	datasourceid="ObjectDataSource1" cssclass="view"
			borderstyle="none" borderwidth="0" CellPadding="0" cellspacing="0" EnableViewState="false" AllowPaging="true">
			<itemtemplate>
			
				<div class="buttonbar buttonbar-top">
					<a href="Albums.aspx"><asp:image ID="Image1" runat="Server"	 skinid="gallery" /></a>
					&nbsp;&nbsp;&nbsp;&nbsp;
					<asp:ImageButton ID="ImageButton9" Runat="server" CommandName="Page" CommandArgument="First" skinid="first"/>
					<asp:ImageButton ID="ImageButton10"	Runat="server" CommandName="Page" CommandArgument="Prev" skinid="prev"/>
					<asp:ImageButton ID="ImageButton11"	Runat="server" CommandName="Page" CommandArgument="Next" skinid="next"/>
					<asp:ImageButton ID="ImageButton12"	Runat="server" CommandName="Page" CommandArgument="Last" skinid="last"/>
				</div><br/>
                <%# Eval("PhotoID") %><br />
				<p><%# Server.HtmlEncode(Eval("Caption").ToString()) %></p>
				<table border="0" cellpadding="0" cellspacing="0" class="photo-frame">
					<tr>
						<td class="topx--"></td>
						<td class="top-x-"></td>
						<td class="top--x"></td>
					</tr>
					<tr>
						<td class="midx--"></td>
						<td><img src="Handler.ashx?PhotoID=<%# Eval("PhotoID") %>&Size=L" class="photo_198" style="border:4px solid white" Height="100" alt='Photo Number <%# Eval("PhotoID") %>' /></a></td>
						<td class="mid--x"></td>
					</tr>
					<tr>
						<td class="botx--"></td>
						<td class="bot-x-"></td>
						<td class="bot--x"></td>
					</tr>
				</table>
				<p><a href='Download.aspx?AlbumID=<%# Eval("AlbumID") %>&Page=<%# Container.DataItemIndex %>'>
					<asp:image runat="Server" id="DownloadButton" AlternateText="download this photo" skinid="download" /></a></p>
				<div class="buttonbar">
					<a href="Albums.aspx"><asp:image ID="Image2" runat="Server"	 skinid="gallery" /></a>
					&nbsp;&nbsp;&nbsp;&nbsp;
					<asp:ImageButton ID="ImageButton1" Runat="server" CommandName="Page" CommandArgument="First" skinid="first"/>
					<asp:ImageButton ID="ImageButton2" Runat="server" CommandName="Page" CommandArgument="Prev" skinid="prev"/>
					<asp:ImageButton ID="ImageButton3" Runat="server" CommandName="Page" CommandArgument="Next" skinid="next"/>
					<asp:ImageButton ID="ImageButton4" Runat="server" CommandName="Page" CommandArgument="Last" skinid="last"/>
				</div>

			</itemtemplate>
		</asp:formview>

</ContentTemplate>
</asp:UpdatePanel>

<%-- deleted to save space --%>

<%--End updatepanel
--%>		

<%--UpdateProgress--%>
        <asp:UpdateProgress ID="UpdateProgress1" runat="server">
            <ProgressTemplate>
                <div id="box">
                    <img id="Img1" runat="server" alt="Loading" src="~/Images/ajax-loader.gif" /></div>
            </ProgressTemplate>
        </asp:UpdateProgress>


    </div>

    <br />
    <div id="comment" style="
   
    z-index:21; 
    font-family: Verdana;height:200px;
	font-size: 10px;
	text-align:left;
	color:#333333;
	letter-spacing:.1em;
    text-align:justify;
	padding: 0;
	width: 600px;overflow:scroll;overflow-x:hidden;">
    <asp:TextBox ID="TextBox1" runat="server" Width="300" ></asp:TextBox> <br />
     <asp:Button ID="Button1" runat="server" BackColor="Transparent" Width="150" Text="Submit Your Comment"></asp:Button> <br />
     <strong style="font-size:20px; font-weight:bolder;">User's Comment</strong><br /><br />
      
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="UserName" HeaderText="UserName" 
                    SortExpression="UserName" />
                <asp:BoundField DataField="commentDesc" HeaderText="commentDesc" 
                    SortExpression="commentDesc" />
                <asp:BoundField DataField="uID" HeaderText="uID" SortExpression="uID" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:aspnetdb%>" SelectCommand=
            "SELECT UserName, commentDesc, uID FROM photo_comment_view Where pID =  <%#'Eval("PhotoID') %>">

            </asp:SqlDataSource>
   </div>
	<br /><br /><br />
	<asp:ObjectDataSource ID="ObjectDataSource1" Runat="server" TypeName="PhotoManager" 
		SelectMethod="GetPhotos">
		<SelectParameters>
			<asp:QueryStringParameter Name="AlbumID" Type="Int32" QueryStringField="AlbumID" DefaultValue="0"/>
		</SelectParameters>
	</asp:ObjectDataSource>
	
</asp:content>

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of JayFromPepJayFromPep🇺🇸

I think your quotes are a bit off kilter.  Try this...

<asp:SqlDataSource ID="SqlDataSource1" runat="server"  ConnectionString="<%$ ConnectionStrings:aspnetdb %>" SelectCommand='SELECT UserName, commentDesc, uID FROM photo_comment_view Where pID =  <%#Eval("PhotoID") %>'></asp:SqlDataSource>

Avatar of ashley2009ashley2009

ASKER

Your solution also gives parser error:

Incorrect syntax near '<'.

Do you think I need to convert <%# Eval("PhotoID") %> to string? If so, how? Or how to solve this?

I have also tried this but still error:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
Line 107:            ConnectionString="<%$ ConnectionStrings:aspnetdbConnectionString %>" SelectCommand=
Line 108:            'SELECT UserName, commentDesc, uID FROM photo_comment_view Where pID = "<%# Server.HtmlEncode(Eval('PhotoID').ToString())"  %>'>


The error is:

Parser Error Message: The server tag is not well formed.


Does anyone have any suggestion. I did internet research, but I was not able to locate any tutorial or article about it. If you can, please give suggestions.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of xizwyckxizwyck🇺🇸

1. Use single quotes whenever you use <% %>, you have double's in line 107. But I don't think this will work.
2. Set it from code behind while binding data to the gridview.

For example (Intellisence will correct my typos):

See below.

Then make sure you call it from the front side:
OnDataBind = "GridView1_DataBind" in the gridview tag.
protected void GridView_DataBind(object sender, GridViewEventArgs e)
{
   if(e.Row.RowType == RowType.DataRow)
   {
       string PhotoID = DataBinder.Eval(e.Row.DataItem,"PhotoID").ToString();
       SqlDataSource1.SelectCommand = "SELECT UserName, commentDesc, uID FROM photo_comment_view Where pID = " + PhotoID;
   } 
}

Open in new window


Hello,

but GridView does not have OnDataBind propery. And also how this
e.Row.DataItem,"PhotoID").ToString()
can fetch PhotoID. I believe that it is not possible as PhotoID is part of FormView control.

Hello,

 I think that I have not explained the scenerio. The website is the Personal template from Microsoft: http://msdn.microsoft.com/en-us/library/ms972963.aspx or you can find the project at

http://msdn.microsoft.com/en-us/express/aa700818#personal or at

http://msdn.microsoft.com/en-us/express/cc533486

I want to add a comment feature with it. There is a PhotoManager Business object where the stored procedures get excuted. I added 1 method, GetComment(int PhotoID), for my comment feature.

I can display comments about a specific picture,
and I get the QueryString from the url that gets generated from Photos.aspx.

In Photos.aspx, this line generates the query string for details.aspx:

<td><a href='Details.aspx?AlbumID=<%# Eval("AlbumID") %>&PhotoID=<%# Eval("PhotoID") %>'>
                            <img src="Handler.ashx?PhotoID=<%# Eval("PhotoID") %>&Size=S" class="photo_198" style="border:4px solid white" alt='Thumbnail of Photo Number <%# Eval("PhotoID") %>' /></a></td>


and then in details.aspx, the image with its comments get showed. However, I have faced a new problem, which is:

While I browse the image with image button or the pager links, the url does not get modified. For example,
say I selected at Photos.aspx photo #7 from Album # 6. Then, the new url is:


http://localhost:64570/PersonalWebSiteCSharp/Details.aspx?AlbumID=6&PhotoID=7

And the comments get displayed for Photo# 7

However, when I go to next image or previous image either with the aid of imagebutton or pager links, the url does not get modified to:


http://localhost:64570/PersonalWebSiteCSharp/Details.aspx?AlbumID=6&PhotoID=8 or
http://localhost:64570/PersonalWebSiteCSharp/Details.aspx?AlbumID=6&PhotoID=9

As a result, I cannot see the comments of the selected photo as the url stays unchanged.

How to change the url with a new "PhotoID" QueryString as I or the User goes to next or previous or browse
through photos either by ImageButton or by the Pager Links?

I made a YouTube video about this problem. The YouTube video's link is at:

http://www.youtube.com/watch?v=ISDQiqvpuQg

How can I change the url with a new PhotoID QueryString as the User goes to next or previous or browse through photos
either by ImageButton or by the Pager Links?

<%@	Page Language="C#" MasterPageFile="~/Default.master" Title="Your Name Here | Photos"
	CodeFile="Photos.aspx.cs" Inherits="Photos_aspx" %>

<asp:content id="Content1" contentplaceholderid="Main" runat="server">
	
	<div class="shim solid"></div> 
	  
	<div class="page" id="photos">
		<div class="buttonbar buttonbar-top">
			<a href="Albums.aspx"><asp:image ID="Image1" runat="Server" skinid="gallery" /></a>
		</div>
		<asp:DataList ID="DataList1" runat="Server"	cssclass="view"	dataSourceID="ObjectDataSource1" 
			repeatColumns="4" repeatdirection="Horizontal" onitemdatabound="DataList1_ItemDataBound" EnableViewState="false">
			<ItemTemplate>
				<table border="0" cellpadding="0" cellspacing="0" class="photo-frame">
					<tr>
						<td class="topx--"></td>
						<td class="top-x-"></td>
						<td class="top--x"></td>
					</tr>
					<tr>
						<td class="midx--"></td>
						<td><a href='Details.aspx?AlbumID=<%# Eval("AlbumID") %>&PhotoID=<%# Eval("PhotoID") %>'>
							<img src="Handler.ashx?PhotoID=<%# Eval("PhotoID") %>&Size=S" class="photo_198" style="border:4px solid white" alt='Thumbnail of Photo Number <%# Eval("PhotoID") %>' /></a></td>
						<td class="mid--x"></td>
					</tr>
					<tr>
						<td class="botx--"></td>
						<td class="bot-x-"></td>
						<td class="bot--x"></td>
					</tr>
				</table>
				<p><%# Server.HtmlEncode(Eval("Caption").ToString()) %></p>
			</ItemTemplate>
			<FooterTemplate>
			</FooterTemplate>
		</asp:DataList>
		<asp:panel id="Panel1" runat="server" visible="false" CssClass="nullpanel">There are currently no pictures in this album.</asp:panel>
		<div class="buttonbar">
			<a href="Albums.aspx"><asp:image id="gallery" runat="Server" skinid="gallery" /></a>
		</div>
	</div>
	
	<asp:ObjectDataSource ID="ObjectDataSource1" Runat="server" TypeName="PhotoManager" 
		SelectMethod="GetPhotos">
		<SelectParameters>
			<asp:QueryStringParameter Name="AlbumID" Type="Int32" QueryStringField="AlbumID" DefaultValue="0"/>
		</SelectParameters>
	</asp:ObjectDataSource>
	
</asp:content>
---------------------------------------------------------------------
<%@	Page Language="C#" MasterPageFile="~/Default.master" Title="Your Name Here | Picture Details"
	CodeFile="Details.aspx.cs" Inherits="Details_aspx" %>

<asp:content id="Content1" contentplaceholderid="Main" runat="server">

	<div class="shim solid"></div>
	
	<div class="page" id="details">
  
		<asp:formview id="FormView1" runat="server"	datasourceid="ObjectDataSource1" cssclass="view" 
			borderstyle="none" borderwidth="0" CellPadding="0" cellspacing="0" EnableViewState="false" AllowPaging="true">
			<itemtemplate>
			
				<div class="buttonbar buttonbar-top">
					<a href="Albums.aspx"><asp:image ID="Image1" runat="Server"	 skinid="gallery" /></a>
					    
					<asp:ImageButton ID="ImageButton9" Runat="server" CommandName="Page" CommandArgument="First" skinid="first"/>
					<asp:ImageButton ID="ImageButton10"	Runat="server" CommandName="Page" CommandArgument="Prev" skinid="prev"/>
					<asp:ImageButton ID="ImageButton11"	Runat="server" CommandName="Page" CommandArgument="Next" skinid="next"/>
					<asp:ImageButton ID="ImageButton12"	Runat="server" CommandName="Page" CommandArgument="Last" skinid="last"/>
				</div>
				<p><%# Server.HtmlEncode(Eval("Caption").ToString()) %></p>
				<table border="0" cellpadding="0" cellspacing="0" class="photo-frame">
					<tr>
						<td class="topx--"></td>
						<td class="top-x-"></td>
						<td class="top--x"></td>
					</tr>
					<tr>
						<td class="midx--"></td>
						<td><img src="Handler.ashx?PhotoID=<%# Eval("PhotoID") %>&Size=S" class="photo_198" 
                        style="border:4px solid white" alt='Photo Number <%# Eval("PhotoID") %>' /></a></td>
						<td class="mid--x"></td>
					</tr>
					<tr>
						<td class="botx--"></td>
						<td class="bot-x-"></td>
						<td class="bot--x"></td>
					</tr>
				</table>
				<p><a href='Download.aspx?AlbumID=<%# Eval("AlbumID") %>&Page=<%# Container.DataItemIndex %>'>
					<asp:image runat="Server" id="DownloadButton" AlternateText="download this photo" skinid="download" /></a></p>
				<div class="buttonbar">
					<a href="Albums.aspx"><asp:image ID="Image2" runat="Server"	 skinid="gallery" /></a>
					    
					<asp:ImageButton ID="ImageButton1" Runat="server" CommandName="Page" CommandArgument="First" skinid="first"/>
					<asp:ImageButton ID="ImageButton2" Runat="server" CommandName="Page" CommandArgument="Prev" skinid="prev"/>
					<asp:ImageButton ID="ImageButton3" Runat="server" CommandName="Page" CommandArgument="Next" skinid="next"/>
					<asp:ImageButton ID="ImageButton4" Runat="server" CommandName="Page" CommandArgument="Last" skinid="last"/>
                    <br />
                      <br />
                    <asp:Label ID="TESTLABEL" runat="server" Text="HELLO" ForeColor="Cyan" Width="200" Height="300"></asp:Label>
				</div>

			</itemtemplate>
		</asp:formview>
        <hr />
        
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource2">
    	<itemtemplate>
        PhotoID: <%# Eval("pID") %><br />
       <strong> User Name: <%# Eval("UserName")%><br/></strong>
         <%# Eval("CommentDesc")%><br/>
         ----------------------------------------<br />

        </itemtemplate>
    </asp:Repeater>


	</div>

	<asp:ObjectDataSource ID="ObjectDataSource1" Runat="server" TypeName="PhotoManager" 
		SelectMethod="GetPhotos">
		<SelectParameters>
			<asp:QueryStringParameter Name="AlbumID" Type="Int32" QueryStringField="AlbumID" DefaultValue="0"/>
		</SelectParameters>
	</asp:ObjectDataSource>

  
  <asp:ObjectDataSource ID="ObjectDataSource2" Runat="server" TypeName="PhotoManager"  
		SelectMethod="GetComments">
		<SelectParameters>
			<asp:QueryStringParameter Name="PhotoID" Type="Int32" QueryStringField="PhotoID"
            DefaultValue="0"/>
		</SelectParameters>
         
        </asp:ObjectDataSource>
</asp:content>
-------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;


public partial class Details_aspx : System.Web.UI.Page {

    void Page_Load(object sender, EventArgs e) {
		Page.MaintainScrollPositionOnPostBack = true;
        if (!IsPostBack) {
             int i = Convert.ToInt32(Request.QueryString["PhotoID"]);
            if (i >= 0) FormView1.PageIndex = i;
        }
    }
    
}

Open in new window




Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of xizwyckxizwyck🇺🇸

Sorry, was going from memory. I get datalist, gridview, etc mixed up.

I thought PhotoID was a part of your dataset.

I'll have to take a look at that.


ASKER CERTIFIED SOLUTION
Avatar of ashley2009ashley2009

ASKER

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Please read all the comments. I stated the solution, which solved my problem.
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.