I'm new to C# and can't seem to figure this one out.
In my application I have a code preprocessing module that allows me to manipulate a file (in this case a csv file) before loading it into my application for processing it into my database. One of the files that comes to my application has a column named "Business ID" that I need removed before I move forward in the rest of my preprocessing. There are various files this same code is used to read in and the only difference between them is that this one CSV file has that extra "Business ID" field. It will only need to be removed if the column is present.
Before someone recommends it, the column cannot be removed outside of the program or during the csv generation - it has to be done in this preprocessing.
My code is below - I redacted information in some of the case statements for confidentiality but the remainder of the code is in tact. I appreciate any help y'all can give!
EDIT: Per request here's as much of a snippet as I can provide of the .csv file. This is just the headers. I can't provide an actual record. Hope this is enough for what you wanted to see.
"Transmission Date","Transmission Time","Transaction Code","Client Site","Transaction Date","Site ID","Company","Division","Franchise","Account","Statement Code","House Number","Customer Number","Line Of Business","Download Write Off $","Service $ Write Off","Equipmnt $ Write Off","# of Pieces Rented","Scheme ID","Agency Code","Bill Type Code","Customer Type","Customer Category","VIP Code","First Name","Middle Initial","Last Name","Disconnect Reason","Bill To Name","Billing Addr Line 1","Billing Addr Line 2","Billing Addr Line 3","Billing City","Billing State","Billing Zip Code","Dwnld Cust Stmt Sts","Customer Status","Stop Bill Date","Disconnect Date","Date Last Payment","Last Payment Amt","Subscriber Name","Service Addr Line 1","Service Addr Line 2","Service Addr Line 3","Service City","Service State","Service Zip Code","Dwnld Bal Last Stmt","SS#","DOB","Home Phone","Work Phone","Other Phone","Email","Driver License","Dwnld Disconnect Rsn","Dwnld Disconnct Date","Deposit Date","Total Paid Deposit","Connect Date","Ext Credit Score","Behavioral Score","Management Area","Customer Comment","Cust Business Name","Privacy Code","Business ID"
using System;
using System.IO;
using System.Linq;
using System.Data;
using System.Collections.Generic;
using GSS.Common.Code;
using GSSI.Latitude.Library.DataAccess;
public class CodeProcessor : ICodePreprocessor
{
public string Execute(string fileContents)
{
string resultFile = @"C:\Temp\" + Path.GetFileName(fileContents);
string fileName = Path.GetFileName(fileContents);
string[] nameParts = fileName.Split('_');
string market = nameParts[0].ToString();
string resiCommFlag = "";
if (fileName.Contains("Residential")){
resiCommFlag = "R";
}
if (fileName.Contains("Commercial")) {
resiCommFlag = "C";
}
using (StreamWriter wr = new StreamWriter(resultFile))
{
using (StreamReader sr = new StreamReader(fileContents))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string test1 = line.TrimStart('"');
string[] lineArray = line.Split(',');
string number = "";
string desk = "";
string qlevel = "";
string current1 = "";
string current3 = "";
string trxCode = lineArray[2].Replace("\"","");
string site = lineArray[5].Replace("\"","");
string client = "";
switch (market) {
case "":
switch (site) {
case "3":
client = "";
break;
case "5":
client = "";
break;
default:
client = "";
break;
}
break;
case "":
switch (site) {
case "1":
switch (resiCommFlag) {
case "R":
client = "";
break;
case "C":
client = "";
break;
}
break;
case "2":
switch (resiCommFlag) {
case "R":
client = "";
break;
case "C":
client = "";
break;
}
break;
case "3":
switch (resiCommFlag) {
case "R":
client = "";
break;
case "C":
client = "";
break;
}
break;
case "4":
switch (resiCommFlag) {
case "R":
client = "";
break;
case "C":
client = "";
break;
}
break;
}
break;
case "":
switch (site) {
case "":
switch (resiCommFlag) {
case "R":
client = "";
break;
case "C":
client = "";
break;
}
break;
}
break;
case "":
switch (site) {
case "1":
switch (resiCommFlag) {
case "R":
client = "";
break;
case "C":
client = "";
break;
}
break;
}
break;
case "":
client = "";
break;
default:
client = "";
break;
}
bool acctFound = false;
string account = lineArray[9].Replace("\"","");
string sql = @"select number, desk, qlevel,current1,current3 from master where customer = '" + client +
"' and account = '" + account + "' order by qlevel,received desc";
DataSet ds = null;
try
{
ds = DataManager.GetDataSet(sql, "master");
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) {
number = ds.Tables[0].Rows[0][0].ToString();
desk = ds.Tables[0].Rows[0][1].ToString();
qlevel = ds.Tables[0].Rows[0][2].ToString();
current1 = ds.Tables[0].Rows[0][3].ToString();
current3 = ds.Tables[0].Rows[0][4].ToString();
acctFound = true;
}
}
catch (Exception ex) { throw ex; }
finally {
if (ds != null) {
ds.Dispose();
ds = null;
}
}
bool chgTrx = false;
switch (trxCode) {
case "":
switch (desk) {
case "":
chgTrx = true;
break;
case "":
chgTrx = true;
break;
default:
if (qlevel != "" && qlevel != "") {
chgTrx = false;
} else {
chgTrx = true;
}
break;
}
if (acctFound == false) {
chgTrx = true;
}
break;
case "":
chgTrx = false;
if (acctFound == false) {
chgTrx = true;
}
break;
case "":
chgTrx = false;
if (acctFound == false) {
chgTrx = true;
}
break;
default:
chgTrx = true;
break;
}
if (chgTrx == true) {
trxCode = "";
lineArray[2] = "\""+trxCode+"\"";
}
wr.WriteLine(string.Join(",", lineArray)+","+number+","+desk+","+qlevel+","+current1+","+current3 );
}
}
}
return resultFile;
}
public void Dispose() { }
}