-2

I made a list of employees connected to MySQL database. Every employee's name can do CRUD. However, when updating employee data, this error message appears "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name : index"

How to overcome it? Here is my code

try {
  if (e.CommandName.Equals("Tambah")) {
    using(SqlConnection sqlcon = new SqlConnection(connectionString)) {
      sqlcon.Open();
      string query = "INSERT INTO EmployeeDB (NamaLengkap,Alamat,Telp,Email,Gender,Jabatan) VALUES (@NamaLengkap,@Alamat,@Telp,@Email,@Gender,@Jabatan)";
      SqlCommand sqlcmd = new SqlCommand(query, sqlcon);
      sqlcmd.Parameters.AddWithValue("@NamaLengkap", (gvEmployeeDB.FooterRow.FindControl("txtNamaLengkapFooter") as TextBox).Text.Trim());
      sqlcmd.Parameters.AddWithValue("@Alamat", (gvEmployeeDB.FooterRow.FindControl("txtAlamatFooter") as TextBox).Text.Trim());
      sqlcmd.Parameters.AddWithValue("@Telp", (gvEmployeeDB.FooterRow.FindControl("txtTelpFooter") as TextBox).Text.Trim());
      sqlcmd.Parameters.AddWithValue("@Email", (gvEmployeeDB.FooterRow.FindControl("txtEmailFooter") as TextBox).Text.Trim());
      sqlcmd.Parameters.AddWithValue("@Gender", (gvEmployeeDB.FooterRow.FindControl("txtGenderFooter") as TextBox).Text.Trim());
      sqlcmd.Parameters.AddWithValue("@Jabatan", (gvEmployeeDB.FooterRow.FindControl("txtJabatanFooter") as TextBox).Text.Trim());
      sqlcmd.ExecuteNonQuery();
      PopulateGridView();
      lblSuccessMessage.Text = "New Record Add!";
      lblErrorMessage.Text = "";
    }
  }
} catch (Exception ex) {

  lblSuccessMessage.Text = "";
  lblErrorMessage.Text = ex.Message;
}
kyun
  • 9,710
  • 9
  • 31
  • 66
Mikki Hendra
  • 1
  • 1
  • 2
  • At what line does this error appear? – Bogdan Doicin May 27 '19 at 04:00
  • 2
    No one ever reads exception messages:( _"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name : index"_ means that prameter named `index` passed somewhere in your code is out of available range. It **must** be non-negative and less than the size of the collection. Look at the line where this exception is thrown and think why it is thrown. – vasily.sib May 27 '19 at 04:00
  • @vasily.sib where is that parameter in the code above? – Bogdan Doicin May 27 '19 at 04:03
  • Put a breakpoint on the first line in the method, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. – SUNIL DHAPPADHULE May 27 '19 at 04:04
  • @BogdanDoicin I don't know, but why should I care? OP has to debug **his** code to answer this question. – vasily.sib May 27 '19 at 04:09
  • 1
    My guess is the problem is in `PopulateGridView();` but as @vasily.sib has stated you should be able to debug this and find the problem. At some point you have an index and it's wrong. – Ben May 27 '19 at 04:10
  • You need to debug this piece of code to find the code which is causing this exception. – Raka May 27 '19 at 04:17

1 Answers1

0

In this case (and others), index is an internal parameter which signifies the position in the string on which the function is working at that moment. As an example, if index=3 then the function operates on the 4th letter of the string.

You get the specified error message if the procedure tries to work with a string either before the beginning of it (index<0) or beyond its end (index>length).

To solve it, you need to use the debugger, create a breakpoint at the line where the exception is thrown and see more closely what's happening there. Personally, I think it's somewhere in this code:

sqlcmd.Parameters.AddWithValue("@NamaLengkap", (gvEmployeeDB.FooterRow.FindControl("txtNamaLengkapFooter") as TextBox).Text.Trim());
sqlcmd.Parameters.AddWithValue("@Alamat", (gvEmployeeDB.FooterRow.FindControl("txtAlamatFooter") as TextBox).Text.Trim());
sqlcmd.Parameters.AddWithValue("@Telp", (gvEmployeeDB.FooterRow.FindControl("txtTelpFooter") as TextBox).Text.Trim());
sqlcmd.Parameters.AddWithValue("@Email", (gvEmployeeDB.FooterRow.FindControl("txtEmailFooter") as TextBox).Text.Trim());
sqlcmd.Parameters.AddWithValue("@Gender", (gvEmployeeDB.FooterRow.FindControl("txtGenderFooter") as TextBox).Text.Trim());
sqlcmd.Parameters.AddWithValue("@Jabatan", (gvEmployeeDB.FooterRow.FindControl("txtJabatanFooter") as TextBox).Text.Trim());

I think this because, in the code you provided, this is the only place where strings modify their lengths. Check once again if you actually have texts to trim, or if one or more of your texts are null.

Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
  • 1
    _"this is the only place"_ - there is also mysterious `PopulateGridView();` method call – vasily.sib May 27 '19 at 04:11
  • I don't think `PopulateGridView()` changes string lengths. I think it only displays something. – Bogdan Doicin May 27 '19 at 04:12
  • 1
    You never know what is written in PopulateGridView(). – Raka May 27 '19 at 04:16
  • You don't read exception messages too, aren't you? _"Must be non-negative and less than the size of the **collection**."_ strings are a collections of chars, but collections aren't limited to just strings. Inside `PopulateGridView` there maybe some calls like `(new[] { 1, 2, 3 })[42]` – vasily.sib May 27 '19 at 04:16
  • I do read them and yes, you may be right as well. – Bogdan Doicin May 27 '19 at 04:20