Reputation: 379
I have a Repeater control in my aspx page:
<asp:Repeater ID="repeater" runat="server" EnableViewState="false">
<ItemTemplate>
<%# Eval("someAttribute") %>
</ItemTemplate>
</asp:Repeater>
On Page_Load I have the following code:
if (String.IsNullOrEmpty((string)Request.QueryString["action"]))
s.OpenConn("SELECT * FROM someTable;");
else
s.OpenConn("SELECT * FROM someTable WHERE id=1;");
if (s.Read())
{
repeater.DataSource = s.GetRead();
repeater.DataBind();
}
The problem is that when I enter ?action=something, data is not displayed on the page. But if I remove ?action=something, I get all the data.
Am I doing something wrong?
Upvotes: 0
Views: 111
Reputation: 29041
Looks like your query
s.OpenConn("SELECT * FROM someTable WHERE id=1;");
isn't returning anything. OR, your #Eval statement is returning an empty string. Change this to
<ItemTemplate>
<%#Eval("SomeAttribute")%> I found one!
</ItemTemplate>
to make sure you get some output if the repeater's .DataSource property is not an empty list. This way, "I found one!" will be displayed even if "SomeAttribute" is empty.
Tips:
Never, EVER EVER embed a query directly in a .ASPX.cs. Used stored procedures or (sigh) parameterized queries.
Also, don't do any db interaction in a codebehind page. Refactor your data access into a separate class. You will thank me later. Google for n-tiered architecture, unit testing, or separation of concerns for more detailed analysis.
You don't need to cast .QueryString[] values to a string. They're already strings.
Set a breakpoint on
if (s.Read())
and see what happens when you get there. This will help you find the problem.
Upvotes: 5
Reputation: 13741
You already read and skipped first result in your if statement. Remove this:
if (s.Read())
Upvotes: 2
Reputation: 38346
Unless the object referred to by s
is doing something really odd, the only reason I can think of, is that someTable
has no rows with id = 1
. Have you tried executing the second SQL statement directly in Sql Server Management Studio?
Upvotes: 2