Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Silly Question - Null Value 1

Status
Not open for further replies.

figler

Programmer
Dec 26, 2001
155
US
<asp:datalist>
<itemtemplate>
<%# container.dataitem(&quot;dtdate&quot;).toshorttimestring %>
</itemtemplate>
</asp:datalist>

obviously the code is shortened a bit :)
an error is raised when container.dataitem(&quot;dtdate&quot;) is null. any ideas on the best way to combat this? i've tried a few things, including if isdbnull(container.dataitem(&quot;dtdate&quot;) and some try/catch stuff, but none of it is working for a variety of reasons (well, maybe just one -- need to read more about asp.net!).

anyway, when i try to use more than one line of code in that embedded script (and get rid of the # sign) it doesn't recognize the word &quot;container&quot; -- any ideas on why this is would be bonus appreciated.

thanks!

-brad
 
How are you filling your datatable?

There are alot of ways you can catch this before it hits this spot, but it depends what technologies you're using

i.e.
In SQL Server 2k, if you're using a stored proc to get the data, you can have script to check there

If you're using OO coding practices, you could run each record through the object's get/sets, and test for null/assign default value there

Jack
 
Hey Jack, thanks for responding.

I should have mentioned this before... I am binding a dataset to a datalist web control. I can't check for null values in a stored procedure, because... well, I'm returning a whole lot of records/columns. And obviously the oo approach you mentioned isn't too appropriate here.

It should be pretty simple, right? Any ideas?
 
Use the itemcreated event in your code behind and check your problem column for a null value there.

That way every time a new row is created the null value is checked and you can put what error handling you want in it.

That'l do donkey, that'l do
[bravo] Mark
 
Actually, I prefer to use a cleaning function...

<%# cleanDate(container.dataitem(&quot;dtdate&quot;)) %>

protected function cleanDate(str as object) as string
dim output as string
if not isdbnull(str) then
output = str.toshorttimestring
else
output = string.empty 'or whatever for default value
end if
return output
end function

you can stick that cleanDate() in your code-behind file, and re-use it for as many fields as you wish, which is why I prefer this to the itemDataBound for this type of operation.

:)
paul
penny1.gif
penny1.gif
 
very good paul that is an excellent solution That'l do donkey, that'l do
[bravo] Mark
 
I agree -- thanks much Paul. I'll do that. -Brad

ps I tried to &quot;clean&quot; the date in my <% ... %> and had trouble -- is that correct? Should that not work?
 
I'm not sure if that will work. But I guess if you tried it.... I'll trust you.

[thumbsup2]
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top