How to bind repeater from two tables in asp.net?

Bind multiple tables to one repeater.

I have a simple solution:

I have two tables Users & Services and my repeater has fields from the two tables.

First create repeater like this.


<asp:Repeater ID="rpServices" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>photo_user</th>
<th>name_user</th>
<th>Service_Name</th>
<th>Field_service</th>
<th>Cost_service</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("photo_user")%></td>
<td><%# Eval("name_user")%></td>
<td><%# Eval("Service_Name")%></td>
<td><%# Eval("Field_service")%></td>
<td><%# Eval("Cost_service")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

Then Create a method for repeater fill up.


private void RepeaterBind()
{
string connectionString = "Data Source=sandeepss-PC;Initial Catalog=CodeFirst;User ID=sa; Password=knowdev";
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
SqlCommand cmd = new SqlCommand(@"Select u.photo_user,u.name_user,
s.Service_Name,s.Field_service,
s.Cost_service FROM Users as u inner join Services as s on u.userId=s.userId", cn);
IDataReader dr = cmd.ExecuteReader();

DataTable dt = new DataTable();
dt.Load(dr);
rpServices.DataSource = dt;
rpServices.DataBind();

}

Finally Call this method where you filling repeater control.

For example I am calling it on Page_Load.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RepeaterBind();
            }
        }

About the Author

avatar