How to catch errors in .aspx file

A

AAaron123

<asp:Label id="descriptionLabel" runat="server" Text='<%#
truncate(CStr(Eval("description"))) %>' />

I suppose I could use the code behind to solve this but I wonder if there is
a way in the .aspx file to handle the situation where Eval returns a DBNull?

I wouldn't want to go to an error page just to handle this.

Like a try/catch



Also, is the CStr necessary. I read that Eval returns a string.



This is inherited code.



Thanks
 
A

AAaron123

AAaron123 said:
<asp:Label id="descriptionLabel" runat="server" Text='<%#
truncate(CStr(Eval("description"))) %>' />

I suppose I could use the code behind to solve this but I wonder if there
is a way in the .aspx file to handle the situation where Eval returns a
DBNull?

I wouldn't want to go to an error page just to handle this.

Like a try/catch



Also, is the CStr necessary. I read that Eval returns a string.


Although I read (twice) that Eval returns a String the type is actually an
Object.

So I do need Cstr but it can't handle DBNull!
 
A

AAaron123

I wrote my own CStr function to solve my problem but I still wonder if .aspx
can handle errors like the above.
 
J

Jesse Houwing

Hello AAaron123,
I wrote my own CStr function to solve my problem but I still wonder if
.aspx can handle errors like the above.

It would be an error not to consider that it could be null...

You could use
string.Format("{0}", Eval("Description"))
instead.

Or (don't know the vb syntax, this would work in C#, but it's probably possible
to do):
Eval(description)==DBNull.Value ? "" : Eval(Description)
Or (Again C# syntax)
Eval(description) ?? "";

These all take into account that the value could indeed be null and handle
that gracefully.

Your code just doesn't take into account the fact that it could in fact return
null and result in a nullpointer.
 
A

AAaron123

Hello, Jesse Houwing and thanks

Jesse Houwing said:
Hello AAaron123,


It would be an error not to consider that it could be null...

You could use string.Format("{0}", Eval("Description"))
instead.

Or (don't know the vb syntax, this would work in C#, but it's probably
possible to do):
Eval(description)==DBNull.Value ? "" : Eval(Description)
Or (Again C# syntax)
Eval(description) ?? "";

These all take into account that the value could indeed be null and handle
that gracefully.

Your code just doesn't take into account the fact that it could in fact
return null and result in a nullpointer.
 
H

Hans Kesting

AAaron123 was thinking very hard :
<asp:Label id="descriptionLabel" runat="server" Text='<%#
truncate(CStr(Eval("description"))) %>' />

I suppose I could use the code behind to solve this but I wonder if there is
a way in the .aspx file to handle the situation where Eval returns a DBNull?

I wouldn't want to go to an error page just to handle this.

Like a try/catch



Also, is the CStr necessary. I read that Eval returns a string.



This is inherited code.



Thanks

Instead of cstr, use the ToString method. For a String, this returns
the string itself. For DBNull.Value it returns String.Empty.

I guess: Text='<%# truncate(Eval("description").ToString()) %>'

Hans Kesting
 
A

AAaron123

Hans Kesting said:
AAaron123 was thinking very hard :

Instead of cstr, use the ToString method. For a String, this returns the
string itself. For DBNull.Value it returns String.Empty.

I guess: Text='<%# truncate(Eval("description").ToString()) %>'

Hans Kesting

Thanks
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top