PDA

View Full Version : My nightmare before I have 10 days off from work


WiGgLr
April 5th, 2007, 07:31 AM
So, today is my last day, but before I go I have to edit some code written by someone a couple of years ago.

That someone didn't understand datasets, so he didn't use them. He also didn't understand the concept of commenting code.

This is just his Page_Load:


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Cls.clsSecurity.fCheck();

try{this.viRecordID = Convert.ToInt32(Request.QueryString["id"]);}catch{}
try{this.viMediaCodeID = Convert.ToInt32(Request.QueryString["mcid"]);}catch{}
this.vStrPostcode = Request.QueryString["pc"];

if(Page.IsPostBack)
{
XmlDocument objXDRecordMap = new XmlDocument();
objXDRecordMap.Load(string.Format("{0}HardFiles\\VWPwfRecordMap.xml", System.Configuration.ConfigurationSettings.AppSett ings["RootDir"]));

XmlDocument objXDNewRecord = new XmlDocument();
objXDNewRecord.LoadXml("<CustomerRecord/>");


foreach(XmlElement elmX in objXDRecordMap.DocumentElement)
{
string vStrElmName = elmX.GetAttribute("name");

XmlElement elmNew = objXDNewRecord.CreateElement(vStrElmName);
objXDNewRecord.DocumentElement.AppendChild(elmNew) ;

string vStrObtain = elmX.GetAttribute("obtain");
if(vStrObtain.Equals("form"))
{
string vStrGetValue = Request.Form[vStrElmName];
if(elmX.GetAttribute("type").Equals("string"))
{
vStrGetValue = Cls.clsStuff.fFormatText(vStrGetValue);
}
elmNew.InnerText = vStrGetValue.Trim();
}
else if(vStrObtain.Equals("default"))
{
elmNew.InnerText = elmX.InnerText;
}
else
{
switch(vStrElmName)
{
case "DateTimeAdded":
elmNew.InnerText = DateTime.Now.ToString("s", System.Globalization.DateTimeFormatInfo.InvariantI nfo);
break;
case "MediaCode":
elmNew.InnerText = this.fGetMediaCode();
break;
case "PAFOutcode":
elmNew.InnerText = this.fGetPostcode(0);
break;
case "PAFIncode":
elmNew.InnerText = this.fGetPostcode(1);
break;
case "VehicleReplacementDate":
int viMonth = Convert.ToInt32(this.sltCVRDMonth.Value);
int viYear = Convert.ToInt32(this.sltCVRDYear.Value);
if(viMonth != 0 && viYear != 0)
{
DateTime dtDT = new DateTime(viYear, viMonth, 1, 0, 0, 0, 0);
elmNew.InnerText = dtDT.ToString("s", System.Globalization.DateTimeFormatInfo.InvariantI nfo);
}
break;
case "FreeChoice":
if(this.FreeChoice.Checked)
{
elmNew.InnerText = "Y";
}
break;
case "BrochureString":
string vStrBrochureString = string.Empty;
int viXMax = 0;
foreach(ListItem objListItem in this.cbxListBrochureCodes.Items)
{
if(objListItem.Selected)
{
vStrBrochureString += string.Format("{0}!", objListItem.Value.Trim().PadLeft(3, '0'));
viXMax++;
if(viXMax.Equals(25))
{
break;
}
}
}
elmNew.InnerText = vStrBrochureString;
break;
case "RequireTestDrive1":
if(this.RequireTestDrive1.Checked)
{
elmNew.InnerText = "Y";
}
break;
case "TestDriveModel1":
if(this.RequireTestDrive1.Checked)
{
elmNew.InnerText = this.TestDriveModel1.SelectedValue;
}
break;
case "TestDriveDerivative1":
if(this.RequireTestDrive1.Checked)
{
elmNew.InnerText = this.TestDriveDerivative1.Text;
}
break;
case "TestDrivePreferredDate1":
if(this.RequireTestDrive1.Checked)
{
int viTD1Day = Convert.ToInt32(this.sltTD1Day.Value); if(viTD1Day.Equals(0)){viTD1Day = 1;}
int viTD1Month = Convert.ToInt32(this.sltTD1Month.Value);
int viTD1Year = Convert.ToInt32(this.sltTD1Year.Value);
if(viTD1Month != 0 && viTD1Year != 0)
{
DateTime dtTD1 = new DateTime(viTD1Year, viTD1Month, viTD1Day, 0, 0, 0);
elmNew.InnerText = dtTD1.ToString("s", System.Globalization.DateTimeFormatInfo.InvariantI nfo);
}
}
break;
case "TestDrivePreferredtimeOfDay1":
if(this.RequireTestDrive1.Checked)
{
elmNew.InnerText = this.TestDrivePreferredtimeOfDay1.Value;
}
break;
case "RequireTestDrive2":
if(this.RequireTestDrive2.Checked)
{
elmNew.InnerText = "Y";
}
break;
case "TestDriveModel2":
if(this.RequireTestDrive2.Checked)
{
elmNew.InnerText = this.TestDriveModel2.SelectedValue;
}
break;
case "TestDriveDerivative2":
if(this.RequireTestDrive2.Checked)
{
elmNew.InnerText = this.TestDriveDerivative2.Text;
}
break;
case "TestDrivePreferredDate2":
if(this.RequireTestDrive2.Checked)
{
int viTD2Day = Convert.ToInt32(this.sltTD2Day.Value); if(viTD2Day.Equals(0)){viTD2Day = 1;}
int viTD2Month = Convert.ToInt32(this.sltTD2Month.Value);
int viTD2Year = Convert.ToInt32(this.sltTD2Year.Value);
if(viTD2Month != 0 && viTD2Year != 0)
{
DateTime dtTD2 = new DateTime(viTD2Year, viTD2Month, viTD2Day, 0, 0, 0);
elmNew.InnerText = dtTD2.ToString("s", System.Globalization.DateTimeFormatInfo.InvariantI nfo);
}
}
break;
case "TestDrivePreferredtimeOfDay2":
if(this.RequireTestDrive2.Checked)
{
elmNew.InnerText = this.TestDrivePreferredtimeOfDay2.Value;
}
break;
case "CompetitionEntry":
elmNew.InnerText = this.CompetitionEntry.Checked.ToString().ToLower() ;
break;
}
}
}

XmlDocument objTestXD = new XmlDocument();
objTestXD.LoadXml(objXDNewRecord.OuterXml);

foreach(XmlElement elmVal in objXDRecordMap.DocumentElement)
{
XmlElement elmTest = (XmlElement)objTestXD.DocumentElement.SelectSingle Node(elmVal.GetAttribute("name"));
if(elmTest.InnerText.Trim().Equals("") && elmVal.GetAttribute("minOccurs").Equals("0"))
{
objTestXD.DocumentElement.RemoveChild(elmTest);
}
}

bool vbIsXmlValErr = false;
Cls.clsXmlValidate objXmlVal = new WebAppVWCoupons.Cls.clsXmlValidate();
objXmlVal.Validate(objTestXD.OuterXml, string.Format("{0}\\HardFiles\\VWPXD.xsd", System.Configuration.ConfigurationSettings.AppSett ings["RootDir"]));
if(objXmlVal.vdValidated)
{
objTestXD.DocumentElement.SetAttribute("val", "1");
}
else
{
XmlDocument errXD = new XmlDocument();
errXD.LoadXml(string.Format("<root>{0}</root>", objXmlVal.vStrErrors));
foreach(XmlElement elmError in errXD.DocumentElement)
{
string[] vStrErr = elmError.InnerText.Split('\'');
string vStrElmErrName = vStrErr[1].Trim();
XmlElement elmErrorIn = (XmlElement)objTestXD.DocumentElement.SelectSingle Node(vStrElmErrName);
try
{
elmErrorIn.SetAttribute("val", "0");
}
catch
{
//Response.Write(vStrElmErrName + "<br/>");
}
}

objTestXD.DocumentElement.SetAttribute("val", "0");
if(!vbIsXmlValErr){vbIsXmlValErr = true;}
}

//Response.Clear();
//Response.ContentType = "text/xml";
//Response.Write(objTestXD.OuterXml);
//Response.End();

if(vbIsXmlValErr)
{
string vStrErrMsg = string.Empty;
foreach(XmlElement elmX in objTestXD.DocumentElement)
{
if(elmX.GetAttribute("val") == "0")
{
string vStrElmName = elmX.Name;
XmlElement elmMap = (XmlElement)objXDRecordMap.DocumentElement.SelectS ingleNode(string.Format("element[@name = \"{0}\"]", vStrElmName));
if(elmMap.GetAttribute("errName") != string.Empty){vStrElmName = elmMap.GetAttribute("errName");}
vStrErrMsg += string.Format("<br/>{0}", vStrElmName);
}
}
this.trEx01.Style["display"] = string.Empty;
this.trEx01.Cells[0].InnerHtml = "<strong>Exception in:</strong>" + vStrErrMsg + "<br/><br/>" + objXmlVal.ErrorMessage.Replace(").", ").<br/>");
}
else
{
Cls.clsSaveNewPRecord objSaveRecord = new WebAppVWCoupons.Cls.clsSaveNewPRecord();
if(this.viRecordID.Equals(0))
{
objSaveRecord.fSaveRecord(objXDNewRecord, this.viMediaCodeID, (int)Session["uid"], this.MailingURN.Text.Trim());
}
else
{
Cls.clsLoadPXml objLoadXml = new WebAppVWCoupons.Cls.clsLoadPXml();
XmlDocument objLoadXD = objLoadXml.fLoadXmlDoc(this.viRecordID);
objLoadXml = null;

if(this.cbxDelete.Checked)
{
objSaveRecord.fDeleteRecord(this.viRecordID, (int)Session["uid"]);
}
else if(this.cbxUpdate.Checked || objLoadXD.DocumentElement.GetAttribute("imported").Equals("true"))
{
objSaveRecord.fUpdateRecord(objXDNewRecord, this.viMediaCodeID, (int)Session["uid"], this.viRecordID, this.MailingURN.Text.Trim());
}
else
{
System.Text.RegularExpressions.Regex objRXSkipCheck = new System.Text.RegularExpressions.Regex("RecordID|DateTimeAdded");

int viCountChanges = 0;
foreach(XmlElement elmOriginal in objLoadXD.DocumentElement)
{
string vStrElmOName = elmOriginal.Name;
XmlElement elmUpdated = (XmlElement)objXDNewRecord.DocumentElement.SelectS ingleNode(vStrElmOName);
string vStrValue00 = elmOriginal.InnerText.Trim().ToLower();
string vStrValue01 = elmUpdated.InnerText.Trim().ToLower();
if((!vStrValue00.Equals(vStrValue01)) && !objRXSkipCheck.IsMatch(vStrElmOName))
{
//Response.Write(string.Format("{2}: {0} = {1}<br/>", vStrValue00, vStrValue01, vStrElmOName));
viCountChanges++;
}

if(vStrElmOName.Equals("MediaCode"))
{
if(!elmUpdated.GetAttribute("MailingURN").Equals(this.MailingURN.Text.Trim()))
{
viCountChanges++;
}
}
}

//Response.Clear();
//Response.Write(viCountChanges.ToString());
//Response.End();

if(viCountChanges > 0)
{
objSaveRecord.fUpdateRecord(objXDNewRecord, this.viMediaCodeID, (int)Session["uid"], this.viRecordID, this.MailingURN.Text.Trim());
}
}
}
objSaveRecord = null;
}

objXDRecordMap = null;
objXDNewRecord = null;
objTestXD = null;
objXmlVal.ErrorMessage = "";
objXmlVal.vdValidated = false;
objXmlVal.vStrErrors = "";
objXmlVal.fDispose();
objXmlVal = null;

if(!vbIsXmlValErr && !this.cbxStay.Checked)
{
Response.Redirect("wfHome.aspx", true);
}

}
else
{
this.tdShowUser.InnerHtml = string.Format("User: <strong>{0}</strong>", Session["fullName"]);

this.fLoadMediaCodes();
this.fRenderBrocureCkeckBoxList();
this.fSetTestDriveModelsLists();
this.fSetDates();

this.lblRecordID.Text = this.viRecordID.ToString();
try{this.MediaCode.Items.FindByValue(this.viMediaC odeID.ToString()).Selected = true;}
catch{}
this.txtPostcode.Text = this.vStrPostcode;

if(this.viRecordID != 0)
{
Cls.clsLoadPXml objLoadXml = new WebAppVWCoupons.Cls.clsLoadPXml();
XmlDocument objLoadXD = objLoadXml.fLoadXmlDoc(this.viRecordID);
objLoadXml = null;

//Response.Clear();
//Response.ContentType = "text/xml";
//Response.Write(objLoadXD.OuterXml);
//Response.End();

string vStrSkip = "RecordID|DateTimeAdded";
foreach(XmlElement elmLoad in objLoadXD.DocumentElement)
{
string vStrElmName = elmLoad.Name.Trim();
string vStrElmValue = elmLoad.InnerText.Trim();
if(vStrElmName.Equals("MediaCode"))
{
if(this.MediaCode.SelectedIndex >= 0)
{
this.MediaCode.Items[this.MediaCode.SelectedIndex].Selected = false;
}
if(this.viMediaCodeID.Equals(0))
{
try{this.MediaCode.Items.FindByValue(elmLoad.GetAt tribute("mcid")).Selected = true;}
catch{}
}
else
{
try{this.MediaCode.Items.FindByValue(this.viMediaC odeID.ToString()).Selected = true;}
catch{}
}

//this.MediaCode.Text = vStrElmValue;
this.MailingURN.Text = elmLoad.GetAttribute("MailingURN");
}
else if(vStrElmName.Equals("BrochureString"))
{
string[] vStrAryBrochureString = vStrElmValue.Split('!');
foreach(string vStrBrochureString in vStrAryBrochureString)
{
if(vStrBrochureString.Trim() != string.Empty)
{
foreach(ListItem objLI in this.cbxListBrochureCodes.Items)
{
if(objLI.Value.Equals(vStrBrochureString.Trim()))
{
objLI.Selected = true; break;
}
}
}
}
}
else if(vStrElmName.Equals("VehicleReplacementDate") && vStrElmValue != "")
{
try
{
DateTime dtVRD = Convert.ToDateTime(vStrElmValue);
if(this.sltCVRDMonth.SelectedIndex >= 0)
{
this.sltCVRDMonth.Items[this.sltCVRDMonth.SelectedIndex].Selected = false;
}
try{this.sltCVRDMonth.Items.FindByValue(dtVRD.Mont h.ToString()).Selected = true;}
catch{}

if(this.sltCVRDYear.SelectedIndex >= 0)
{
this.sltCVRDYear.Items[this.sltCVRDYear.SelectedIndex].Selected = false;
}
try{this.sltCVRDYear.Items.FindByValue(dtVRD.Year. ToString()).Selected = true;}
catch{}
}
catch
{
}
}
else if(vStrElmName.Equals("TestDrivePreferredDate1") && vStrElmValue != "")
{
try
{
DateTime dtTDPD1 = Convert.ToDateTime(vStrElmValue);
if(this.sltTD1Day.SelectedIndex >= 0)
{
this.sltTD1Day.Items[this.sltTD1Day.SelectedIndex].Selected = false;
}
try{this.sltTD1Day.Items.FindByValue(dtTDPD1.Day.T oString()).Selected = true;}
catch{}

if(this.sltTD1Month.SelectedIndex >= 0)
{
this.sltTD1Month.Items[this.sltTD1Month.SelectedIndex].Selected = false;
}
try{this.sltTD1Month.Items.FindByValue(dtTDPD1.Mon th.ToString()).Selected = true;}
catch{}

if(this.sltTD1Year.SelectedIndex >= 0)
{
this.sltTD1Year.Items[this.sltTD1Year.SelectedIndex].Selected = false;
}
try{this.sltTD1Year.Items.FindByValue(dtTDPD1.Year .ToString()).Selected = true;}
catch{}
}
catch
{
}
}
else if(vStrElmName.Equals("TestDrivePreferredDate2") && vStrElmValue != "")
{
try
{
DateTime dtTDPD2 = Convert.ToDateTime(vStrElmValue);
if(this.sltTD2Day.SelectedIndex >= 0)
{
this.sltTD2Day.Items[this.sltTD2Day.SelectedIndex].Selected = false;
}
try{this.sltTD2Day.Items.FindByValue(dtTDPD2.Day.T oString()).Selected = true;}
catch{}

if(this.sltTD2Month.SelectedIndex >= 0)
{
this.sltTD2Month.Items[this.sltTD2Month.SelectedIndex].Selected = false;
}
try{this.sltTD2Month.Items.FindByValue(dtTDPD2.Mon th.ToString()).Selected = true;}
catch{}

if(this.sltTD2Year.SelectedIndex >= 0)
{
this.sltTD2Year.Items[this.sltTD2Year.SelectedIndex].Selected = false;
}
try{this.sltTD2Year.Items.FindByValue(dtTDPD2.Year .ToString()).Selected = true;}
catch{}
}
catch
{
}
}
else if(vStrElmName.IndexOf(vStrSkip).Equals(-1))
{
foreach(System.Web.UI.Control objControl in this.Controls)
{
foreach(System.Web.UI.Control objSubControl in objControl.Controls)
{
string vStrControlID = objSubControl.ID;
string vStrControlType = objSubControl.GetType().ToString();
if(vStrControlType.Equals("System.Web.UI.WebControls.TextBox") && vStrControlID.Equals(vStrElmName))
{
System.Web.UI.WebControls.TextBox objTxtTemp = (System.Web.UI.WebControls.TextBox)objSubControl;
objTxtTemp.Text = vStrElmValue;
break;
}
else if(vStrControlType.Equals("System.Web.UI.HtmlControls.HtmlSelect") && vStrControlID.Equals(vStrElmName) && vStrElmValue != string.Empty)
{
System.Web.UI.HtmlControls.HtmlSelect objSelectTemp = (System.Web.UI.HtmlControls.HtmlSelect)objSubContr ol;
if(objSelectTemp.SelectedIndex >= 0)
{
objSelectTemp.Items[objSelectTemp.SelectedIndex].Selected = false;
try{objSelectTemp.Items.FindByValue(vStrElmValue). Selected = true;}
catch{}
}
break;
}
else if(vStrControlType.Equals("System.Web.UI.WebControls.CheckBox") && vStrControlID.Equals(vStrElmName))
{
if(vStrElmValue.Equals("Y") || vStrElmValue.ToLower().Equals("true"))
{
System.Web.UI.WebControls.CheckBox objCbxTemp = (System.Web.UI.WebControls.CheckBox)objSubControl;
objCbxTemp.Checked = true;
}
break;
}
else if(vStrControlType.Equals("System.Web.UI.WebControls.DropDownList") && vStrControlID.Equals(vStrElmName))
{
System.Web.UI.WebControls.DropDownList objDDLTemp = (System.Web.UI.WebControls.DropDownList)objSubCont rol;
try{objDDLTemp.Items.FindByValue(vStrElmValue).Sel ected = true;}
catch{}
break;
}
}
}
}
}

if((int)Session["AccessLevel"] > 0)
{
if(objLoadXD.DocumentElement.GetAttribute("exported").Equals("true"))
{
this.cbxUpdate.Enabled = true;
}
else
{
this.cbxUpdate.Enabled = false;
}
this.cbxDelete.Enabled = true;
}
//else if(objLoadXD.DocumentElement.GetAttribute("cuid").Equals(Session["uid"].ToString()))
//{
//this.cbxUpdate.Enabled = true;
//}
else
{
this.cbxUpdate.Enabled = false;
this.cbxDelete.Enabled = false;
this.cbxStay.Visible = true;
}
}
else
{
this.cbxUpdate.Enabled = false;
this.cbxDelete.Enabled = false;
this.cbxStay.Visible = false;
}
}

if((int)Session["AccessLevel"] > 0)
{
this.MediaCode.Disabled = false;
}
else
{
this.MediaCode.Disabled = true;
}

this.fMakeBrocureModelsJavaArray();

string vStrNav = Request.QueryString["nav"];
if(vStrNav != null)
{
this.fSetNavigate(vStrNav);
this.cbxStay.Checked = true;
}
else
{
this.spNav.Visible = false;
}
}


To top it off, it's written for .net 1.1 framework, so VS2003. I really don't want to upgrade it to VS2005 because I'd probably have to fix a shed load of obsolete code all over the fucking place.

So far, VS2003 has crashed about 5 or 6 times for me while I edit this code.

The person who wrote this code is a motherfucker

Grog
April 5th, 2007, 07:34 AM
you have nightmares about this stuff?

CharlieM
April 5th, 2007, 07:35 AM
Have fun with that.

stockfisch
April 5th, 2007, 07:50 AM
yikes

Archaos
April 5th, 2007, 08:03 AM
I have no idea what that means.

LinnEva
April 5th, 2007, 08:12 AM
it was me, eat it bitch

Ist
April 5th, 2007, 09:38 AM
You poor soul

WiGgLr
April 5th, 2007, 10:51 AM
YAY, I sorted it...

it was actually quite clever what the guy had done, but ultimately pointless...

now I know how to pad what can be done in 100 lines of code out to 1000 though, hehehe



at least I don't have to stay late

CharlieM
April 5th, 2007, 01:51 PM
YAY, I sorted it...

it was actually quite clever what the guy had done, but ultimately pointless...

now I know how to pad what can be done in 100 lines of code out to 1000 though, hehehe



at least I don't have to stay late

Cant be assed tracing through his code, but what did he do to save monstrous ammounts of code ?

WiGgLr
April 5th, 2007, 04:03 PM
Cant be assed tracing through his code, but what did he do to save monstrous ammounts of code ?

He had his control names on the main ASPX form match up with the field names in the table that they were going into, with an XML file which served as a schema (it wasn't a real one, but almost). This XML mentioned the datatype of each field and whether it should be picked up from "Form" or "Other"

He then loaded that XML file into an XMLReader, then looped through each field (or XML element). If the XML said that it came from the Form, he used the Request object to request the value from the main form POST). If it was Other, then he derived the value using a case statement.

All of these values would be put into another XMLDocument (in memory), which would be checked against a real XMLSchema (this time provided by the client of the project). If it was all ok, then this XMLDocument waas passed to another class which eventually opened a copy of the destination table into an un-typed dataset, then looped through each element in the XMLDocument to populate a new row before sending the update to the database.

This didn't really save much code, I could have used far less. If there were likely to be a lot of changes going forward, I would re-write the whole thing. There will only be one or two changes a year though so I've written a "how to make this kind of change" document until I've got time to re-visit the project. If it aint broke and all that...