Reputation: 3706
I've spent too long on this now, and I'm pretty sure I'm fairly close to the solution but I just need a "little push" over the edge.
I want to grab all fields from my table 'FamousQuotes' and repeat them in a repeater. Unfortunately at the minute, this isn't working; I don't get any compile-time errors, but neither do I see any data appear at runtime in my repeater
Here is my code behind:
Sub randomQuote()
Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
conn.Open()
Dim cmd As New OleDbCommand("SELECT TOP 1 * FROM [FamousQuotes] ORDER BY rnd(INT(NOW*id)-NOW*id)", conn)
cmd.ExecuteNonQuery()
Dim datareader As OleDbDataReader = cmd.ExecuteReader()
If datareader.HasRows Then
datareader.Read()
quoteCascade.DataSource = datareader
quoteCascade.DataBind()
datareader.Close()
datareader = Nothing
End If
conn.Close()
End Sub
You don't probably don't need to see my repeater code, it's nothing remarkable, but here you go:
<asp:Repeater id="quoteCascade" runat="server">
<ItemTemplate>
<blockquote>
"<%#Container.DataItem("Quote")%>"
<small>
<%#Container.DataItem("SourceName")%><cite title="Source Title">
<%#Container.DataItem("SourceTitle")%></cite></small>
</blockquote>
</ItemTemplate>
</asp:Repeater>
So what's going wrong here?
EDIT: I've updated my question with working code, a la Damien Joe's answer below:
Sub randomQuote()
Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
conn.Open()
Dim cmd As New OleDbCommand("SELECT TOP 6 * FROM [FamousQuotes] ORDER BY rnd(INT(NOW*id)-NOW*id)", conn)
quoteCascade.DataSource = cmd.ExecuteReader()
quoteCascade.DataBind()
conn.Close()
End Sub
Upvotes: 1
Views: 3669
Reputation: 13038
You can just execute datareader on the fly, no need to call ExecuteNonQuery()
quoteCascade.DataSource = cmd.ExecuteReader();
quoteCascade.DataBind();
//Close your connection
Upvotes: 4