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 System.Configuration;
using System.Collections.Specialized;
using System.Collections;


namespace PNB
{

    public partial class Form1 : Form
    {

        int x;
        int y;
        string bn = "";
        string SingleLine = "";

        int[] numbers = {16,19,27,28,45,75,80,100,105,115,121,129,130,147,
                                150,155,170,180,185,191,197,199,200,212,222,242,247,277,
                                317,357,397,437,477,494,511,528,545,562,592,608,620,630,
                                640,649,653,909}; //int[] numbers

        int b = 0;
        int c = 0;
        int n;

        string temp = "";//declared
        string LineToWrite = "";//declared
        char[] sep = { ',' };

        StreamWriter SW, SW1;
        StreamReader SR, SR1;
        int flag; // to check whterh it is payin or pay out 

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
 
             if (rbPayin.Checked != true) { flag = 0; } else { flag = 1; }
            CreateFile();
            
            MessageBox.Show("PNB File is created successfully.");

             if (flag == 1)
            {
                int i = 0;
                string line1 = ""; //line1 of file1
                string line2 = ""; //line2 for file2

                ArrayList arrline1 = new ArrayList();
                ArrayList arrline2 = new ArrayList();


                string desti_line = "";//this is for second file in which we have to write

                MessageBox.Show("Now,kindly Upload the Balance (ABF) File");
                SR = File.OpenText(openFileDialog1.FileName);
                openFileDialog1.ShowDialog();
                SR1 = File.OpenText(openFileDialog1.FileName);


                bn = "ABF";

                SaveFileDialog saveFileDialog2 = new SaveFileDialog();

                saveFileDialog2.FileName = System.DateTime.Now.Date.ToString("ddMMyyyy") + "_" + bn + ".txt";

                saveFileDialog2.ShowDialog();


                SW1 = File.CreateText(saveFileDialog2.FileName);

                while ((line1 = SR.ReadLine()) != null)
                {

                    arrline1.Add(line1);

                }

                while ((line2 = SR1.ReadLine()) != null)
                {

                    arrline2.Add(line2);

                }
                arrline2.RemoveAt(0);

                foreach (string templine1 in arrline1)
                {
                    desti_line = "";    //this is the string which we want to write in second file
                    string[] arraytempline1 = templine1.Split(',');
                    if (templine1 == "")
                    {

                        break;
                    }

                    desti_line = arraytempline1[0] + "," + arraytempline1[1] + "," + arraytempline1[2] + "," + arraytempline1[3]
                        + "," + arraytempline1[4];

                    float amount_from_exchange = float.Parse(arraytempline1[4]); //amount from exchangefile(to be payin from member)
  
                    float transacted_amount;

                    float shortage_amount;

                    foreach (string templine2 in arrline2)
                    {
                        string[] arraytempline2 = templine2.Split(',');
  
                        float amount_from_bank = float.Parse(arraytempline2[2]); //amount from abf-file(present in the account)
  
                        if (arraytempline1[2] == arraytempline2[1])
                        {
  
                            shortage_amount = (amount_from_bank - amount_from_exchange);
  
                            if (shortage_amount >= 0)
                            {
                                shortage_amount = Math.Abs(amount_from_bank - amount_from_exchange);
                                desti_line = desti_line + "," + arraytempline1[4] + "," + "0" + ",";
                            }
                            else
                            {
                                desti_line = desti_line + "," + arraytempline2[2];
                                shortage_amount = Math.Abs(amount_from_bank - amount_from_exchange);
                                desti_line = desti_line + "," + shortage_amount + "," + "S";
                            }

                            desti_line = desti_line + "," + arraytempline1[5] + "," + arraytempline1[6] + "," + arraytempline1[7]
                            + "," + arraytempline1[8];

                            SW1.WriteLine(desti_line);

                        }   //if (arraytempline1[2] == arraytempline2[1])

                    }  //foreach (string templine2 in arrline2)



                }   //foreach (string templine1 in arrline1)

            
                SR1.Close();
                SR.Close();
                SW1.Close();

                MessageBox.Show("Response file to exchange is successfully created");




            }   // if (flag == 1)



            if (MessageBox.Show("Do u want to process another file?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {

            }
            else
            {
                this.Close();
            }

        } // Private Button1 click




        private void button2_Click(object sender, EventArgs e)
        {




            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {

            }
            filenametextBox.Text = openFileDialog1.FileName;



        }//End Of button2_click

        
        
        private int CreateFile()
        {


            if (openFileDialog1.FileName == "")
            {
                MessageBox.Show("File Not Selected", "Error");
                return -1;

            } //if (openFileDialog1.FileName == "")





            SR = File.OpenText(openFileDialog1.FileName);

            n = numbers.Length;



            bn = "PNB";

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.FileName = System.DateTime.Now.Date.ToString("ddMMyyyy") + "_" + bn + ".txt";

            saveFileDialog1.ShowDialog();

            SW = File.CreateText(saveFileDialog1.FileName);

while ((SingleLine = SR.ReadLine()) != null && SR.ReadLine() != "")
            {
                //MessageBox.Show("Line:" + SingleLine); 

                if (SingleLine == "")
                {
                    break;
                }
                string[] arraynum1 = SingleLine.Split(sep); //ace 

                if (arraynum1.Length > 9 || arraynum1.Length < 9)
                {
                    MessageBox.Show("Column in the selected file is more or less than specified file(Application is Closing)");
                    this.Close();
                    return 0;
                    
                }
                temp = "";

                for (x = 0; x < numbers.Length; x++)
                {

                    if (x == 0)
                    {
                        int a = numbers[x];
                        b = 0;
                        c = a;

                    }
                    else
                    {

                        int a = numbers[x];
                        b = numbers[x - 1];//start
                        c = (a - b);//End                     

                    }


                    if (x == 0 || x == 2 || x == 3 || x == 4 || x == 5 || x == 9 || x == 28 || x == 7 || x == 38)
                    {
                        // || x == 29


                        if (x == 0)
                        {
                            // 1.        	Account Number	16	Account Number	16	Left	Yes    
                            y = 2;
                            LineToWrite = "";
                            LineToWrite = arraynum1[y] + "".PadRight(c);
                            temp += LineToWrite.Substring(0, c);
                        }

                        if (x == 2)
                        {
                            // 3.        	Service Outlet	Service Outlet
                            string ser_outlet = "008200";
                            LineToWrite = "";
                            LineToWrite = ser_outlet + "".PadRight(c);
                            temp += LineToWrite.Substring(0, c);
                        }

                        if (x == 3)
                        {
                            //4.        	Part Tran Type	1	 Valid values are “D/C”, corresponding	28-28	NA

                            y = 3;
                            LineToWrite = "";
                            LineToWrite = arraynum1[y] + "".PadRight(c);
                            temp += LineToWrite.Substring(0, c);
                        }


                        if (x == 4)//3
                        {
                            //5.        	Transaction Amount     	17	This should be of NUMBER(14,2). i.e 14 digits before decimal point and 2 digits after decimal point. It should be right justified with spaces in the left. eg, if the amount to be uploaded is 528.35, then it should be ( Eleven spaces and  then    528.35 as 	29-45	Right	Yes

                            y = 4;//Transaction Amount
                            LineToWrite = "";
                            LineToWrite = arraynum1[y];
                            LineToWrite = "".PadRight(c - LineToWrite.Length) + LineToWrite;
                            temp += LineToWrite;

                        }
                        if (x == 5)//4
                        {
                            //6.        	Transaction Particulars	30	Transaction Particulars	46-75	Left	No

                            y = 6; //Settlement type 
                            LineToWrite = "";
                            LineToWrite = arraynum1[y] + "".PadRight(c);
                            LineToWrite = LineToWrite.Substring(0, c);
                            temp += LineToWrite;
                        }
                        if (x == 7)//5
                        {
                            //8.        	Reference Number	20	Reference Number	81-100	Left	No

                            y = 5;// uniqu. ref. number
                            LineToWrite = "";
                            LineToWrite = arraynum1[y] + "".PadRight(c);
                            LineToWrite = LineToWrite.Substring(0, c);
                            temp += LineToWrite;
                        }
                        if (x == 9)//6
                        {
                            //Instrument Date DD-MM-yyyy	10	Mandatory if Instrument Type is present	106-115	Left	CM if Instrument Type is present
                            y = 1;
                            LineToWrite = "";
                            LineToWrite = arraynum1[y].Substring(0, 2) + "-" + arraynum1[y].Substring(2, 2) + "-" + arraynum1[y].Substring(4, 4);
                            temp += LineToWrite.Substring(0, c);

                        }
                        if (x == 28)//7
                        {
                            //29.    	Particulars1	40		278-317	Left	 No

                            y = 0;//Member Code
                            LineToWrite = "";
                            LineToWrite = arraynum1[y] + "".PadRight(c);
                            temp += LineToWrite.Substring(0, c);

                        }
                        /* if (x == 29)//8
                         {
                             //30.    	Particulars_2	40		318-357	Left	 No

                             y = 
                                 LineToWrite = "";                            
                             LineToWrite = arraynum1[y] + "".PadRight(c);
                             temp += LineToWrite.Substring(0, c); 
                            
                         }*/
                        if (x == 38)//9
                        {
                            //39.    	Remarks	30		563-592	Left	No


                            y = 8; //Narration
                            LineToWrite = "";
                            LineToWrite = arraynum1[y] + "".PadRight(c);
                            temp += LineToWrite.Substring(0, c);

                        }




                    }//(x == 0 || x == 3 || x == 4 || x == 5 || x == 7 )
                    else if (x == 1 || x == 14)
                    {
                        temp += "INR";
                    }
                    else if (x == 1)
                    {
                        temp += "".PadRight(16) + "1";

                    }
                    else
                    {
                        temp = temp + "".PadRight(c);
                    }
                }//for (int x = 1; x <= numbers.Length; x++)


                SW.WriteLine(temp);
                temp = "02321700000222  " + temp.Substring(16, temp.Length - 16);
                if (temp.Substring(27, 1) == "D")
                {
                    LineToWrite = "C";
                }
                else
                {
                    LineToWrite = "D";
                }

                LineToWrite = temp.Substring(0, 27) + LineToWrite + temp.Substring(28, temp.Length - 28);
                //880 + 26 + 1
                //02321700000222
                SW.WriteLine(LineToWrite);
            }//End Of while loop

            SR.Close();

            SW.Close();
            return 1;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }//Form1_FormClosing

    }//End Of the class Form1

}//End of Namespace PNB

I am adding this code in above program if the user is cancelling save dialog box

if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
        {
            MessageBox.Show("File Processing is Cancelled Application is Closing");
            this.Close();
        }

But after closing it giving Array out of Bound Exception
I am not getting

can you please paste details of encountered exception

Thanks abelLazm for giving your valuable time
If user cancel the savedialog

MessageBox.Show("File Processing is Cancelled Application is Closing");

and i have written this.close() to close the application
But it goes to the below line from above code

desti_line = desti_line + "," + arraytempline1[4] + "," + "0" + ",";

Giving array out of bound exception

write

desti_line = desti_line + "," + arraytempline1[4] + "," + "0" + ",";

in else part if it doesn't work the then write your if-statement the otherwise mean take the result "ok" in if statement and this close in your else part

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.