473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Formatting addresses on web page

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("Address 1"),
(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
Dec 14 '06 #1
7 1194
Hi,

Mike wrote:
I want to suppress blank lines in an address on a web page.
<snip>
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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Dec 14 '06 #2
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?
"Laurent Bugnion" <ga*********@bluewin.chwrote in message
news:uM**************@TK2MSFTNGP03.phx.gbl...
Hi,

Mike wrote:
>I want to suppress blank lines in an address on a web page.

<snip>
>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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Dec 14 '06 #3
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

"Mike" <xx*@xxx.xxxwrote in message
news:O3*************@TK2MSFTNGP06.phx.gbl...
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?
"Laurent Bugnion" <ga*********@bluewin.chwrote in message
news:uM**************@TK2MSFTNGP03.phx.gbl...
>Hi,

Mike wrote:
>>I want to suppress blank lines in an address on a web page.

<snip>
>>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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch


Dec 14 '06 #4
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...

"Mike" <xx*@xxx.xxxa écrit dans le message de news:
%2****************@TK2MSFTNGP03.phx.gbl...
>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("Address 1"),
(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

Dec 14 '06 #5
Great! Thanks - that's two more things I've learned today!

"Steve B." <st**********@com.msn_swap_msn_and_comwrote in message
news:uC**************@TK2MSFTNGP06.phx.gbl...
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...

"Mike" <xx*@xxx.xxxa écrit dans le message de news:
%2****************@TK2MSFTNGP03.phx.gbl...
>>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("Addres s1"),
(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


Dec 14 '06 #6
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.

"Mike" <xx*@xxx.xxxwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
Great! Thanks - that's two more things I've learned today!

"Steve B." <st**********@com.msn_swap_msn_and_comwrote in message
news:uC**************@TK2MSFTNGP06.phx.gbl...
>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...

"Mike" <xx*@xxx.xxxa écrit dans le message de news:
%2****************@TK2MSFTNGP03.phx.gbl...
>>>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("Addre ss1"),
(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



Dec 14 '06 #7
Ok sorry for the error..

you can try this :

<asp:Label ID="Address1Label" runat="server" Text='<%# Eval("Address1",
"{0}<br>")%>'
visible='<%# Eval("Address1") != System.Data.DBNull.Value %>'
></asp:Label>

"Mike" <xx*@xxx.xxxa écrit dans le message de news:
er***************@TK2MSFTNGP06.phx.gbl...
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.

"Mike" <xx*@xxx.xxxwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
>Great! Thanks - that's two more things I've learned today!

"Steve B." <st**********@com.msn_swap_msn_and_comwrote in message
news:uC**************@TK2MSFTNGP06.phx.gbl...
>>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...

"Mike" <xx*@xxx.xxxa écrit dans le message de news:
%2****************@TK2MSFTNGP03.phx.gbl...
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("Addr ess1"),
(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



Dec 14 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Joe | last post by:
I want to use mail() to send a message to a group of addresses in a mysql table. I¹ve got my mail script and my sql query, but I don¹t know how to format the query results to fit into the mail()...
10
by: dave | last post by:
Hello, I'm creating a site that has several contact email addresses. These are vlid addresses, i'm wondering if i can use php to alter these addresses so that spam harvesters can't get them, yet a...
4
by: Rob Meade | last post by:
Hi all, Ok - this leads on from speaking to a couple here and in the SQL server group... I've an application which allows the user to type in their text into a form, they add 'happy' tags...
3
by: Grytpype-Thynne | last post by:
Can anyone point me in the direction of a cross browser way of displaying two addresses side by side after some preliminary text? I have tried a containing box and floating two further boxes...
4
by: hope | last post by:
Hi, How can I format a string field using Data Formatting Expression property in datagrid? For example: format last name from BROWN to Brown. Thanks
9
by: sck10 | last post by:
Hello, I have a page with an ImageButton that is used to redirect to another page. When the page first opens, everything looks as expected. However, when I click on the image, the new page...
14
by: Hendrik Maryns | last post by:
Hi, Since I was so pleased to discover how to enable my keyboard to type symbols like … directly, I made a howto-page about it: http://tcl.sfs.uni-tuebingen.de/~hendrik/keyboard.shtml. I...
4
by: =?Utf-8?B?U2VyZ2Vp?= | last post by:
Dear staff Can I get your assistance with \3GB (LARGEADDRESSAWARE) switch in mixed mode process built by VS 2008, please? I have a mixed mode application: C# GUI calling native C++ DLL through...
3
by: =?Utf-8?B?b24tbGluZSBqb3VybmFsIGVkaXRvcg==?= | last post by:
I can't seem to cut-and-paste text, with either .doc or .html formatting, into my .asp web-pages --all the formatting is lost. Is there some code that needs to be added to the page that will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.