By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,954 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,954 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

--
Scott
http://www.OdeToCode.com
On Wed, 14 Apr 2004 18:19:20 +0200, "bg"
<st**************@hotmail.com> wrote:
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


Nov 18 '05 #2

P: n/a
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
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:ss********************************@4ax.com...
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.

--
Scott
http://www.OdeToCode.com
On Wed, 14 Apr 2004 18:19:20 +0200, "bg"
<st**************@hotmail.com> wrote:
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

Nov 18 '05 #3

P: n/a
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
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:ss********************************@4ax.com...
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.

--
Scott
http://www.OdeToCode.com
On Wed, 14 Apr 2004 18:19:20 +0200, "bg"
<st**************@hotmail.com> wrote:
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

Nov 18 '05 #4

P: n/a
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,
--
Scott
http://www.OdeToCode.com

On Thu, 15 Apr 2004 10:36:43 +0200, "bg"
<st**************@hotmail.com> wrote:
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
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:ss********************************@4ax.com.. .
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.

--
Scott
http://www.OdeToCode.com
On Wed, 14 Apr 2004 18:19:20 +0200, "bg"
<st**************@hotmail.com> wrote:
>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 sometimesnot. >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
>
>


Nov 18 '05 #5

P: n/a
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().Subst ring(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


"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:3l********************************@4ax.com...
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,
--
Scott
http://www.OdeToCode.com

On Thu, 15 Apr 2004 10:36:43 +0200, "bg"
<st**************@hotmail.com> wrote:
Hi Scott and thanks for the tip.

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

Thanks
/Stefan
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:ss********************************@4ax.com.. .
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.

--
Scott
http://www.OdeToCode.com
On Wed, 14 Apr 2004 18:19:20 +0200, "bg"
<st**************@hotmail.com> wrote:

>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
>
>

Nov 18 '05 #6

P: n/a
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.

--
Scott
http://www.OdeToCode.com

On Fri, 16 Apr 2004 13:43:27 +0200, "bg"
<st**************@hotmail.com> wrote:
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().Subst ring(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


"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:3l********************************@4ax.com.. .
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,
--
Scott
http://www.OdeToCode.com

On Thu, 15 Apr 2004 10:36:43 +0200, "bg"
<st**************@hotmail.com> wrote:
>Hi Scott and thanks for the tip.
>
>I tried that yesterday but don't know what type to declare. DataItemseems >not to exists... String wont work of couse.
> public string GetDate2(string sIn)
> {
>
>Thanks
>/Stefan
>
>
>"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
>news:ss********************************@4ax.com.. .
>> 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.
>>
>> --
>> Scott
>> http://www.OdeToCode.com
>>
>>
>> On Wed, 14 Apr 2004 18:19:20 +0200, "bg"
>> <st**************@hotmail.com> wrote:
>>
>> >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 andsometimes >not.
>> >How can I check if it exists to avoid crash (DataBinder.Eval:
>> >'System.Data.DataRowView' does not contain a property with the namedate) >> >
>> ><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
>> >
>> >
>>
>


Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.