I have develop an utility for my company in which there is two file format
If i browse file which is eg->ACE.T004
It gives following error
Error:Index and Length must refer to the location within the string
Parameter name : Length

Following is my code

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;

namespace PNB
{
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog1.FileName == "")
                {
                    MessageBox.Show("File Not Selected", "Error");

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

                StreamWriter SW;
                StreamReader SR;
                                
                string bn = "";
                if (rbPNB.Checked == true) { bn = "_PNB"; } else { bn = "_Ace"; }
                
                SR = File.OpenText(openFileDialog1.FileName);
                SW = File.CreateText("D:\\PNB\\" + System.DateTime.Now.Date.ToString("ddMMyyyy") + bn + ".txt");
                
                int[] numbers = {15, 18, 26, 27, 44, 74, 79, 99, 104, 114, 120, 128, 129, 146, 149, 154,
				    169, 179, 184, 190 ,196, 198, 199, 211, 221, 241, 246, 276, 316, 356, 396,
				    436, 476, 493, 510, 527, 544, 561, 591, 607, 619, 629, 639, 648, 652, 908
				        }; //int[] numbers 
                
                
                
                string[] arr = { };//string[] arr
                int n = numbers.Length;
                int b = 0;
                int c = 0;
                int s;
                int numberslength = numbers.Length;
                string SingleLine = "";
                while ((SingleLine = SR.ReadLine()) != null)
                {
                
                    for (int i = 0; i < n; i++)
                    {
                        if (i == 0)
                        {
                            s = -1;
                            int a = numbers[i];
                            b = s + 1;
                            c = a - b + 1;

                
                        }
                        else
                        {
                            int a = numbers[i];
                            b = numbers[i - 1] + 1;//start
                            c = a - b + 1;//End
                        }

                       SingleLine = String.Concat(SingleLine, SingleLine.Substring(b, c));
                        SingleLine = String.Concat(SingleLine, ",");


                       
                    }//for (int i = 0; i < n; i++)

                    string st = SingleLine;

                    arr = st.Split(',');
                    
                    int index;
                    string outputstring = "";
                    if (rbPNB.Checked == true)
                    {
                        bn = "PNB";
                        string strpos = System.Configuration.ConfigurationManager.AppSettings["array"];
                        char[] sep = { ',' };
                        string[] arraynum = strpos.Split(sep);

                        for (int j = 0; j < arraynum.Length; j++)
                        {
                            index = Int32.Parse(arraynum[j]) - 1;
                            outputstring += arr[index] + ",";
                        }
                    }//if (bn == "Ace")
                    
                    else
                    {
                        bn = "Ace";
                        string strpos1 = System.Configuration.ConfigurationManager.AppSettings["array1"];
                        char[] sep1 = { ',' };
                        string[] arraynum1 = strpos1.Split(sep1);
                        for (int j = 0; j < arraynum1.Length; j++)
                        {
                            index = Int32.Parse(arraynum1[j]) - 1;
                            outputstring += arr[index] + ",";
                        }
                    }
                    SingleLine = outputstring.Trim(',');
                    SW.WriteLine(SingleLine);

                    // }//if (bn == "Ace")                              




                } // while ( !SR.EndOfStream ) 

                SR.Close();
                SW.Close();

                MessageBox.Show("Sucessful File Conversion to " + bn);
            }//try
            catch(Exception er) {
                MessageBox.Show("Error : "+ er.Message);
            }
        } // 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 void rbPNB_CheckedChanged(object sender, EventArgs e)
        {

        }//End of rbPNB_checkedChanged

        private void rbAce_CheckedChanged(object sender, EventArgs e)
        {

        }//End of rbAce_checkedChanged

    }//End Of the class Form1

}//End of Namespace PNB

Recommended Answers

All 5 Replies

I'm guessing the error is in line 80. What's the value of SingleLine, b and c when it gives you the error?

SingleLine is the line of the file from where it is reading and b & c are the postion of the characters from string which to be written in the new file

I know what they are supposed to be, I want the actual values when the error happens.
And throw in 'i' while you are at it.

I am not getting how to find out values when error happen
Can you suggest me for the same

Are you running the program within Visual Studio? If so, you can just point the cursor at them when the error occurs and it will show you the value.

If not, when the error occurs you should get a pop-up window with Abort, Retry, Cancel on it. Retry will start the debugger and again, you should be able to see the values.

If you don't get that box, then run it on a machine with Visual Studio installed.

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.