DataBinder.Eval(Container.DataItem, "date") when date might be missing

B

bg

Hi!

How do I check if "date" exists before using that code?

I've built a RSSreader and sometimes there's a date in it and sometimes not.
How can I check if it exists to avoid crash (DataBinder.Eval:
'System.Data.DataRowView' does not contain a property with the name date)

<asp:DataGrid
id="RssNewsGrid"
AutoGenerateColumns="False"
runat="server"
AlternatingItemStyle-BackColor="#ffffff"
HeaderStyle-BackColor="#ffffff"
OnPageIndexChanged="ChangePaging"
AllowPaging="True"
Width="100%"
Cellpadding="4"
BorderWidth="0">
<PagerStyle Mode="NumericPages" Visible="False" HorizontalAlign="Right" />
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<h2 class="<%=HeaderClass2%>"><%# DataBinder.Eval(Container.DataItem,
"title") %></h2>
<em><%# DataBinder.Eval(Container.DataItem, "date") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


Thank you
/Stefan
 
S

Scott Allen

One solution is to call a method in your page object intead of
DataBinder.Eval, i.e.:

<%# GetDate(Container.DataItem %>

Them implement GetDate to look for the optional field and return
String.Empty if not present.
 
B

bg

Hi Scott and thanks for the tip.

I tried that yesterday but don't know what type to declare. DataItem seems
not to exists... String wont work of couse.
public string GetDate2(string sIn)
{

Thanks
/Stefan
 
S

Scott Allen

Well, it depends on what the DataSource is (an XmlDocument, an
ArrayList, a DataSet, etc).

Something you could try is:

public string GetDate(object o)
{
return String.Empty;
}

and set a breakpoint on the method. Then when you run with the
debugger you can inspect o in a quick watch window and see what type
the data binding is passing. Then go back and declare the method with
the correct type and work from there. A bit of a hack, but like I say,
depending on your DataSource you'll get different types of object
references here.

HTH,
 
B

bg

Hi Scott!

I finally made it after a lot of try and error. Thanks for all the help. I
had problems understanding the object model any ideas where I can learn more
so I get this (including my own code :-> )?
/Stefan

The result:


public string GetDate(Object o)
{
string ret = "";

DataRowView drv = (DataRowView) o;

if (drv.Row.Table.Columns.Contains("date"))
{
int index = drv.Row.Table.Columns.IndexOf("date");
ret = drv.Row.ItemArray.GetValue(index).ToString().Substring(0, 10);
}

return ret;
}


<asp:DataGrid
id="RssNewsGrid"
AutoGenerateColumns="False"
runat="server"
AlternatingItemStyle-BackColor="#ffffff"
HeaderStyle-BackColor="#ffffff"
OnPageIndexChanged="ChangePaging"
AllowPaging="True"
Width="100%"
Cellpadding="4"
BorderWidth="0">
<PagerStyle Mode="NumericPages" Visible="False" HorizontalAlign="Right" />
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<h2 class="<%=HeaderClass2%>"><%# DataBinder.Eval(Container.DataItem,
"title") %></h2>
<em><asp:Label CssClass="txtNormal" runat="server" Text='<%#
GetDate(Container.DataItem) %>' ' ID="Label2" NAME="Label2"/></em>
<asp:Label CssClass="txtNormal" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "description") %>' ID="Label1"
NAME="Label1"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


Thanks again
 
S

Scott Allen

I'm glad you have everything working.

Try search for:
dino esposito data binding

Dino has written a number of good articles on various places around
the web.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top