This is what i written
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using BL;
namespace ACHWinapp
{
public partial class frmNewAch : Form
{
BL.ACHFileheader objBL = new ACHFileheader();
BL.ACHBatchHeader objBatchHeader = new ACHBatchHeader();
BL.PPDEntryDetail objppd = new PPDEntryDetail();
string[] strFileEntries;
String FileName = string.Empty;
string strFiletype;
TreeNodeCollection myNodeCollection;
string Node;
string strFilePath = string.Empty;
string strDate = DateTime.Now.ToShortDateString();
bool sflag = false;
Stream myStream = Stream.Null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
public frmNewAch()
{
InitializeComponent();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (treeViewACH.SelectedNode.Parent != null)
{
string strSwitch = treeViewACH.SelectedNode.Parent.Text;
switch (strSwitch)
{
case "FileHeader":
{
Node = treeViewACH.SelectedNode.Text;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH\\" + Node;
if (File.Exists(strFilePath))
{
pnlACH.Visible = true;
pnlACH.Controls.Clear();
treeViewACH.SelectedNode.ToolTipText = strFilePath;
datagridFileHeader.Visible = true;
dataGridAddenda.Visible = false;
datagridBatchHeader.Visible = false;
datagridEntryDetails.Visible = false;
StreamReader sr = new StreamReader(strFilePath);
string s = sr.ReadLine();
datagridFileHeader.Rows[0].Cells[0].Value = s.Substring(0, 1);
datagridFileHeader.Rows[0].Cells[1].Value = s.Substring(1, 2).Trim();
datagridFileHeader.Rows[0].Cells[2].Value = s.Substring(3, 10).Trim();
datagridFileHeader.Rows[0].Cells[3].Value = s.Substring(13, 10);
datagridFileHeader.Rows[0].Cells[4].Value = s.Substring(23, 6).Trim();
datagridFileHeader.Rows[0].Cells[5].Value = s.Substring(29, 4).Trim();
datagridFileHeader.Rows[0].Cells[6].Value = s.Substring(33, 1);
datagridFileHeader.Rows[0].Cells[7].Value = s.Substring(34, 3).Trim();
datagridFileHeader.Rows[0].Cells[8].Value = s.Substring(37, 2).Trim();
datagridFileHeader.Rows[0].Cells[9].Value = s.Substring(39, 1);
datagridFileHeader.Rows[0].Cells[10].Value = s.Substring(40, 23).Trim();
datagridFileHeader.Rows[0].Cells[11].Value = s.Substring(63, 23).Trim();
datagridFileHeader.Rows[0].Cells[12].Value = s.Substring(86, 8).Trim();
}
break;
}
case "BatchHeader":
{
Node = treeViewACH.SelectedNode.Text;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH\\" + Node;
if (File.Exists(strFilePath))
{
pnlACH.Visible = true;
pnlACH.Controls.Clear();
treeViewACH.SelectedNode.ToolTipText = strFilePath;
datagridFileHeader.Visible = false;
dataGridAddenda.Visible = false;
datagridBatchHeader.Visible = true;
datagridEntryDetails.Visible = false;
StreamReader sr = new StreamReader(strFilePath);
string s = sr.ReadLine();
datagridBatchHeader.Rows[0].Cells[0].Value = s.Substring(0, 1);
datagridBatchHeader.Rows[0].Cells[1].Value = s.Substring(1, 3).Trim();
datagridBatchHeader.Rows[0].Cells[2].Value = s.Substring(4, 16).Trim();
datagridBatchHeader.Rows[0].Cells[3].Value = s.Substring(20, 20);
datagridBatchHeader.Rows[0].Cells[4].Value = s.Substring(40, 10).Trim();
datagridBatchHeader.Rows[0].Cells[5].Value = s.Substring(50, 3).Trim();
datagridBatchHeader.Rows[0].Cells[6].Value = s.Substring(53, 10);
datagridBatchHeader.Rows[0].Cells[7].Value = s.Substring(63, 6).Trim();
datagridBatchHeader.Rows[0].Cells[8].Value = s.Substring(69, 6).Trim();
//datagridBatchHeader.Rows[0].Cells[9].Value = s.Substring(75, 3);
datagridBatchHeader.Rows[0].Cells[10].Value = s.Substring(78, 1).Trim();
datagridBatchHeader.Rows[0].Cells[11].Value = s.Substring(79, 8).Trim();
datagridBatchHeader.Rows[0].Cells[12].Value = s.Substring(87, 7).Trim();
}
break;
}
case "PPD":
{
Node = treeViewACH.SelectedNode.Text;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH\\" + Node;
treeViewACH.SelectedNode.ToolTipText = strFilePath;
if (File.Exists(strFilePath))
{
pnlACH.Visible = true;
pnlACH.Controls.Clear();
datagridFileHeader.Visible = false;
datagridBatchHeader.Visible = false;
datagridEntryDetails.Visible = true;
dataGridAddenda.Visible = true;
StreamReader sr = new StreamReader(strFilePath);
string s = sr.ReadLine();
int cnt = 0;
while (s != null)
{
if ((s.Substring(0, 1)) == "6")
{
datagridEntryDetails.Rows[0].Cells[0].Value = s.Substring(0, 1);
datagridEntryDetails.Rows[0].Cells[1].Value = s.Substring(1, 2).Trim();
datagridEntryDetails.Rows[0].Cells[2].Value = s.Substring(3, 8).Trim();
datagridEntryDetails.Rows[0].Cells[3].Value = s.Substring(11, 1);
datagridEntryDetails.Rows[0].Cells[4].Value = s.Substring(12, 17).Trim();
datagridEntryDetails.Rows[0].Cells[5].Value = s.Substring(29, 10).Trim();
datagridEntryDetails.Columns[6].HeaderText = "IndividualIdentificationNumber";
datagridEntryDetails.Rows[0].Cells[6].Value = s.Substring(39, 15);
datagridEntryDetails.Columns[7].HeaderText = "IndividualName";
datagridEntryDetails.Rows[0].Cells[7].Value = s.Substring(54, 22).Trim();
datagridEntryDetails.Rows[0].Cells[8].Value = s.Substring(76, 2).Trim();
datagridEntryDetails.Rows[0].Cells[9].Value = s.Substring(78, 1);
datagridEntryDetails.Rows[0].Cells[10].Value = s.Substring(79, 15).Trim();
}
if ((s.Substring(0, 1)) == "7")
{
dataGridAddenda.Rows.Add();
dataGridAddenda.Rows[cnt].Cells[0].Value = s.Substring(0, 1);
dataGridAddenda.Rows[cnt].Cells[1].Value = s.Substring(1, 2).Trim();
dataGridAddenda.Rows[cnt].Cells[2].Value = s.Substring(3, 80).Trim();
dataGridAddenda.Rows[cnt].Cells[3].Value = s.Substring(83, 4);
dataGridAddenda.Rows[cnt++].Cells[4].Value = s.Substring(87, 7).Trim();
}
s = sr.ReadLine();
}
}
break;
}
case "CCD":
{
Node = treeViewACH.SelectedNode.Text;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH\\" + Node;
treeViewACH.SelectedNode.ToolTipText = strFilePath;
if (File.Exists(strFilePath))
{
pnlACH.Visible = true;
pnlACH.Controls.Clear();
datagridFileHeader.Visible = false;
datagridBatchHeader.Visible = false;
datagridEntryDetails.Visible = true;
dataGridAddenda.Visible = true;
StreamReader sr = new StreamReader(strFilePath);
string s = sr.ReadLine();
int cnt = 0;
while (s != null)
{
if ((s.Substring(0, 1)) == "6")
{
datagridEntryDetails.Rows[0].Cells[0].Value = s.Substring(0, 1);
datagridEntryDetails.Rows[0].Cells[1].Value = s.Substring(1, 2).Trim();
datagridEntryDetails.Rows[0].Cells[2].Value = s.Substring(3, 8).Trim();
datagridEntryDetails.Rows[0].Cells[3].Value = s.Substring(11, 1);
datagridEntryDetails.Rows[0].Cells[4].Value = s.Substring(12, 17).Trim();
datagridEntryDetails.Rows[0].Cells[5].Value = s.Substring(29, 10).Trim();
datagridEntryDetails.Columns[6].HeaderText = "IdentificationNumber";
datagridEntryDetails.Rows[0].Cells[6].Value = s.Substring(39, 15);
datagridEntryDetails.Columns[7].HeaderText = "RecievingCompanylName";
datagridEntryDetails.Rows[0].Cells[7].Value = s.Substring(54, 22).Trim();
datagridEntryDetails.Rows[0].Cells[8].Value = s.Substring(76, 2).Trim();
datagridEntryDetails.Rows[0].Cells[9].Value = s.Substring(78, 1);
datagridEntryDetails.Rows[0].Cells[10].Value = s.Substring(79, 15).Trim();
}
if ((s.Substring(0, 1)) == "7")
{
dataGridAddenda.Rows.Add();
dataGridAddenda.Rows[cnt].Cells[0].Value = s.Substring(0, 1);
dataGridAddenda.Rows[cnt].Cells[1].Value = s.Substring(1, 2).Trim();
dataGridAddenda.Rows[cnt].Cells[2].Value = s.Substring(3, 80).Trim();
dataGridAddenda.Rows[cnt].Cells[3].Value = s.Substring(83, 4);
dataGridAddenda.Rows[cnt++].Cells[4].Value = s.Substring(87, 7).Trim();
}
s = sr.ReadLine();
}
}
break;
}
}
if (treeViewACH.SelectedNode.Text == "ACHFile")
{
datagridFileHeader.Visible = false;
datagridBatchHeader.Visible = false;
datagridEntryDetails.Visible = false;
pnlACH.Controls.Clear();
}
if (treeViewACH.SelectedNode.Text == "FileHeader")
{
datagridFileHeader.Visible = false;
datagridBatchHeader.Visible = false;
datagridEntryDetails.Visible = false;
pnlACH.Controls.Clear();
pnlACH.Visible = true;
UserControl usrcntrlfileheader = new UsrCntrlFileHeader();
usrcntrlfileheader.Show();
pnlACH.Controls.Add(usrcntrlfileheader);
myNodeCollection = treeViewACH.SelectedNode.Nodes;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH";
strFileEntries = Directory.GetFiles(strFilePath, "FileHeader_*.txt");
if (strFileEntries.Length > 0)
{
foreach (string fileName in strFileEntries)
{
foreach (TreeNode n1 in myNodeCollection)
{
string s = n1.Text;
if (n1.Text == fileName.Substring(fileName.IndexOf("FileHeader_")))
{
sflag = true;
}
}
if (!sflag)
{
strFiletype = fileName.Substring(fileName.IndexOf("FileHeader_"));
treeViewACH.SelectedNode.Nodes.Add(new TreeNode(strFiletype));
}
}
}
}
if (treeViewACH.SelectedNode.Text == "CCD")
{
datagridFileHeader.Visible = false;
datagridBatchHeader.Visible = false;
datagridEntryDetails.Visible = false;
dataGridAddenda.Visible = false;
int bcount = 0, fcount = 0;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH";
string time = DateTime.Now.ToString("MM/dd/yyyy");
time = time.Replace("/", "");
strFileEntries = Directory.GetFiles(strFilePath, "*Header*" + time + ".txt");
foreach (string fileName in strFileEntries)
{
if ((fileName.IndexOf("BatchHeader") > 0))
{
bcount++;
}
else if ((fileName.IndexOf("FileHeader_") > 0))
{
fcount++;
}
}
if (!(bcount > 0 && fcount > 0))
{
pnlACH.Controls.Clear();
pnlACH.Visible = true;
UserControl usrcntrlHeader = new usrcntrlCreateHeader();
usrcntrlHeader.Show();
pnlACH.Controls.Add(usrcntrlHeader);
}
else
{
pnlACH.Controls.Clear();
pnlACH.Visible = true;
UserControl usrcntrlCCD = new UsrCntrlCCD();
usrcntrlCCD.Show();
pnlACH.Controls.Add(usrcntrlCCD);
myNodeCollection = treeViewACH.Nodes;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH";
strFileEntries = Directory.GetFiles(strFilePath, "CCD_EntryDetailRecord_*.txt");
myNodeCollection = treeViewACH.SelectedNode.Nodes;
if (strFileEntries.Length > 0)
{
foreach (string fileName in strFileEntries)
{
foreach (TreeNode n1 in myNodeCollection)
{
string s = n1.Text;
if (n1.Text == fileName.Substring(fileName.IndexOf("CCD_EntryDetailRecord_")))
{
sflag = true;
}
}
if (!sflag)
{
strFiletype = fileName.Substring(fileName.IndexOf("CCD_EntryDetailRecord_"));
treeViewACH.SelectedNode.Nodes.Add(new TreeNode(strFiletype));
}
}
}
}
}
if (treeViewACH.SelectedNode.Text == "PPD")
{
datagridFileHeader.Visible = false;
datagridBatchHeader.Visible = false;
datagridEntryDetails.Visible = false;
dataGridAddenda.Visible = false;
int bcount = 0, fcount = 0;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH";
string time = DateTime.Now.ToString("MM/dd/yyyy");
time = time.Replace("/", "");
strFileEntries = Directory.GetFiles(strFilePath, "*Header*" + time + ".txt");
foreach (string fileName in strFileEntries)
{
if ((fileName.IndexOf("BatchHeader") > 0))
{
bcount++;
}
else if ((fileName.IndexOf("FileHeader_") > 0))
{
fcount++;
}
}
if (!(bcount > 0 && fcount > 0))
{
pnlACH.Controls.Clear();
pnlACH.Visible = true;
UserControl usrcntrlHeader = new usrcntrlCreateHeader();
usrcntrlHeader.Show();
pnlACH.Controls.Add(usrcntrlHeader);
}
else
{
pnlACH.Controls.Clear();
pnlACH.Visible = true;
UserControl usrcntrlPPD = new usrcntrlPPD();
usrcntrlPPD.Show();
pnlACH.Controls.Add(usrcntrlPPD);
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH";
strFileEntries = Directory.GetFiles(strFilePath, "PPD_EntryDetailRecord*.txt");
myNodeCollection = treeViewACH.SelectedNode.Nodes;
if (strFileEntries.Length > 0)
{
foreach (string fileName in strFileEntries)
{
foreach (TreeNode n1 in myNodeCollection)
{
string s = n1.Text;
if (n1.Text == fileName.Substring(fileName.IndexOf("PPD_EntryDetailRecord_")))
{
sflag = true;
}
}
if (!sflag)
{
strFiletype = fileName.Substring(fileName.IndexOf("PPD_EntryDetailRecord_"));
treeViewACH.SelectedNode.Nodes.Add(new TreeNode(strFiletype));
}
}
}
}
}
if (treeViewACH.SelectedNode.Text == "EntryDetail")
{
pnlACH.Controls.Clear();
datagridFileHeader.Visible = false;
datagridEntryDetails.Visible = false;
datagridBatchHeader.Visible = false;
dataGridAddenda.Visible = false;
}
if (treeViewACH.SelectedNode.Text == "BatchHeader")
{
datagridFileHeader.Visible = false;
datagridBatchHeader.Visible = false;
datagridEntryDetails.Visible = false;
dataGridAddenda.Visible = false;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH";
string time = DateTime.Now.ToString("MM/dd/yyyy");
time = time.Replace("/", "");
strFilePath += "/FileHeader_" + time + ".txt";
if (!(File.Exists(strFilePath)))
{
pnlACH.Controls.Clear();
pnlACH.Visible = true;
UserControl usrcreatefileheader = new usrcntrlCreateFileHeader();
usrcreatefileheader.Show();
pnlACH.Controls.Add(usrcreatefileheader);
}
else
{
datagridFileHeader.Visible = false;
pnlACH.Controls.Clear();
pnlACH.Visible = true;
UserControl usrcntrlbatchheader = new UsrCntrlBatchHeader();
usrcntrlbatchheader.Show();
pnlACH.Controls.Add(usrcntrlbatchheader);
myNodeCollection = treeViewACH.SelectedNode.Nodes;
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH";
strFileEntries = Directory.GetFiles(strFilePath, "*Batch*.txt");
if (strFileEntries.Length > 0)
{
foreach (string fileName in strFileEntries)
{
foreach (TreeNode n1 in myNodeCollection)
{
string s = n1.Text;
if (n1.Text == fileName.Substring((fileName.IndexOf("Batch") - 4)))
{
sflag = true;
}
}
if (!sflag)
{
strFiletype = fileName.Substring((fileName.IndexOf("Batch") - 4));
treeViewACH.SelectedNode.Nodes.Add(new TreeNode(strFiletype));
}
}
}
}
}
}
}
}
}