RockStar
RockStar

Reputation: 87

How to bind data in asp repeater using entity framework?

Originally I was able to populate data in code-behind for the asp repeater like this:

public string guitarName = ConnectionClassGuitarItems.guitarName;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataSet ds = GetData();
        Repeater1.DataSource = ds;
        Repeater1.DataBind();

    }
}

 private void GetData()
{
    string CS = ConfigurationManager.ConnectionStrings["musicStoreConnection"].ConnectionString;
    string query = "SELECT * FROM stringInstrumentItem JOIN brand ON stringInstrumentItem.brandId = brand.brandId WHERE stringInstrumentItem.brandId IN(SELECT brand.brandId FROM brand WHERE name = @brand)";
    using (SqlConnection con = new SqlConnection(CS))
    {
        using (SqlCommand comm = new SqlCommand(query, con))
        {
            con.Open();
            comm.Connection = con;
            comm.Parameters.Add(new SqlParameter("@brand", guitar));
            comm.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            comm.Parameters.Clear();

            return ds;
        }
    }

}

Now, I wanted to change the code above into utilizing entity framework. So far i have already done this:

 private void GetData()
{
    MusicStoreDBEntities obj = new MusicStoreDBEntities();
    List<stringInstrumentItem> name = new List<stringInstrumentItem>();
    name = (from g in obj.stringInstrumentItems where g.brand.name == guitarName && g.type == "Guitar" select g).ToList();
    DataSet ds = new DataSet();

}

I have already changed the raw sql into using linq to entity and added a new condition. But i don't know how to bind the data from entity framework to the asp repeater. I want to know how to bind data in asp repeater using entity framework.

Edited:

I've tried binding it as suggested below but it is giving me an exception error that it does not contain a property with the name 'name' pointing towards to this line of code: <%# Eval("name") %> <%# Eval("model") %>. As you can see, the name inside eval is really part of the table brand(please refer to the additional info below for insight of the two tables in my database). I don't have problems with this previously with the old approach of data binding using the dataset, but when i changed it to using entity framework, it does not recognize name anymore. Can it be just the query?

Here is the full code of the aspx file:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="repeater_ItemDataBound">
    <ItemTemplate>
        <div class="one-two">
            <asp:LinkButton ID="linkButton" OnClick="Repeater1_OnClick" runat="server" CommandArgument='<%# Eval("brandId") + ";" + Eval("model") %>'>
                 <asp:Image ID="brandImage" runat="server" ImageUrl='<%# Eval("itemimage1") %>' height="130px" width="350px" />
             </asp:LinkButton>
            <div class="content">
                <div id="label"><%# Eval("name") %> <%# Eval("model") %></div>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

Additional Info:

Table stringInstrumentItem(The column brandId is the foreign key and references the primary key of table brand, which is also named brandId):


itemId  brandId  model
1         1            xyz
2         1            abc
3         2            hjk

Table brand(which has the primary key called brandId that is referencing by the table strinInstrumentItem):


brandId  name  image
1         Ibanez    xyz.jpg
2         Fender    abc.jpg
3         Gibson    hjk.jpg

Upvotes: 0

Views: 1457

Answers (1)

mason
mason

Reputation: 32694

private List<stringInstrumentItem> GetInstruments()
{
    var musicStoreContext = new MusicStoreDBEntities();
    List<stringInstrumentItem> instruments =
        (from g in obj.stringInstrumentItems
         where g.brand.name == guitarName && g.type == "Guitar"
         select g
        ).ToList();
    return instruments;
}

You can bind Repeaters and other controls directly to strongly typed objects, so just return a list of your model object.

Make sure to take advantage of strongly typed repeaters now.

Upvotes: 1

Related Questions