Solved

C# DataBinding - List<structure> does not contain a property with the name 'FileName'

Posted on 2012-04-09
5
1,041 Views
Last Modified: 2012-04-10
I am migrating VB.Net Code into a C# Project and have run into the following error:
[But you can see that there is in fact a property in my List of Type Structure called FileName?!]

Error Screenshot with Question Details

Here is the code behind that Binds my List to the Repeater Control [in case it is relevant]:
        private void PopulateConventionalCoolerWalkPanel()
        {

            List<FSPIPartnerGlobal.stcFileInfo> li = new List<FSPIPartnerGlobal.stcFileInfo>();             
            li = objDirectorySearcher.GetFilesInDirectory("C:\\PartnerAccessFiles\\ConventionalCoolerWalk", "*.jpg");
 
            if (li.Count != 0)
            {
                pnlConventionalCoolerWalk.Visible = true;
                rptConventionalCoolerWalk.DataSource = li.GetRange(0, 5);
                rptConventionalCoolerWalk.DataBind();
                litConventionalCoolerWalkUpdated.Text = Convert.ToString(li[0].DateCreated);
            }
            li.Clear();
        }

Open in new window




I am thinking that there must be a syntax error in my Eval code, but it looks ok to me?
Jason
0
Comment
Question by:jsmithr
  • 3
  • 2
5 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37824973
you seem to be clearing the list after binding the data.
Secondaly, simpley using <%#Eval("FileName")%> will also bind the file name to your repeater's item template....

Here is my solution that worked perfectly for me:-

Markup:-
<asp:Repeater runat="server" ID="rptTest">
         <ItemTemplate>
             <span><%#Eval("Name")%></span>
             <span><%#Eval("Id") %></span>
         </ItemTemplate>
         
     </asp:Repeater>

Open in new window

Code Behind:-
using System;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var persons = new List<Person>
                          {
                              new Person() {Id = "1", Name = "Jatin"},
                              new Person() {Id = "2", Name = "John"}
                          };
        rptTest.DataSource = persons;
        rptTest.DataBind();
        persons.Clear();
    }
    protected void btnTest_Click(object sender, EventArgs e)
    {
        
    }

    public class Person
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
}

Open in new window

0
 

Author Comment

by:jsmithr
ID: 37825152
I dont know what to say. I am not defining properties in a C# Class. I am returning a Structure as a List and Binding my repeater to that list... albeit without success.

Thee must be something funny with the way C# DataBinds Lists that I do not understand.

Jason
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37825169
try to add a public property in your struct namely FileName or you can use AIP(automatically implemented properties) as i did in my code.
BTW, i tested my code by changing person from class to struct and it worked seamlessly....

Eval can only bind to public property/field in the list Type....
0
 

Author Comment

by:jsmithr
ID: 37825234
Yep, thats it. its a limitation of vb.net in .net 2.0. I will have to define properties in the structure like this:

    Public Structure stcFileInfo
        Public strFullName As String

        Public DirectoryName As String
        Public FileName As String
        Public Extension As String
        Public DateCreated As DateTime
        Public DateModified As DateTime
        Public AllProperties As FileInfo

        Public Property FullName() As String
            Get
                Return strFullName
            End Get
            Set(ByVal value As String)
                strFullName = value
            End Set
        End Property

    End Structure

Open in new window


Then, return that structure to the C# side, Dimension my List, then DataBind to the List.

The Eval("FullName") then works. I will post the completed code tomorrow.
Jason
0
 

Author Comment

by:jsmithr
ID: 37827072
Here is the modification to the vb.net structure that allowed me to use the C# DataBind via Eval("PropertyName").

Public Class clsDirectorySearcher
    Public Structure stcFileInfo
        Public strFullName As String
        Public strDirectoryName As String
        Public strFileName As String
        Public strExtension As String
        Public dtDateCreated As DateTime
        Public dtDateModified As DateTime
        Public fiAllProperties As FileInfo

        Public Property FullName() As String
            Get
                Return strFullName
            End Get
            Set(ByVal value As String)
                strFullName = value
            End Set
        End Property

        Public Property DirectoryName() As String
            Get
                Return strDirectoryName
            End Get
            Set(ByVal value As String)
                strDirectoryName = value
            End Set
        End Property

        Public Property FileName() As String
            Get
                Return strFileName
            End Get
            Set(ByVal value As String)
                strFileName = value
            End Set
        End Property

        Public Property Extension() As String
            Get
                Return strExtension
            End Get
            Set(ByVal value As String)
                strExtension = value
            End Set
        End Property

        Public Property DateCreated() As DateTime
            Get
                Return dtDateCreated
            End Get
            Set(ByVal value As DateTime)
                dtDateCreated = value
            End Set
        End Property

        Public Property DateModified() As DateTime
            Get
                Return dtDateModified
            End Get
            Set(ByVal value As DateTime)
                dtDateModified = value
            End Set
        End Property

        Public Property AllProperties() As FileInfo
            Get
                Return fiAllProperties
            End Get
            Set(ByVal value As FileInfo)
                fiAllProperties = value
            End Set
        End Property

    End Structure

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now