How to catch errors in .aspx file

  • Thread starter Thread starter AAaron123
  • Start date Start date
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
 
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!
 
I wrote my own CStr function to solve my problem but I still wonder if .aspx
can handle errors like the above.
 
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.
 
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.
 
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
 
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
 
Back
Top