Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Formatting addresses on web page

Reply
Thread Tools

Formatting addresses on web page

 
 
Mike
Guest
Posts: n/a
 
      12-14-2006
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


 
Reply With Quote
 
 
 
 
Laurent Bugnion
Guest
Posts: n/a
 
      12-14-2006
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
 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      12-14-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      12-14-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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

>
>



 
Reply With Quote
 
Steve B.
Guest
Posts: n/a
 
      12-14-2006
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" <(E-Mail Removed)> a écrit dans le message de news:
%(E-Mail Removed)...
>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
>



 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      12-14-2006
Great! Thanks - that's two more things I've learned today!

"Steve B." <(E-Mail Removed)_swap_msn_and_com> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> a écrit dans le message de news:
> %(E-Mail Removed)...
>>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
>>

>
>



 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      12-14-2006
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" <(E-Mail Removed)> wrote in message
news:%23%(E-Mail Removed)...
> Great! Thanks - that's two more things I've learned today!
>
> "Steve B." <(E-Mail Removed)_swap_msn_and_com> wrote in message
> news:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> a écrit dans le message de news:
>> %(E-Mail Removed)...
>>>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
>>>

>>
>>

>
>



 
Reply With Quote
 
Steve B.
Guest
Posts: n/a
 
      12-14-2006
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" <(E-Mail Removed)> a écrit dans le message de news:
er%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%23%(E-Mail Removed)...
>> Great! Thanks - that's two more things I've learned today!
>>
>> "Steve B." <(E-Mail Removed)_swap_msn_and_com> wrote in message
>> news:(E-Mail Removed)...
>>> 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" <(E-Mail Removed)> a écrit dans le message de news:
>>> %(E-Mail Removed)...
>>>>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
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to implement a firewall for Windows platform that blocks based on Mac addresses instead of IP addresses cagdas.gerede@gmail.com C Programming 1 12-07-2006 04:30 AM
Physical Addresses VS. Logical Addresses namespace1 C++ 3 11-29-2006 03:07 PM
Formatting addresses question Matthew Margolis Ruby 3 09-25-2004 06:22 PM
Re: Web page is not available - "The Web page you requested is not available offline. To view this page, click Connect" Natty Gur ASP .Net 0 06-06-2004 05:46 AM
Need formatting options menu for formatting hard drive Mark T. Computer Support 3 11-24-2003 11:50 PM



Advertisments