Formatting addresses on web page

Discussion in 'ASP .Net' started by Mike, Dec 14, 2006.

  1. Mike

    Mike Guest

    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
     
    Mike, Dec 14, 2006
    #1
    1. Advertising

  2. 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
     
    Laurent Bugnion, Dec 14, 2006
    #2
    1. Advertising

  3. Mike

    Mike Guest

    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" <> wrote in message
    news:...
    > 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
     
    Mike, Dec 14, 2006
    #3
  4. Mike

    Mike Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    >> 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

    >
    >
     
    Mike, Dec 14, 2006
    #4
  5. Mike

    Steve B. Guest

    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" <> a écrit dans le message de news:
    %...
    >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
    >
     
    Steve B., Dec 14, 2006
    #5
  6. Mike

    Mike Guest

    Great! Thanks - that's two more things I've learned today!

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

    >
    >
     
    Mike, Dec 14, 2006
    #6
  7. Mike

    Mike Guest

    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" <> wrote in message
    news:%23%...
    > Great! Thanks - that's two more things I've learned today!
    >
    > "Steve B." <_swap_msn_and_com> wrote in message
    > news:...
    >> 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" <> a écrit dans le message de news:
    >> %...
    >>>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
    >>>

    >>
    >>

    >
    >
     
    Mike, Dec 14, 2006
    #7
  8. Mike

    Steve B. Guest

    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" <> a écrit dans le message de news:
    er%...
    > 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" <> wrote in message
    > news:%23%...
    >> Great! Thanks - that's two more things I've learned today!
    >>
    >> "Steve B." <_swap_msn_and_com> wrote in message
    >> news:...
    >>> 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" <> a écrit dans le message de news:
    >>> %...
    >>>>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
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Steve B., Dec 14, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miguel Dias Moura
    Replies:
    4
    Views:
    1,126
    Zenobia
    Jun 26, 2004
  2. VB Programmer
    Replies:
    3
    Views:
    3,773
    VB Programmer
    Aug 9, 2004
  3. VB Programmer

    Web service for finding addresses?

    VB Programmer, Dec 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    409
  4. namespace1
    Replies:
    3
    Views:
    906
  5. Matthew Margolis

    Formatting addresses question

    Matthew Margolis, Sep 25, 2004, in forum: Ruby
    Replies:
    3
    Views:
    96
    Matthew Margolis
    Sep 25, 2004
Loading...

Share This Page