Since I'll be leaving my current company soon it's about time to publish some more WTFs I had to deal with during the 4 years I have been here. This was my first programming job after graduating from Uni, so I wouldn't be too surprised if I ever see some of my code showing up here. Especially the first programs I wrote here are very WTFy and looking back at them I would do MANY things different today.
But hey, at the very least I can say I have never produced code such as THIS:
public partial class Form1 : Form
{
DataSet ds; //DataSet for SQLSrv\Customer\dbo....
DataSetTableAdapters.DSTableAdapter _ds; //Customer-TableAdapter
public Form1()
{
//For calling from table - SQLSrv\Customer\dbo....
ds = new DataSet();
_ds = new Customer.DataSetTableAdapters.DSTableAdapter();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String line, TESTO; //String to read from row
decimal total = 0, RDN = 0, Amount = 0;
int alreadythere = 0, number = 0, right = 0, header = 0, write = 0;
this._ds.Fill(this.ds.CustTable);
// Check file path
DirectoryInfo di = new DirectoryInfo("\\\\path\\to\\file");
foreach (FileInfo fi in di.GetFiles("*.FileEnding", SearchOption.TopDirectoryOnly))
{
textBox1.AppendText("Data record 1" + "\r\n");
textBox2.AppendText("Data record 2" + "\r\n");
alreadythere = 0; right = 0;
try
{
try
{ //Checks if file has been processed or not
for (int i = 0; i <= this.ds.CustTable.Count; i++)
{
if (this.ds.CustTable[i].Datei == fi.ToString())
{
alreadythere = 1; //Is file already there
}
else
{
}
}
}
catch
{
}
if (alreadythere == 1) //File is already there and isn't processed any further
{
textBox1.Clear();
textBox2.Clear();
}
else
{
//Read this area: [area1Begin] to [area1End]
StreamReader preReader1 = new StreamReader(fi.DirectoryName + "\\" + fi.ToString(), Encoding.GetEncoding(1252));
try
{
while ((line = preReader1.ReadLine()) != null)
{
textBox1.AppendText(line.Substring(line.IndexOf("[area1Begin]"), line.IndexOf("[area1End]")) + "\r\n");
right = 1;
}
}
catch
{
textBox1.Clear();
}
//Read this area: [area1End] bis [area1End] (LastIndexOF)
StreamReader preReader2 = new StreamReader(fi.DirectoryName + "\\" + fi.ToString(), Encoding.GetEncoding(1252));
try
{
while ((line = preReader2.ReadLine()) != null)
{
if (line.Substring(0, 7) == "[area1Begin]")
{
textBox2.AppendText(line.Substring(line.IndexOf("[area1End]"), line.LastIndexOf("[area1End]") - line.IndexOf("[area1End]")) + "\r\n");
right = 1;
}
}
}
catch
{
textBox2.Clear();
}
//Read this area: [area2Begin] to [area2Begin] (LastIndexOF)
StreamReader preReader3= new StreamReader(fi.DirectoryName + "\\" + fi.ToString(), Encoding.GetEncoding(1252));
try
{
while ((line = preReader3.ReadLine()) != null)
{
}
}
catch
{
}
//Should file be processed?
if (right == 1)
{
//Save read areas in this path "\\\\path\\to\\file\\file_prepare1.csv"
try
{
if (File.Exists("\\\\path\\to\\file\\file_prepare1.csv"))
{
System.IO.File.Delete("\\\\path\\to\\file\\file_prepare1.csv");
}
else
{
}
StreamWriter file1 = new StreamWriter("\\\\path\\to\\file\\file_prepare1.csv", true, Encoding.Default, 1024);
{
file1.WriteLine(textBox1.Text);
file1.Close();
}
}
catch
{
Application.Exit();
}
//Save read areas in this path "\\\\path\\to\\file\\file_prepare2.csv"
try
{
if (File.Exists("\\\\path\\to\\file\\file_prepare2.csv"))
{
System.IO.File.Delete("\\\\path\\to\\file\\file_prepare2.csv");
}
else
{
}
StreamWriter file1 = new StreamWriter("\\\\path\\to\\file\\file_prepare2.csv", true, Encoding.Default, 1024);
{
file1.WriteLine(textBox2.Text);
file1.Close();
}
}
catch
{
Application.Exit();
}
textBox1.Clear();
textBox2.Clear();
try
{
for (int i2 = 0; i2 <= this.ds.CustTable.Count; i2++)
{
number = i2;
}
}
catch
{
}
textBox1.AppendText("Data record 1" + "\r\n");
DataSet.CustTableRow addrow = this.ds.CustTable.NewCustTableRow();
number = number + 1;
addrow.Nr = number;
addrow.Pfad = fi.DirectoryName;
addrow.Datei = Convert.ToString(fi.ToString());
addrow.File_found_date = DateTime.Now;
//Read buffered areas
try
{
using (CsvReader csv1 = new CsvReader(new StreamReader("\\\\path\\to\\file\\file_prepare1.csv", Encoding.Default), true, ';'))
{
while (csv1.ReadNextRecord())
{
try
{
textBox1.AppendText(csv1[0].Substring(0, csv1[0].IndexOf("ENT-") + 63) + "\r\n");
}
catch
{
textBox1.AppendText(csv1[0].Substring(0, csv1[0].IndexOf("ENT-") + 43) + "\r\n");
}
}
}
}
catch
{
}
[snip... repeat this 27 times, each time with a different starting index for Substring]
//Save read areas in this path "\\\\path\\to\\file\\file_wrapup.csv"
try
{
if (File.Exists("\\\\path\\to\\file\\file_wrapup.csv"))
{
System.IO.File.Delete("\\\\path\\to\\file\\file_wrapup.csv");
}
else
{
}
StreamWriter file1 = new StreamWriter("\\\\path\\to\\file\\file_wrapup.csv", true, Encoding.Default, 1024);
{
file1.WriteLine(textBox1.Text + textBox2.Text);
file1.Close();
}
}
catch
{
Application.Exit();
}
//Read needed data
textBox1.Clear();
textBox2.Clear();
textBox2.Visible = false;
textBox3.Visible = false;
try
{
using (CsvReader csv1 = new CsvReader(new StreamReader("\\\\path\\to\\file\\file_wrapup.csv", Encoding.Default), true, ';'))
{
while (csv1.ReadNextRecord())
{
try
{
if (csv1[0].Substring(csv1[0].IndexOf("000905"), 6) == "000905")
{
textBox1.AppendText(csv1[0].Substring(csv1[0].IndexOf("000905") + 19, 8) + ";" + csv1[0].Substring(csv1[0].IndexOf("000905") + 27, 10) + ";" + "670" + csv1[0].Substring(csv1[0].IndexOf("ELV") + 6, 6) + ";" + csv1[0].Substring(csv1[0].LastIndexOf("ME") - 12, 5) + "." + DateTime.Now.ToString("yyyy") + ";");
Program.naechste = csv1[0].Substring(csv1[0].IndexOf("000905") + 19, 8) + ";" + csv1[0].Substring(csv1[0].IndexOf("000905") + 27, 10);
try
{
if (csv1[0].Substring(csv1[0].IndexOf("SELF") + 14, 5) == "BECAUSE")
{
textBox1.AppendText("because of objection" + ";");
}
else
{
textBox1.AppendText("shortage of cover" + ";");
}
}
catch
{
textBox1.AppendText("shortage of cover" + ";");
}
try
{
Amount = Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("ENT-") - 7, 7));
textBox1.AppendText(Amount + ";");
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message + "\r\nField: Amount");
}
try
{
RDN = Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("FOREIGN") + 5, 5)) + Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("FOREIGN") + 5, 5));
textBox1.AppendText(RDN + ";");
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message + "\r\nField: RDN");
}
try
{
total = Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("ENT-") - 7, 7)) + Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("FOREIGN") + 5, 5)) + Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("FOREIGN") + 5, 5));
textBox1.AppendText(total + ";");
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message + "\r\nField: total");
}
try
{
textBox1.AppendText("670" + csv1[0].Substring(csv1[0].IndexOf("ELV") + 6, 6) + ";");
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message + "\r\nField: TID");
}
try
{
textBox1.AppendText("670" + csv1[0].Substring(csv1[0].IndexOf("ELV") + 6, 6) + "\r\n");
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message + "\r\nField: TID");
}
}
else
{
}
}
catch
{
//other rows
textBox1.AppendText(Program.naechste + ";" + "670" + csv1[0].Substring(csv1[0].IndexOf("ELV") + 6, 6) + ";" + csv1[0].Substring(csv1[0].LastIndexOf("ME") - 12, 5) + "." + DateTime.Now.ToString("yyyy") + ";");
try
{
if (csv1[0].Substring(csv1[0].IndexOf("OWN") + 14, 5) == "REASON")
{
textBox1.AppendText("because of objection" + ";");
}
else
{
textBox1.AppendText("shortage of cover" + ";");
}
}
catch
{
textBox1.AppendText("shortage of cover" + ";");
}
try
{
Amount = Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("ENT-") - 7, 7));
textBox1.AppendText(Amount + ";");
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message + "\r\nField: Amount");
}
try
{
RDN = Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("FOREIGN") + 5, 5)) + Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("FOREIGN") + 5, 5));
textBox1.AppendText(RDN + ";");
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message + "\r\nField: RDN");
}
try
{
total = Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("ENT-") - 7, 7)) + Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("FOREIGN") + 5, 5)) + Convert.ToDecimal(csv1[0].Substring(csv1[0].IndexOf("FOREIGN") + 5, 5));
textBox1.AppendText(total + ";");
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message + "\r\nField: total");
}
try
{
textBox1.AppendText("670" + csv1[0].Substring(csv1[0].IndexOf("ELV") + 6, 6) + ";");
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message + "\r\nField: TID");
}
try
{
textBox1.AppendText("670" + csv1[0].Substring(csv1[0].IndexOf("ELV") + 6, 6) + "\r\n");
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message + "\r\nField: TID");
}
}
}
}
}
catch
{
}
//Save final data
try
{
textBox5.Clear();
if (header == 0)
{
textBox5.AppendText("BIN" + ";" + "Account" + ";" + "TID" + ";" + "Date" + ";" + "Reason of RDN" + ";" + "Amount" + ";" + "RDN" + ";" + "total" + ";" + "TID" + ";" + "TID" + "\r\n");
StreamWriter file1 = new StreamWriter("\\\\path\\to\\file\\File_yesno.csv", true, Encoding.Default, 1024);
{
file1.WriteLine(textBox5.Text + textBox1.Text);
file1.Close();
}
header++;
write = 1;
textBox3.AppendText("_" + fi.ToString());
textBox6.AppendText(fi.ToString() + "<br>");
}
else
{
StreamWriter file1 = new StreamWriter("\\\\path\\to\\file\\File_yesno.csv", true, Encoding.Default, 1024);
{
file1.WriteLine(textBox1.Text);
file1.Close();
}
write = 1;
textBox3.AppendText("_" + fi.ToString());
textBox6.AppendText(fi.ToString() + "<br>");
}
textBox1.Clear();
textBox4.Clear();
textBox5.Clear();
//Save final data - END
}
catch
{
Application.Exit();
}
//Processing of data has been successful - save data to database
this.ds.CustTable.AddMCTabelleRow(addrow); //Ad row
this._ds.Update(this.ds.CustTable); //write to database
}
else
{
}
}
}
catch (Exception EXC)
{
MessageBox.Show(EXC.Message);
}
}
//Customer oder not customer?
if (write == 1)
{
textBox1.Clear();
textBox4.Clear();
textBox5.Clear();
int customerornot = 0, present = 0, present2 = 0;
textBox5.AppendText("Account (C11)" + ";" + "BIN(C10)" + ";" + "Cardnumber" + "\r\n");
textBox4.AppendText("Account (C11)" + ";" + "BIN(C10)" + ";" + "TID" + ";" + "Date" + ";" + "Reason of RDN" + ";" + "Amount" + ";" + "RDN" + ";" + "total" + ";" + "TID" + ";" + "TID" + "\r\n");
using (CsvReader csv1 = new CsvReader(new StreamReader("\\\\path\\to\\file\\File_yesno.csv", Encoding.Default), true, ';'))
{
while (csv1.ReadNextRecord())
{
customerornot = 0;
using (CsvReader csv2 = new CsvReader(new StreamReader("\\\\path\\to\\file\\Terminalbestand Cruse 24.06 2011.csv", Encoding.Default), true, ';'))
{
while (csv2.ReadNextRecord())
{
if ((csv1[1].ToString().Length > 2) && (csv2[0].ToString().Length > 2))
{
string a = csv1[2];
string b = csv2[0];
if (csv1[2] == csv2[0])
{
customerornot = 1;
present = 1;
textBox4.AppendText(csv1[1] + ";" + csv1[0] + ";" + csv1[2] + ";" + csv1[3] + ";" + csv1[4] + ";" + csv1[5] + ";" + csv1[6] + ";" + csv1[7] + ";" + csv1[8] + ";" + csv1[9] + "\r\n");
}
}
}
if (customerornot == 1)
{
}
else
{
textBox5.AppendText(csv1[1] + ";" + csv1[0] + ";" + "99" + "\r\n");
customerornot = 1;
present2 = 1;
}
}
}
if (present == 1)
{
StreamWriter file2 = new StreamWriter("\\\\path\\to\\file\\Created by Customer\\Customer_(" + textBox3.Text + ")_.csv", true, Encoding.Default, 1024);
{
file2.WriteLine(textBox4.Text);
file2.Close();
}
}
if (present2 == 1)
{
StreamWriter file3 = new StreamWriter("\\\\path\\to\\file\\Created by Customer\\NotCustomer_(" + textBox3.Text + ")_.csv", true, Encoding.Default, 1024);
{
file3.WriteLine(textBox5.Text);
file3.Close();
}
}
//Send Message
try
{
[snip ... Code to send an email, clearly copied from some tutorial]
}
catch (Exception ee)
{
Console.WriteLine("{0} Exception caught.", ee);
}
//End Message
}
}
//Customer or not customer? - END
[snip... delete all the files we have created here. Of course with hard coded paths]
Application.Exit();
}
}
I had to snip some of the code du to character limit. The original program is 1220 lines long. Including unused namespaces such as "using System.IO".
A breakdown of the program:
-
Flags declared as int, because bool just isn't trustworthy.
-
We check if a file has already been processed not by a simple SQL statement where we pass the file name as a parameter, no. We iterate through each row in the table, checking it against the file name. And if we have indeed found the file name in the table, we don't stop there. In fact, we run into an IndexOutOfRangeException every time we check a file name (because of "<= Table.Count" as opposed to "< Table.Count"). And since we check all the files in a folder with as many as 10,000 files in it, we run into A LOT OF EXCEPTIONS HERE!!! But don't worry, it's been taken care of by an empty catch block. We have a lot of empty catch blocks here, so get used to it...
-
When processing the file, we don't iterate through all the lines only once, but 3 times. The third time we don't do anything at all, but we still go through the whole file again. Also note how how in case of an exception the flag "right" is still set to 1, but the textBoxes are cleared at least.
-
Using textBoxes like variables. Storing the stuff we read from the file in them, reading from the textBoxes again to save it in a different file. How many textBoxes are there? 6. All convieniently named "textBox1" through "textBox6".
-
My favorite, because the same developer has done the same thing in a different program: Determine how many lines are in a table by using a for-loop with the number of lines in the table used as break condition. This is pure genius and a good way to make sure the CPU doesn't get bored.
-
Using 28 CsvReaders to read the stuff we wrote to a file back into the textBoxes, instead of maybe writing a small method for this kind of stuff. Omitted from the code due to problems with the character limit.
-
Juggling data between the 6 textBoxes and writing everything to a file again.
-
Last but not least, everything happens in "Form_Load". Makes you wonder why this isn't just a console application. But on the other hand, a console application doesn't have a GUI, so you cannot use textBoxes as some kind of buffer.
-
As a bonus, we have many if-else statements like this:
if(condition)
{
doStuff();
}
else
{
}
And sometimes even
if(condition)
{
}
else
{
doStuff();
}
Oh and the messed up indentation at "if (right == 1)" is from the original program, I didn't bother to correct it. Deal with it. :P