title: Image url in code-behind not working using asp.net
body: <p>I have a problem with displaying images in my website using asp.net. Originally I have it all working successfully when i haven't integrated yet a strongly typed data control in my repeater. Here is the code previously:</p>
  
  <pre><code>public void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
  {
      Image img = e.Item.FindControl("brandImage") as Image;
      img.ImageUrl = "../Images/Guitar Brands/Guitar Items/" + 
        ((DataRowView)e.Item.DataItem).Row["itemimage1"];
  }
  </code></pre>
  
  <p>The code above works perfectly when im still using datasets. But when i started using strongly typed repeater, it gives me this server error:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'System.Data.Entity.DynamicProxies.
    stringInstrumentItem_F14A58FE0974F90187F45D6
    203769613D21C52F3672B47D240863DC806C87C17' to type 
    'System.Data.DataRowView'.</p>
    
    <p>Description: An unhandled exception occurred during the execution of
    the   current web request. Please review the stack trace for more
    information   about the error and where it originated in the code. </p>
    
    <p>Exception Details: System.InvalidCastException: Unable to cast object
    of type   'System.Data.Entity.DynamicProxies.<br>
    stringInstrumentItem_F14A58FE0974F90187F45D6203769613D21C52F3672B47D24086
    3DC806C87C17' to type 'System.Data.DataRowView'.</p>
    
    <p>Source Error: </p>
    
    <p>Line 26:     {   Line 27:         Image img =
    e.Item.FindControl("brandImage") as Image;   Line 28:<br>
    img.ImageUrl = "../Images/Guitar Brands/Guitar Items/" +<br>
    ((DataRowView)e.Item.DataItem).Row["itemimage1"];   Line 29:     }<br>
    Line 30: </p>
    
    <p>Source File: c:\Users\User1\Documents\Visual Studio<br>
    2015\WebSites\MusicStore\Pages\GuitarItemsFront.aspx.cs    Line: 28 </p>
    
    <p>Stack Trace: </p>
    
    <p>[InvalidCastException: Unable to cast object of type<br>
    'System.Data.Entity.DynamicProxies. 
    stringInstrumentItem_F14A58FE0974F90187F45D6203769613D21C52F 
    3672B47D240863DC806C87C17' to type 'System.Data.DataRowView'.] 
    Pages_GuitarItemsFront.repeater_ItemDataBound(Object sender,<br>
    RepeaterItemEventArgs e) in c:\Users\User1\Documents\Visual Studio<br>
    2015\WebSites\MusicStore\Pages\GuitarItemsFront.aspx.cs:28 
    System.Web.UI.WebControls.Repeater.OnItemDataBound(RepeaterItemEventArgs
    e)   +111  System.Web.UI.WebControls.Repeater.CreateItem(Int32
    itemIndex, ListItemType   itemType, Boolean dataBind, Object dataItem)
    +138  System.Web.UI.WebControls.Repeater.AddDataItemsIntoItemsArray(IEnumerable
    dataSource, Boolean useDataSource) +217 
    System.Web.UI.WebControls.Repeater.PostGetDataAction(IEnumerable<br>
    dataSource) +71 
    System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean<br>
    useDataSource) +220 
    System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +62 
    System.Web.UI.WebControls.Repeater.DataBind() +77 
    System.Web.UI.WebControls.Repeater.EnsureDataBound() +58 
    System.Web.UI.WebControls.Repeater.OnPreRender(EventArgs e) +15 
    System.Web.UI.Control.PreRenderRecursiveInternal() +88 
    System.Web.UI.Control.PreRenderRecursiveInternal() +160 
    System.Web.UI.Control.PreRenderRecursiveInternal() +160 
    System.Web.UI.Control.PreRenderRecursiveInternal() +160 
    System.Web.UI.Control.PreRenderRecursiveInternal() +160 
    System.Web.UI.Page.ProcessRequestMain(Boolean<br>
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    +883</p>
  </blockquote>
  
  <p>The error is pointing towards the image path. I want to know how to make my image url work again with strongly typed data control in repeater. I will include also the code for the aspx file.</p>
  
  <pre><code>&lt;asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"&gt;
  &lt;asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="repeater_ItemDataBound" ItemType="stringInstrumentItem" SelectMethod="GetItemData"&gt;
      &lt;ItemTemplate&gt;
          &lt;div class="one-two"&gt;
              &lt;asp:LinkButton ID="linkButton" OnClick="Repeater1_OnClick" runat="server" CommandArgument='&lt;%# Item.brandId + ";" + Item.model %&gt;'&gt;
                   &lt;asp:Image ID="brandImage" runat="server" ImageUrl='&lt;%# Item.itemimage1 %&gt;' height="130px" width="350px" /&gt;
               &lt;/asp:LinkButton&gt;
              &lt;div class="content"&gt;
                  &lt;div id="label"&gt;&lt;%# Item.brand.name %&gt; &lt;%# Item.model %&gt;&lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p></p>
  
  <p>Also to get an idea what my database is, here are the tables: </p>
  
  <p>Table stringInstrumentItem(The column brandId is the foreign key and references the primary key of table brand, which is also named brandId):</p>
  
  <p><br/>itemId&nbsp;&nbsp;brandId&nbsp;&nbsp;model
  <br/>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xyz
  <br/>2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;abc
  <br/>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hjk</p>
  
  <p>Table brand(which has the primary key called brandId that is referencing by the table strinInstrumentItem):</p>
  
  <p><br/>brandId&nbsp;&nbsp;name&nbsp;&nbsp;image
  <br/>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ibanez&nbsp;&nbsp;&nbsp;&nbsp;xyz.jpg
  <br/>2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fender&nbsp;&nbsp;&nbsp;&nbsp;abc.jpg
  <br/>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Gibson&nbsp;&nbsp;&nbsp;&nbsp;hjk.jpg</p>
  
  <p>Here is also the code for GetItemData Method:</p>
  
  <pre><code>public List&lt;stringInstrumentItem&gt; GetItemData()
  {
      MusicStoreDBEntities obj = new MusicStoreDBEntities();
      List&lt;stringInstrumentItem&gt; name = new List&lt;stringInstrumentItem&gt;();
      name = (from g in obj.stringInstrumentItems where g.brand.name == guitarName &amp;&amp; g.type == "Guitar" select g).ToList();
      return name;
  }
  </code></pre>