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
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