Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey my friends, I have two tables Users & Services and my repeater has fields from the two tables.

How can I bind this repeater from the both tables ??

Note : Relation between the two tables: userId forgien key in Services table from Users table.
ASP
<asp:repeater runat="server" ID="rpServices">
<itemtemplate>
[Userphoto] [Username]    // these two fields from users table.
Introduce:
[ServiceName]
in
[ServiceField] in range [ServiceCost]  // these three fields from Services table.
</itemtemplate>



I want the code which bind the repeater from the two tables ??
Posted
Updated 24-Oct-19 21:34pm
v2

Please try following code.

1. create repeater (you can change design, I am using table to show data)

XML
<asp:Repeater ID="rpServices" runat="server">
    <HeaderTemplate>
    <table>
    <tr>
    <th>Userphoto</th>
    <th>Username</th>
    <th>ServiceName</th>
    <th>ServiceField</th>
    <th>ServiceCost</th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
     <td><%# Eval("Userphoto")%></td>
     <td><%# Eval("Username")%></td>
     <td><%# Eval("ServiceName")%></td>
     <td><%# Eval("ServiceField")%></td>
     <td><%# Eval("ServiceCost")%></td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>


2. Create a method for repeater fill up
C#
private void RepeaterBind()
        {
            string connectionString = "Data Source=sandeepss-PC;Initial Catalog=CodeFirst;User ID=sa; Password=knowdev";
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand(@"Select u.Userphoto,u.Username,
                                             s.ServiceName,s.ServiceField,
                                            s.ServiceCost FROM Users as u inner join Services as s on u.userId=s.userId", con);
            IDataReader dr = cmd.ExecuteReader();

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


3. Call this method where you filling repeater control. For example I am calling it on Page_Load

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RepeaterBind();
            }
        }
 
Share this answer
 
You don't bind to two tables, you bind to the result of a query involving those two tables. So, what's your query for those tables?
 
Share this answer
 
why you are binding two tables have u ever feel that a+b=c then in this (a+b) result goes to c in your scenario result will be goes to the one datatable use joins of sql and do it

regards...:)
 
Share this answer
 
Solution 1 didn't work in my project..
 
Share this answer
 
Comments
CHill60 25-Oct-19 5:01am    
Then comment on that solution using the "Have a Question or Comment?" link next to it. Do not post comments as "Solutions"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900