Formatting addresses on web page

M

Mike

I want to suppress blank lines in an address on a web page.

At the moment, I am displaying the data like this:

<asp:Label ID="Address1Label" runat="server" Text='<%#
Eval("Address1")%>'></asp:Label><br />
<asp:Label ID="Address2Label" runat="server" Text='<%#
Eval("Address2")%>'></asp:Label><br />
<asp:Label ID="Address3Label" runat="server" Text='<%# Eval("Address3")
%>'></asp:Label><br />
<asp:Label ID="TownLabel" runat="server" Text='<%#
Eval("Town")%>'></asp:Label><br />
<asp:Label ID="LocationLabel" runat="server" Text='<%# Eval("Location")
%>'></asp:Label><br />
<asp:Label ID="PostcodeLabel" runat="server" Text='<%# Eval("Postcode")
%>'></asp:Label><br />
<asp:Label ID="TelephoneLabel" runat="server" Text='<%# Eval("Telephone")
%>'></asp:Label><br />
<asp:Label ID="FaxLabel" runat="server" Text='<%#
Eval("Fax")%>'></asp:Label><br />
<asp:Label ID="WebLabel" runat="server" Text='<%#
Eval("Web")%>'></asp:Label><br />

This of course creates blank lines on the page if a line is empty.

I created a method that checks to see if there was anything in the field and
then builds a string including the field, or excluding it depending on the
result:

public static string FormatAddress(string ad1, string ad2, string ad3,
string town, string county, string post)
{
string FormattedAddress ="";
if (ad1 != "")
{ FormattedAddress = ad1 + "<br />"; }
if (ad2 != "")
{ FormattedAddress += ad2 + "<br />"; }
if (ad3 != "")
{ FormattedAddress += ad3 + "<br />"; }
if (town != "")
{ FormattedAddress += town + "<br />"; }
if (county != "")
{ FormattedAddress += county + "<br />"; }
if (post != "")
{ FormattedAddress += post + "<br />"; }
return FormattedAddress;
}

Then I tried to pass this into it:

<asp:Label ID="FormatAddressLabel" runat="server" Text='<%#
MyUtilityClass.FormatAddress((string)Eval("Address1"),
(string)Eval("Address2"), (string)Eval("Address3")...%>'></asp:Label>

but it fell over with an "Unable to cast object of type 'System.DBNull' to
type 'System.String'" exception. I realise what this means, in that at
least one of the fields is null, and this type of casting won't work.

Now I'm stuck. Has anyone got any suggestions?

Thanks

Mike
 
L

Laurent Bugnion

Hi,
I want to suppress blank lines in an address on a web page.

but it fell over with an "Unable to cast object of type 'System.DBNull' to
type 'System.String'" exception. I realise what this means, in that at
least one of the fields is null, and this type of casting won't work.

Now I'm stuck. Has anyone got any suggestions?

Thanks

Mike

You must test the field value against System.DbNull.Value to check if it
exists in the DB. You must do this *before* you call your method,
because the method expects string parameters. DbNull is not equivalent
to null and can also not be casted to a string.

HTH,
Laurent
 
M

Mike

Thanks Laurent,

I kind of guessed that was what I had to do, but I'm not sure how to
proceed. I'm thinking of scrapping the standalone method, but using
something similar to accomplish this task in the FormView's DataBinding
event to intercept the values coming from the datareader - test whether they
are DBNulls or not, then doing my thing with them. Does this sound
sensible?
 
M

Mike

I've worked this out now. The clue was in the exception message I first
got: "Unable to cast Object...." so I worked out that Eval("SomeValue")
produces an object.

All I did then was change the method parameters from "string" to "object",
then within the method body, test if the value of <object>.ToString != null.

Works beautifully :)

Mike
 
S

Steve B.

Change

<asp:Label ID="Address1Label" runat="server" Text='<%#
Eval("Address1")%>'></asp:Label><br />

by :

<asp:Label ID="Address1Label" runat="server" Text='<%# Eval("Address1",
"{0}<br>")%>'></asp:Label>

Repeat this to all of your lines...

It's quite easier that having to test the value...
 
M

Mike

Well, now that I've tried your suggestion, I've found that it doesn't
suppress blank lines if the filed is empty. Nevertheless, I wasn't aware
that you could apply custom formatting in the way you have demonstrated, so
that, together with my improved method is useful.
 
S

Steve B.

Ok sorry for the error..

you can try this :

<asp:Label ID="Address1Label" runat="server" Text='<%# Eval("Address1",
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top