views:

734

answers:

2

I am trying to bind data from SQL to a repeater control. I have tried what I usually do for a Gridview and it doesn't work. I would like to see an example whether it be using an SQLAdapter or using ExecuteReader from a command. Thank you!

string sql = "SELECT [WallPost], [DatePosted] FROM [WallTable] WHERE [UserId] = '"
 + Request["friend"] + "'";

string strCon =      
System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;

SqlConnection conn = new SqlConnection(strCon);

SqlDataAdapter daUserProfile = new SqlDataAdapter(sql, conn);

dsSocialSite.UserProfileDataTable tbUserProfile = 
    new dsSocialSite.UserProfileDataTable();

daUserProfile.Fill(tbUserProfile);

rpWall2.DataSource = tbUserProfile; //rpWall2 = repeater control

rpWall2.DataBind();
+3  A: 

Using an example I had knocked up the other day

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" 
AutoEventWireup="true"CodeFile="Default.aspx.cs" Inherits="Default2"
Title="Untitled      Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<div id="Questions" align="center" style="background-color: #C0C0C0">
    <asp:Repeater ID="QuestionsRepeater" runat="server" 
         DataSourceID="SqlDataSourceQuestions">
    <ItemTemplate>
    <div align="left" style="text-indent: 15px">
        <asp:Label ID="Label1" 
         runat="server" Text= '<%# Eval("QCategory") %>' 
         Font-Bold="True" Font-Size="Medium"></asp:Label>
    </div>

        <br />
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
         DataSourceID="SqlDataSourceRatings" DataTextField="RatingsCategory" 
         DataValueField="RatingsCategory"  RepeatDirection="Horizontal" >
        </asp:RadioButtonList>

    </ItemTemplate>
</asp:Repeater>

<asp:SqlDataSource ID="SqlDataSourceQuestions" runat="server" 
    ConnectionString="<%$ ConnectionStrings:sandboxConnectionString %>" 
    SelectCommand="SELECT [QCategory] FROM [QuestionsCategory]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceRatings" runat="server" 
ConnectionString="<%$ ConnectionStrings:sandboxConnectionString %>" 
SelectCommand="SELECT [RatingsCategory], [RatingsId] FROM [Ratings]">

John Nolan
Thank you, I am actually looking to do it using the code behind the page. I appreciate it!
Ok I actually tried it this way and when I test the query, it shows the info I want. It looks exactly like your code because I did it in design view. When I go to view the page, nothing shows up???
Nevermind that worked...I forgot to add the Template.
A: 

Doing it on the html (as in John Nolans answer) side is easier with the repeater since it uses templates.

If you want to do it in code behind, create a class that implements ITemplate then place it like so:

myRepeater.ItemTemplate = new MyTemplateClass();
SirDemon