please help my code c# experts
i have the following code and would like to see how YOU WOULD REWRITE IT BETTER since i am woried about easy of use/update and maintainability.
can this be written better? i am looking for cutting short long if/thens and other loops and branches, redundancies etc...
this is not the only code but is the most representative of the problems faced. your solution will be implemented to other code as well.
thank you very much for your time/trouble
here is the code: (with some NOTES: inserted to help guide you)
protected void Page_Load(object sender, EventArgs e)
{
// cache
//NOTE: i think there is no way around this, and there needs to be no CACHE at the moment
//NOTE: this will problably be reviewed laterm but now it needs to STAY
Response.Cache.SetExpires(
DateTime.N
ow.AddYear
s(-1));
Response.Cache.SetRevalida
tion(HttpC
acheRevali
dation.All
Caches);
Response.Cache.SetNoStore(
);
Response.Cache.SetNoServer
Caching();
Response.Cache.SetCacheabi
lity(HttpC
acheabilit
y.NoCache)
;
// session
// NOTE: can't be helped, it seems (microsoft implementation)
Session["SessionID"] = Session.SessionID.ToString
();
// read query string
string displayEntity = "";
// NOTE: is there a better way to do this??
int i;
if (int.TryParse(Request.Quer
yString["D
isplayEnti
ty"], out i))
{
// get the id
displayEntity = Request.QueryString.Get("D
isplayEnti
ty");
}
else
{
// ERROR: entity not found
Response.Redirect("~/Error
/?Category
=404&Messa
ge=EntityN
otFound");
}
// database select
SqlDataSource hrDataSource = new SqlDataSource();
// connection string
hrDataSource.ConnectionStr
ing = ConfigurationManager.Conne
ctionStrin
gs["hrConn
ectionStri
ng"].ToStr
ing();
hrDataSource.SelectCommand
Type = SqlDataSourceCommandType.S
toredProce
dure;
hrDataSource.SelectCommand
= "selectEntityInformationBy
UID";
// parameters
hrDataSource.SelectParamet
ers.Add("U
ID", displayEntity);
// try?
DataView dv = (DataView)hrDataSource.Sel
ect(DataSo
urceSelect
Arguments.
Empty);
if (dv.Count == 0)
{
// ERROR: entity not found
Response.Redirect("~/Error
/?Category
=404&Messa
ge=EntityN
otFound");
}
// NOTE: can this be done in another way, because in this case there can be only ONE result
// so the for-each is not needed
foreach (DataRowView drv in dv)
{
// TODO: log access to an entity page
// NOTE: not yet implemented (don't worry about this)
// NOTE: is there an easier way to read all of these, rather than doing it manualy in the
// NOTE: hundreds of lines bellow??
string UID = drv["UID"].ToString();
bool ListingService = (bool)drv["ListingService"
];
bool ListingPlusService = (bool)drv["ListingPlusServ
ice"];
bool BusinessService = (bool)drv["BusinessService
"];
// read listing
string NationalID = drv["NationalID"].ToString
();
string ShortName = drv["ShortName"].ToString(
);
string FullName = drv["FullName"].ToString()
;
string ClassificationDesignation = drv["ClassificationDesigna
tion"].ToS
tring();
string StreetAndNumber = drv["StreetAndNumber"].ToS
tring();
string PostalCode = drv["PostalCode"].ToString
();
string MinorPlace = drv["MinorPlace"].ToString
();
string MajorPlace = drv["MajorPlace"].ToString
();
string County = drv["County"].ToString();
string Country = drv["Country"].ToString();
string ContactPhone = drv["ContactPhone"].ToStri
ng();
// read listing plus
string ShortDescription = drv["ShortDescription"].To
String();
string ContactEmail = drv["ContactEmail"].ToStri
ng();
string MainSite = drv["MainSite"].ToString()
;
// read business
string InternalSite = drv["InternalSite"].ToStri
ng();
string SupportPhone = drv["SupportPhone"].ToStri
ng();
string CriticalPhone = drv["CriticalPhone"].ToStr
ing();
string Telefax = drv["Telefax"].ToString();
string MobilePhone = drv["MobilePhone"].ToStrin
g();
string SupportEmail = drv["SupportEmail"].ToStri
ng();
string CriticalEmail = drv["CriticalEmail"].ToStr
ing();
string LocalSite = drv["LocalSite"].ToString(
);
string ContactSite = drv["ContactSite"].ToStrin
g();
string SupportSite = drv["SupportSite"].ToStrin
g();
string CriticalSite = drv["CriticalSite"].ToStri
ng();
string InvestorsSite = drv["InvestorsSite"].ToStr
ing();
string MondayStart1 = drv["MondayStart1"].ToStri
ng();
string MondayEnd1 = drv["MondayEnd1"].ToString
();
string MondayStart2 = drv["MondayStart2"].ToStri
ng();
string MondayEnd2 = drv["MondayEnd2"].ToString
();
string TuesdayStart1 = drv["TuesdayStart1"].ToStr
ing();
string TuesdayEnd1 = drv["TuesdayEnd1"].ToStrin
g();
string TuesdayStart2 = drv["TuesdayStart2"].ToStr
ing();
string TuesdayEnd2 = drv["TuesdayEnd2"].ToStrin
g();
string WednesdayStart1 = drv["WednesdayStart1"].ToS
tring();
string WednesdayEnd1 = drv["WednesdayEnd1"].ToStr
ing();
string WednesdayStart2 = drv["WednesdayStart2"].ToS
tring();
string WednesdayEnd2 = drv["WednesdayEnd2"].ToStr
ing();
string ThursdayStart1 = drv["ThursdayStart1"].ToSt
ring();
string ThursdayEnd1 = drv["ThursdayEnd1"].ToStri
ng();
string ThursdayStart2 = drv["ThursdayStart2"].ToSt
ring();
string ThursdayEnd2 = drv["ThursdayEnd2"].ToStri
ng();
string FridayStart1 = drv["FridayStart1"].ToStri
ng();
string FridayEnd1 = drv["FridayEnd1"].ToString
();
string FridayStart2 = drv["FridayStart2"].ToStri
ng();
string FridayEnd2 = drv["FridayEnd2"].ToString
();
string SaturdayStart1 = drv["SaturdayStart1"].ToSt
ring();
string SaturdayEnd1 = drv["SaturdayEnd1"].ToStri
ng();
string SaturdayStart2 = drv["SaturdayStart2"].ToSt
ring();
string SaturdayEnd2 = drv["SaturdayEnd2"].ToStri
ng();
string SundayStart1 = drv["SundayStart1"].ToStri
ng();
string SundayEnd1 = drv["SundayEnd1"].ToString
();
string SundayStart2 = drv["SundayStart2"].ToStri
ng();
string SundayEnd2 = drv["SundayEnd2"].ToString
();
bool Cash = (bool)drv["Cash"];
bool Checks = (bool)drv["Checks"];
bool AmericanExpress = (bool)drv["AmericanExpress
"];
bool DinersClub = (bool)drv["DinersClub"];
bool MasterCard = (bool)drv["MasterCard"];
bool Visa = (bool)drv["Visa"];
bool Invoice = (bool)drv["Invoice"];
string OtherPayements = drv["OtherPayements"].ToSt
ring();
string BankAccount = drv["BankAccount"].ToStrin
g();
string BankName = drv["BankName"].ToString()
;
// display listing
Page.Title = ShortName + " « Detaljne Informacije « OpenBiz";
EntityHeaderTitleShortName
.Text = ShortName;
Bar_EntityLinkShortName.Te
xt = ShortName;
EntityDetailsFullName.Text
= FullName;
// NOTE: is there a way for the following line to be permanently on the PAGE, not in code
// NOTE: since it does not change, except the DisplayEntity
// NOTE: how to do it?? if possible?
Bar_EntityLink.NavigateUrl
= "~/Entity/?DisplayEntity="
+ UID;
// NOTE: is there a way to bind other stuff too to a certain variable, and not do it in code?
// NOTE: if possible? like the following line.
EntityDetailsNationalID.Te
xt = NationalID;
// NOTE: a better way of doing this, if exists?
// NOTE: generating an address
EntityDetailsAddressStreet
.Text = StreetAndNumber;
if (MinorPlace.Length > 0)
{
EntityDetailsAddressMinorP
lace.Text = MinorPlace;
}
EntityDetailsAddressPostal
CodeAndMaj
orPlace.Te
xt = PostalCode + " " + MajorPlace;
EntityDetailsAddressCounty
.Text = County;
EntityDetailsAddressCountr
y.Text = Country;
if (ContactPhone.Length > 0)
{
EntityDetailsPhoneContactP
anel.Visib
le = true;
EntityDetailsPhoneContact.
Text = ContactPhone;
}
else
{
EntityDetailsPhonePanel.Vi
sible = false;
EntityDetailsPhoneContactP
anel.Visib
le = false;
}
EntityLogoImage.Visible = false;
// display customer
if (ListingService)
{
EntitySideBarMenuEntryInfo
rmationSou
rce.Visibl
e = true;
}
else
{
EntitySideBarMenuEntryInfo
rmationSou
rce.Visibl
e = false;
}
// display listing plus
if (ListingPlusService)
{
EntityDetailsShortDescript
ion.Text = ShortDescription;
EntityDetailsShortDescript
ionPanel.V
isible = true;
if (MainSite.Length > 0)
{
EntityDetailsInternetPanel
.Visible = true;
EntityDetailsInternetMainS
itePanel.V
isible = true;
EntityDetailsInternetMainS
iteLink.Na
vigateUrl = "
http://" + MainSite;
EntityDetailsInternetMainS
ite.Text = MainSite;
}
else
{
EntityDetailsInternetMainS
itePanel.V
isible = false;
EntityDetailsInternetPanel
.Visible = false;
}
if (ContactEmail.Length > 0)
{
EntityDetailsEmailPanel.Vi
sible = true;
EntityDetailsEmailContactP
anel.Visib
le = true;
EntityDetailsEmailContactL
ink.Naviga
teUrl = "mailto:" + ContactEmail;
EntityDetailsEmailContact.
Text = ContactEmail;
}
else
{
EntityDetailsEmailContactP
anel.Visib
le = false;
EntityDetailsEmailPanel.Vi
sible = false;
}
// logo exists?
if (File.Exists(Server.MapPat
h("/Entity
/Images/Lo
go/" + UID + ".jpg")))
{
EntityLogoImage.Visible = true;
EntityLogoImage.ImageUrl = "~/Entity/Images/Logo/" + UID + ".jpg";
EntityLogoImage.ToolTip = ShortName + " Logo";
}
else
{
EntityLogoImage.Visible = false;
}
}
else
{
EntityDetailsShortDescript
ionPanel.V
isible = false;
EntityDetailsInternetPanel
.Visible = false;
EntityDetailsWorkingHoursP
anel.Visib
le = false;
EntityDetailsBankAccountPa
nel.Visibl
e = false;
EntityDetailsEmailPanel.Vi
sible = false;
}
// display business
if (BusinessService)
{
if (InternalSite.Length > 0)
{
EntityDetailsInternetPanel
.Visible = true;
EntityDetailsInternetInter
nalSitePan
el.Visible
= true;
EntityDetailsInternetInter
nalSiteLin
k.Navigate
Url = "
http://" + InternalSite;
EntityDetailsInternetInter
nalSite.Te
xt = InternalSite;
}
else
{
EntityDetailsInternetInter
nalSitePan
el.Visible
= false;
}
if (LocalSite.Length > 0)
{
EntityDetailsInternetPanel
.Visible = true;
EntityDetailsInternetLocal
SitePanel.
Visible = true;
EntityDetailsInternetLocal
SiteLink.N
avigateUrl
= "
http://" + LocalSite;
EntityDetailsInternetLocal
Site.Text = LocalSite;
}
else
{
EntityDetailsInternetLocal
SitePanel.
Visible = false;
}
if (ContactSite.Length > 0)
{
EntityDetailsInternetPanel
.Visible = true;
EntityDetailsInternetConta
ctSitePane
l.Visible = true;
EntityDetailsInternetConta
ctSiteLink
.NavigateU
rl = "
http://" + ContactSite;
EntityDetailsInternetConta
ctSite.Tex
t = LocalSite;
}
else
{
EntityDetailsInternetConta
ctSitePane
l.Visible = false;
}
if (SupportSite.Length > 0)
{
EntityDetailsInternetPanel
.Visible = true;
EntityDetailsInternetSuppo
rtSitePane
l.Visible = true;
EntityDetailsInternetSuppo
rtSiteLink
.NavigateU
rl = "
http://" + SupportSite;
EntityDetailsInternetSuppo
rtSite.Tex
t = LocalSite;
}
else
{
EntityDetailsInternetSuppo
rtSitePane
l.Visible = false;
}
if (CriticalSite.Length > 0)
{
EntityDetailsInternetPanel
.Visible = true;
EntityDetailsInternetCriti
calSitePan
el.Visible
= true;
EntityDetailsInternetCriti
calSiteLin
k.Navigate
Url = "
http://" + CriticalSite;
EntityDetailsInternetCriti
calSite.Te
xt = LocalSite;
}
else
{
EntityDetailsInternetCriti
calSitePan
el.Visible
= false;
}
if (InvestorsSite.Length > 0)
{
EntityDetailsInternetPanel
.Visible = true;
EntityDetailsInternetInves
torsSitePa
nel.Visibl
e = true;
EntityDetailsInternetInves
torsSiteLi
nk.Navigat
eUrl = "
http://" + InvestorsSite;
EntityDetailsInternetInves
torsSite.T
ext = LocalSite;
}
else
{
EntityDetailsInternetInves
torsSitePa
nel.Visibl
e = false;
}
if (MainSite.Length > 0 || InternalSite.Length > 0 || LocalSite.Length > 0 || ContactSite.Length > 0 || SupportSite.Length > 0 || CriticalSite.Length > 0 || InvestorsSite.Length > 0)
{
EntityDetailsInternetPanel
.Visible = true;
}
else
{
EntityDetailsInternetPanel
.Visible = false;
}
if (SupportPhone.Length > 0)
{
EntityDetailsPhoneSupportP
anel.Visib
le = true;
EntityDetailsPhoneSupport.
Text = SupportPhone;
}
else
{
EntityDetailsPhoneSupportP
anel.Visib
le = false;
}
if (CriticalPhone.Length > 0)
{
EntityDetailsPhoneCritical
Panel.Visi
ble = true;
EntityDetailsPhoneCritical
.Text = CriticalPhone;
}
else
{
EntityDetailsPhoneCritical
Panel.Visi
ble = false;
}
if (Telefax.Length > 0)
{
EntityDetailsPhoneTelefaxP
anel.Visib
le = true;
EntityDetailsPhoneTelefax.
Text = Telefax;
}
else
{
EntityDetailsPhoneTelefaxP
anel.Visib
le = false;
}
if (MobilePhone.Length > 0)
{
EntityDetailsPhoneMobilePa
nel.Visibl
e = true;
EntityDetailsPhoneMobile.T
ext = MobilePhone;
}
else
{
EntityDetailsPhoneMobilePa
nel.Visibl
e = false;
}
if (ContactPhone.Length > 0 || SupportPhone.Length > 0 || CriticalPhone.Length > 0 || Telefax.Length > 0 || MobilePhone.Length > 0)
{
EntityDetailsPhonePanel.Vi
sible = true;
}
else
{
EntityDetailsPhonePanel.Vi
sible = false;
}
if (SupportEmail.Length > 0)
{
EntityDetailsEmailPanel.Vi
sible = true;
EntityDetailsEmailSupportP
anel.Visib
le = true;
EntityDetailsEmailSupportL
ink.Naviga
teUrl = "mailto:" + SupportEmail;
EntityDetailsEmailSupport.
Text = SupportEmail;
}
else
{
EntityDetailsEmailSupportP
anel.Visib
le = false;
}
if (CriticalEmail.Length > 0)
{
EntityDetailsEmailPanel.Vi
sible = true;
EntityDetailsEmailCritical
Panel.Visi
ble = true;
EntityDetailsEmailCritical
Link.Navig
ateUrl = "mailto:" + CriticalEmail;
EntityDetailsEmailCritical
.Text = CriticalEmail;
}
else
{
EntityDetailsEmailCritical
Panel.Visi
ble = false;
}
if (ContactEmail.Length > 0 || SupportEmail.Length > 0 || CriticalEmail.Length > 0)
{
EntityDetailsEmailPanel.Vi
sible = true;
}
else
{
EntityDetailsEmailPanel.Vi
sible = false;
}
bool workingHoursNotEmpty = false;
if (MondayStart1.Length > 0 && MondayEnd1.Length > 0)
{
EntityDetailsWorkingHoursM
onday1Pane
l.Visible = true;
EntityDetailsWorkingHoursM
onday1.Tex
t = MondayStart1 + " - " + MondayEnd1;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursM
onday1Pane
l.Visible = true;
}
if (TuesdayStart1.Length > 0 && TuesdayEnd1.Length > 0)
{
EntityDetailsWorkingHoursT
uesday1Pan
el.Visible
= true;
EntityDetailsWorkingHoursT
uesday1.Te
xt = TuesdayStart1 + " - " + TuesdayEnd1;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursT
uesday1Pan
el.Visible
= true;
}
if (WednesdayStart1.Length > 0 && WednesdayEnd1.Length > 0)
{
EntityDetailsWorkingHoursW
ednesday1P
anel.Visib
le = true;
EntityDetailsWorkingHoursW
ednesday1.
Text = WednesdayStart1 + " - " + WednesdayEnd1;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursW
ednesday1P
anel.Visib
le = true;
}
if (ThursdayStart1.Length > 0 && ThursdayEnd1.Length > 0)
{
EntityDetailsWorkingHoursT
hursday1Pa
nel.Visibl
e = true;
EntityDetailsWorkingHoursT
hursday1.T
ext = ThursdayStart1 + " - " + ThursdayEnd1;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursT
hursday1Pa
nel.Visibl
e = true;
}
if (FridayStart1.Length > 0 && FridayEnd1.Length > 0)
{
EntityDetailsWorkingHoursF
riday1Pane
l.Visible = true;
EntityDetailsWorkingHoursF
riday1.Tex
t = FridayStart1 + " - " + FridayEnd1;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursF
riday1Pane
l.Visible = true;
}
if (SaturdayStart1.Length > 0 && SaturdayEnd1.Length > 0)
{
EntityDetailsWorkingHoursS
aturday1Pa
nel.Visibl
e = true;
EntityDetailsWorkingHoursS
aturday1.T
ext = SaturdayStart1 + " - " + SaturdayEnd1;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursS
aturday1Pa
nel.Visibl
e = true;
}
if (SundayStart1.Length > 0 && SundayEnd1.Length > 0)
{
EntityDetailsWorkingHoursS
unday1Pane
l.Visible = true;
EntityDetailsWorkingHoursS
unday1.Tex
t = SundayStart1 + " - " + SundayEnd1;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursS
unday1Pane
l.Visible = true;
}
if (MondayStart2.Length > 0 && MondayEnd2.Length > 0)
{
EntityDetailsWorkingHoursM
onday2Pane
l.Visible = true;
EntityDetailsWorkingHoursM
onday2.Tex
t = MondayStart2 + " - " + MondayEnd2;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursM
onday2Pane
l.Visible = true;
}
if (TuesdayStart2.Length > 0 && TuesdayEnd2.Length > 0)
{
EntityDetailsWorkingHoursT
uesday2Pan
el.Visible
= true;
EntityDetailsWorkingHoursT
uesday2.Te
xt = TuesdayStart2 + " - " + TuesdayEnd2;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursT
uesday2Pan
el.Visible
= true;
}
if (WednesdayStart2.Length > 0 && WednesdayEnd2.Length > 0)
{
EntityDetailsWorkingHoursW
ednesday2P
anel.Visib
le = true;
EntityDetailsWorkingHoursW
ednesday2.
Text = WednesdayStart2 + " - " + WednesdayEnd2;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursW
ednesday2P
anel.Visib
le = true;
}
if (ThursdayStart2.Length > 0 && ThursdayEnd2.Length > 0)
{
EntityDetailsWorkingHoursT
hursday2Pa
nel.Visibl
e = true;
EntityDetailsWorkingHoursT
hursday2.T
ext = ThursdayStart2 + " - " + ThursdayEnd2;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursT
hursday2Pa
nel.Visibl
e = true;
}
if (FridayStart2.Length > 0 && FridayEnd2.Length > 0)
{
EntityDetailsWorkingHoursF
riday2Pane
l.Visible = true;
EntityDetailsWorkingHoursF
riday2.Tex
t = FridayStart2 + " - " + FridayEnd2;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursF
riday2Pane
l.Visible = true;
}
if (SaturdayStart2.Length > 0 && SaturdayEnd2.Length > 0)
{
EntityDetailsWorkingHoursS
aturday2Pa
nel.Visibl
e = true;
EntityDetailsWorkingHoursS
aturday2.T
ext = SaturdayStart2 + " - " + SaturdayEnd2;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursS
aturday2Pa
nel.Visibl
e = true;
}
if (SundayStart2.Length > 0 && SundayEnd2.Length > 0)
{
EntityDetailsWorkingHoursS
unday2Pane
l.Visible = true;
EntityDetailsWorkingHoursS
unday2.Tex
t = SundayStart2 + " - " + SundayEnd2;
workingHoursNotEmpty = true;
}
else
{
EntityDetailsWorkingHoursS
unday2Pane
l.Visible = true;
}
if (workingHoursNotEmpty)
{
EntityDetailsWorkingHoursP
anel.Visib
le = true;
}
else
{
EntityDetailsWorkingHoursP
anel.Visib
le = false;
}
string tempPayment = "";
bool paymentNotEmpty = false;
if (Cash)
{
if (tempPayment.Length > 0)
{
tempPayment = tempPayment + ",";
}
tempPayment = tempPayment + " Gotovina";
paymentNotEmpty = true;
}
if (Checks)
{
if (tempPayment.Length > 0)
{
tempPayment = tempPayment + ",";
}
tempPayment = tempPayment + " ekovi";
paymentNotEmpty = true;
}
if (AmericanExpress)
{
if (tempPayment.Length > 0)
{
tempPayment = tempPayment + ",";
}
tempPayment = tempPayment + " American Express";
paymentNotEmpty = true;
}
if (DinersClub)
{
if (tempPayment.Length > 0)
{
tempPayment = tempPayment + ",";
}
tempPayment = tempPayment + " DinersClub";
paymentNotEmpty = true;
}
if (MasterCard)
{
if (tempPayment.Length > 0)
{
tempPayment = tempPayment + ",";
}
tempPayment = tempPayment + " MasterCard";
paymentNotEmpty = true;
}
if (Visa)
{
if (tempPayment.Length > 0)
{
tempPayment = tempPayment + ",";
}
tempPayment = tempPayment + " Visa";
paymentNotEmpty = true;
}
if (Invoice)
{
if (tempPayment.Length > 0)
{
tempPayment = tempPayment + ",";
}
tempPayment = tempPayment + " Predraun";
paymentNotEmpty = true;
}
if (OtherPayements.Length > 0)
{
if (tempPayment.Length > 0)
{
tempPayment = tempPayment + ",";
}
tempPayment = tempPayment + " " + OtherPayements;
paymentNotEmpty = true;
}
if (paymentNotEmpty)
{
EntityDetailsPaymentOption
sPanel.Vis
ible = true;
}
else
{
EntityDetailsPaymentOption
sPanel.Vis
ible = false;
}
EntityDetailsPayementOptio
ns.Text = tempPayment;
if (BankName.Length > 0 || BankAccount.Length > 0)
{
EntityDetailsBankAccountPa
nel.Visibl
e = true;
EntityDetailsBankAccountNa
me.Text = BankName;
EntityDetailsBankAccountNu
mber.Text = BankAccount;
}