Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Whats Wrong in the following code I get Index was outside the bounds of the array. this error in for loop

TxtModel.Text is multiline textbox
C#
public void concode()
   {           
   int i;
     {
       string[] sep = new string[] { "\r\n" };
       string[] aaa = TxtModel.Text.Split(sep,StringSplitOptions.RemoveEmptyEntries);              
 for (i = 0; i <= aaa[i].Length-1; i++)// error at this stage Index was outside the bounds of the array.
      {
        string model = aaa[i].Substring(0, 5); // LICT 
        string length = aaa[i].Substring(5, 4);
        string style = aaa[i].Substring(9, 4);
        string mat = aaa[i].Substring(13, 2);
        string llength = aaa[i].Substring(15, 3);
        string instru = aaa[i].Substring(18, 1);
        if (model != null)
        {
         SqlCommand cmd = new SqlCommand("select * from abc where  Codemap='" + model + "' ", con);
         SqlDataReader rd;
          try
          {
             con.Open();
             rd = cmd.ExecuteReader();
             if (rd.Read() == true)
               {
                 if (rd["Codemap"].ToString() == "ab")
                   {
                   string abc = rd["PName"].ToString();
                   int partid = rd.GetInt32(0);
                  gettMate(partid, abc, mat , length , style , llength , instru );
                  }

              if (rd["Codemap"].ToString() == "bc")
              {
                  string pqr = rd["PName"].ToString();
                  int partid = rd.GetInt32(0);
                 gettMate(partid, abc, mat , length , style , llength , instru );
                                    }
                                }
                            }


                  catch (Exception)
                   {
                      throw;
                   }
                  finally
                   {
                     con.Close();
                   }
                 }
               }                
            }
        }
Posted
Updated 4-Jan-15 18:29pm
v4
Comments
King Fisher 4-Jan-15 23:33pm    
Check your Array Length.
Member 11270220 4-Jan-15 23:36pm    
I check but same error
King Fisher 5-Jan-15 0:19am    
Can you post the Sample value's of your Array .I mean what do you have on TxtModel.Text while running your Application .
Suvendu Shekhar Giri 4-Jan-15 23:48pm    
Make sure that you have correctly typed Codemap & PName as in the database.
Member 11270220 4-Jan-15 23:49pm    
ya I check all but same error

1 solution

In this loop, you are doing completely wrong thing. Probably, you meant to do something like
C++
for (i = 0; i <= aaa.Length-1; i++) //...

Besides, in all those expressions Substring(5, 4) or Substring(9, 4) nothing guarantees that the string you are using has any characters at index 5 or 9, or has enough characters after this index to get a slice. You need to check it up before taking a substring.

The whole idea of using immediate constants like 9, 5, or 4 is really bad; it makes the code purely maintainable.

As to your bugs, these problems are very basic, so, instead of asking anyone to fix them, you should learn how to debug them yourself. Such problems are the most easy to solve. Use the debugger, learn how to use it well, and you will be able to debug such problems in no time.

—SA
 
Share this answer
 
Comments
Member 11270220 5-Jan-15 0:42am    
m checking that substring with database
Sergey Alexandrovich Kryukov 5-Jan-15 0:59am    
Where? :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900