Hey,
I am trying to do a SELECT where i bind a repeater using the value of a label. I have tested the label to see if the value is actually outputted and it is, but the SELECT does not work…
The Label that i am retrieving the value of is actually within a Listview so i append it to another label outside of the listview so i can get access to it easily.
This is my repeater:-
<!-- start child repeater -->
<asp:repeater id="childRepeater" runat="server"
onitemdatabound="childRepeater_ItemDataBound">
<itemtemplate>
<asp:Label ID="lblCommentID" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "CID") %>'></asp:Label>
<div style="height:auto;margin-bottom:12px;">
<a href="<%# "user-wall.aspx?ID=" + Eval("UID") %>"><asp:Image ID="Image5" runat="server" ImageUrl='<%# string.Format("~/avatars/{0}", Eval("Avatar")) %>' runat="server" Width="45px" Height="45px"/></a>
<p><a href="<%# "user-wall.aspx?ID=" + Eval("UID") %>"><%# DataBinder.Eval(Container.DataItem, "Fname") %></a>
- <%# DataBinder.Eval(Container.DataItem, "Comment") %>
<br />
<span style='float:right;font-size:11px'><asp:LinkButton ID="LinkButton1" runat="server" visible="false" OnClientClick='<%# "remove-response.aspx?ID=" + Eval("CID") %>'>remove</asp:LinkButton></span><small><%# DataBinder.Eval(Container.DataItem, "Added") %></small>
</p>
</div>
</itemtemplate>
</asp:repeater>
<!-- end child repeater -->
</div>
<div style="width: 442px; margin-top:10px; margin-left: auto; margin-right: auto; margin-bottom: auto;">
<asp:TextBox ID="TextBox2" runat="server" CssClass="text-comment" onfocus="this.value='';"
onblur="if (this.value=='') this.value='Write a comment';"
value="Write a comment" ForeColor="#A79090" Width="339px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit"
CommandArgument='<%# Eval("CID") %>' onclick="Button1_Click"/>
</div>
Then on page load i try to bind it like this:-
if (!Page.IsPostBack)
{
string photoID = Request.QueryString["PhotoID"];
string albumID = Request.QueryString["AlbumID"];
ViewState["hfAlbumID"] = albumID;
//Get Page number by passing photo id
int index = GetPageNumber(int.Parse(photoID), int.Parse(albumID));
DataPager1.SetPageProperties(index, 1, true);
string connStr1 = "Data Source=SQLB23.webcontrolcenter.com;User ID=wbsd;Password=*****;";
SqlConnection dbConn1 = new SqlConnection(connStr1);
dbConn1.Open();
string sSQL = "SELECT (hussaini_album_comments.comment_id) AS CID, " +
"(hussaini_album_comments.comment) AS Comment, " +
"(hussaini_album_comments.user_id) AS UID, " +
"(hussaini_album_comments.date_added) AS Added, " +
"(hussaini_users.fname) AS Fname, " +
"(hussaini_users.avatar) AS Avatar " +
"FROM hussaini_album_comments LEFT OUTER JOIN hussaini_users ON " +
"hussaini_album_comments.user_id = hussaini_users.user_id WHERE hussaini_album_comments.pic_id = @pic_id";
SqlCommand cmd = new SqlCommand(sSQL, dbConn1);
cmd.Parameters.Add("@pic_id", SqlDbType.Char).Value = Label6.Text;
SqlDataReader dtrCat = cmd.ExecuteReader();
if (dtrCat.HasRows)
{
childRepeater.DataSource = dtrCat;
childRepeater.DataBind();
}
else
{
Label8.Visible = true;
Label8.Text = "<BR><i>There are no comments for this photo. Please enter something.</i><BR>";
}
dtrCat.Close();
dbConn1.Close();
}
I retrieve the value of Label6 by using the value of Label5 which is inside a listview like so:-
protected void lvPhotoViewer_DataBound(object sender, EventArgs e)
{
foreach (ListViewDataItem item in lvPhotoViewer.Items)
{
Label Label5 = (Label)item.FindControl("Label5");
if (Label5.Text != null)
{
Label6.Text = Label5.Text;
}
}
}
So now i have the value of Label 5 inside Label 6. So in the SELECT why does Label 6 not get picked up?
To prove that it does actually get the value i manage to insert into the database on button click like so:-
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (TextBox2.Text != "Write a comment" && TextBox2.Text != "")
{
string conString = "Data Source=SQLB23.webcontrolcenter.com;User ID=wbsd;Password=******";
SqlConnection empConnection = new SqlConnection(conString);
string insertStatement = "INSERT INTO hussaini_album_comments ([pic_id], [user_id], [comment], [date_added]) "
+ "VALUES (@pic_id, @user_id, @comment, GetDate())";
SqlCommand insertCommand = new SqlCommand(insertStatement, empConnection);
insertCommand.Parameters.Add("@pic_id", SqlDbType.Char).Value = Label6.Text;
insertCommand.Parameters.Add("@user_id", SqlDbType.Char).Value = System.Convert.ToInt32(Session["MEM_ID"].ToString());
insertCommand.Parameters.Add("@comment", SqlDbType.Char).Value = TextBox2.Text;
empConnection.Open();
insertCommand.ExecuteNonQuery();
empConnection.Close();
}
}
finally
{
Response.Redirect("view-photo.aspx?PhotoID=" + Request.QueryString["PhotoID"].ToString() + "&AlbumID=" + Request.QueryString["AlbumID"].ToString() + "&UID=" + Request.QueryString["UID"].ToString());
}
}
See this parameter:-
insertCommand.Parameters.Add(“@pic_id”, SqlDbType.Char).Value = Label6.Text;
I am using Label 6 here and have no problems with it, so why does the SELECT in page load not work?
Hope someone can help.
Regards
Billy