views:

330

answers:

2

I have a Repeater that is databound to a SQL database. To access the information I do the following:

string Author = (string)DataBinder.Eval(e.Item.DataItem, "Author");

There are other properties (expressions? as intellisense puts it) that I need to access, but I am unsure of what their names are. Can I loop through the e.Item.DataItem and get the names of all the expressions?

I posed this question already, sort of, but I think I was unclear. The result of that post gave me what looks like the table structure from the database, not the actual expression names/values:

UrlWithExtension [System.String]
ThumbnailUrl [System.String]
Content [System.Object]
Language [System.String]
// etc

Getting the values is not really important, just the names of the expressions.

Thanks!

A: 

I think you might be referring to what is called Fields (essentially variables that are declared at class level and made public). You can use the same method that I supplied in that other Q&A thread, with a small modification:

obj.GetType().
    GetFields().
    ToList().
    ForEach(p =>
        Console.WriteLine("{0} [{1}]", p.Name, p.FieldType.ToString()
        ));

I didn't really think about that when I made the code for the properties, since I almost never expose fields publicly in my classes.

When I need to know what the interface of an object looks like, I often find it easiest to simply add a break point in the debugger, and add a watch to examine the object a bit closer. That will reveal all (non-method) members and their types (and current content as well).

Fredrik Mörk
Unfortunately that is not it. When I run that, I get a blank string (I modified your code slightly to append the text to a string builder instead of outputting to the console. Thanks for your continued input!
Anders
A: 

The answer to your other post appears to be the answer to this one as well. To get the "Expresions" that you're looking for, you could do something like this...

var props = e.Item.DataItem.GetType().GetProperties();

foreach (var item in props)
{
    Console.WriteLine(string.Format("{0}:{1}", item.Name, DataBinder.Eval(e.Item.DataItem, item.Name)));
}

This loop will go over your DataItem and grab each property and output its Name and Value to the console. If your Author isn't showing up in the list of results, it could be that it isn't a property but a public Field instead, so you should take a look at GetFields() or some of the other methods as well.

Example using GetFields...

var props = e.Item.DataItem.GetType().GetFields();

foreach (var item in props)
{
    Console.WriteLine(string.Format("{0}:{1}", item.Name, DataBinder.Eval(e.Item.DataItem, item.Name)));
}
Scott Ivey
Hmm, it must be something else. This gives me the same result as Fredrik's code, just the actual values (ie: UrlWithExtension:~/Libraries/Activation_Key/Activation_Key.sflb.ashx) instead of the type :(.
Anders
Did you try it with GetFields() instead of GetProperties()?
Scott Ivey
Yes, that returns a blank string.
Anders
If you're binding to a SQL database, the expressions that you can use with the Eval method will map directly to the columns returned by your query.
Scott Ivey
Hmm, well that still doesn't make much sense to me unless the expressions have aliases or something since 'Author' is not in the GetProperties() list. Is this possible?
Anders
Not sure - what does your SQL statement look like that you are binding to?
Scott Ivey
Haha, good question. I am utilizing a CMS (telerik's SiteFinity), so I do not have direct access to the data layer to see this. This makes it all the more tricky/annoying.
Anders