title: Index was out of range. Must be non-negative and less than the size of the collection.Parameter name index
body: <p>I want to Delete and Insert the Record but I have the error which is Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index I can't solve it please tell me how to solve it and yes I have the problem of Updating the Imge also please tell me how to update the image as well.</p>
  
  <p>My Updating and Delete command DAC code is </p>
  
  <pre><code>public static bool UpdateStudent( string FirstName, string LastName, char Gender, float GPA, Byte[] MyImage)
          {
              bool success = false;
              string sql = "UPDATE Student SET FristName=@prmStudentFirstName,LastName=@prmLastName," +
                  "Gender = @prmGender,GPA=@prmGPA,MyImage=@prmMyImage" +
                      "WHERE StudentID=@prmStudentID";
              using (SqlCommand command= new SqlCommand(sql,ConnectionManager.GetConnection()))
              {
                 // command.Parameters.Add("@prmStudentID", SqlDbType.Int, 10).Value = StudentID;
                  command.Parameters.Add("@prmFirstName", SqlDbType.VarChar, 25).Value = FirstName;
                  command.Parameters.Add("@prmLastName", SqlDbType.VarChar, 25).Value = LastName;
                  command.Parameters.Add("@prmGender", SqlDbType.Char, 1).Value = Gender;
                  command.Parameters.Add("@prmGPA", SqlDbType.Float).Value = GPA;
                  command.Parameters.Add("@prmMyImage", SqlDbType.VarBinary).Value = MyImage;
                  int rowsAffected = command.ExecuteNonQuery();
                  success = (rowsAffected == 1); 
              }
              return success;
          }
          public static bool DeleteStudent(string StudentID)
          {
              bool success = false;
              string sql = "DELETE FROM Student WHERE StudentID= @prmStudentID";
              using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
              {
                  command.Parameters.Add("@prmStudentID", SqlDbType.Int, 10).Value = StudentID;
                  int rowAffected = command.ExecuteNonQuery();
                  success = (rowAffected == 1);
              }
              return success;
          }
      }
  </code></pre>
  
  <p>My aspx.cs Code is </p>
  
  <pre><code>protected void studentGridview_RowEditing(object sender, GridViewEditEventArgs e)
      {
          StudentGridView.EditIndex = e.NewEditIndex;
          DisplayStudentInformation();
      }
      protected void studentGridview_RowCancelEdit(object sender, GridViewCancelEditEventArgs e)
      {
          StudentGridView.EditIndex = -1;
          DisplayStudentInformation();
      }
      protected void studentGridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
      { //Index was out of range. Must be non-negative and less than the size of the collection.
        //Parameter name: index
          string StudentID = ((Label)StudentGridView.Rows[e.RowIndex].Cells[0].FindControl("txtStudentID")).Text.ToString();
          string FirstName = ((TextBox)StudentGridView.Rows[e.RowIndex].Cells[1].FindControl("txtStudentFName")).Text;
          string LastName = ((TextBox)StudentGridView.Rows[e.RowIndex].Cells[2].FindControl("txtStudentLName")).Text;
          string Gender = ((TextBox)StudentGridView.Rows[e.RowIndex].Cells[3].FindControl("txtStudentGender")).Text;
          string GPA = ((TextBox)StudentGridView.Rows[e.RowIndex].Cells[4].FindControl("txtStudentGPA")).Text;
          //Problem as at blow line I cant set the Image for Updating it 
          string MyImage = ((System.Web.UI.WebControls.Image)StudentGridView.Rows[e.RowIndex].Cells[5].FindControl("Image")).ImageUrl;
          char studentGender = Convert.ToChar(Gender);
          float studentGPA = Convert.ToInt64(GPA);
          if (FileUpload2.HasFile)
          {
              byte[] ImageByteArray = null;
              ImageByteArray = ConvertImageToByteArray(FileUpload2);
              try
              {
                  if (DAC.UpdateStudent( FirstName, LastName, studentGender, studentGPA, ImageByteArray))
                  {
                      StatusLabel.Text = "Student" + StudentID + "Has been Updated Successfully.";
                      StudentGridView.EditIndex = -1;
                      DisplayStudentInformation();
                  }
              }
              catch (SqlException ex)
              {
                  StatusLabel.Text = ex.Message;
              }
          }
      }
      protected void StudentGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {     //Index was out of range. Must be non-negative and less than the size of the collection.
             //Parameter name: index
          string StudentID = ((Label)StudentGridView.Rows[e.RowIndex].Cells[0].FindControl("txtStudentID")).Text;
          try
          {
              if (DAC.DeleteStudent(StudentID))
              {
                  StatusLabel.Text = "Student" + StudentID + "Has been Delted. ";
                  DisplayStudentInformation();
              }
              else
              {
                  StatusLabel.Text = "Student" + StudentID + "Could Not Be Delted.";
              }
          }
          catch (SqlException ex)
          {
              StatusLabel.Text = ex.Message;
          }
      }
  </code></pre>
  
  <p>My .aspx code is</p>
  
  <pre><code>asp:ScriptManager ID="asm" runat ="server"&gt;
      /asp:ScriptManager&gt;
      asp:Label ID="StatusLabel" runat="server"&gt;&lt;/asp:Label&gt;
      div style="vertical-align: top; height:300px; width:100%; overflow:auto;"&gt;
      asp:GridView ID="StudentGridView" runat ="server" Height="302px" Width="800px" 
          BackColor ="White" BorderColor ="#999999" GridLines ="Vertical" 
          BorderWidth="1px" CellPadding ="3" 
             AutoGenerateColumns ="false" style="margin-top: 0px" 
  EnableViewState ="false" OnRowEditing="studentGridview_RowEditing" 
             OnRowCancelingEdit="studentGridview_RowCancelEdit"  OnRowUpdating="studentGridview_RowUpdating" 
               OnRowDeleting="StudentGridView_RowDeleting" &gt;
           Columns&gt;
           asp:TemplateField  HeaderText="StudentID"&gt;
                  ItemTemplate&gt;
                    asp:Label ID ="txtStudentID" runat ="server" Text='&lt;%# Eval("StudentID") %&gt;' /&gt;
                  /ItemTemplate&gt;
                 /asp:TemplateField&gt;
                asp:TemplateField HeaderText ="FirstName"&gt;
                 ItemTemplate&gt;
                   asp:TextBox ID="txtStudentFName" runat ="server" Text ='&lt;%# Eval("FirstName") %&gt;' &gt;&lt;/asp:TextBox&gt;
                 /ItemTemplate&gt;
                /asp:TemplateField&gt;
                asp:TemplateField HeaderText ="LastName"&gt;
                 ItemTemplate&gt;
                   asp:TextBox ID="txtStudentLName" runat ="server" Text ='&lt;%# Eval("LastName") %&gt;' /&gt;
                 /ItemTemplate&gt;
                /asp:TemplateField&gt;
                asp:TemplateField HeaderText ="Gender"&gt;
                 ItemTemplate&gt;
                   asp:TextBox ID="txtStudentGender" runat ="server" Text ='&lt;%# Eval("Gender") %&gt;' /&gt;
                 /ItemTemplate&gt;
                /asp:TemplateField&gt;
                asp:TemplateField HeaderText ="GPA"&gt;
                 ItemTemplate&gt;
                   asp:TextBox ID="txtStudentGPA" runat ="server" Text ='&lt;%# Eval("GPA") %&gt;' /&gt;
                 /ItemTemplate&gt;
                /asp:TemplateField&gt;
                 asp:TemplateField  HeaderText ="Images"&gt;
                  ItemTemplate&gt;
                   asp:Image ID ="Image" runat ="server" Height ='30px' Width ='30px' ImageUrl ='&lt;%# "Handler.ashx?StudentID="+Eval("StudentID") %&gt;' /&gt;
  
                  /ItemTemplate&gt;
                 /asp:TemplateField&gt;
                 asp:CommandField ShowDeleteButton="true" CausesValidation ="false" /&gt;
                 asp:CommandField ShowEditButton="true" CausesValidation="false" /&gt;
                /Columns&gt;     
          HeaderStyle BackColor ="#0000B4" Font-Bold ="true" ForeColor ="White" Font-Size="Small" /&gt;
          EditRowStyle BackColor ="#00ABC" Font-Bold="true" ForeColor="White" font-Size="Small" /&gt;
          AlternatingRowStyle BackColor="#DCDCDC" Font-Size="Small" /&gt;
          SelectedRowStyle BackColor ="#00ABAC" Font-Bold="true" ForeColor ="White" Font-Size="Small" /&gt;
      /asp:GridView&gt;
      /div&gt;
      asp:Table runat="server" Height="16px" Width="78px"&gt;
       asp:TableRow&gt;
       asp:TableCell &gt;&lt;asp:Image ID ="AddStudentRecord" runat ="server"  Width="100" Height ="50" ImageUrl="~/images/AddStudentRecord.JPG"/&gt;&lt;/asp:TableCell&gt;
       /asp:TableRow&gt;
       asp:TableRow&gt;
          asp:TableCell&gt;
             asp:Panel CssClass="modal" ID ="ModalPanel"  ScrollBars="Vertical" runat ="server" Width="950" Height="90" style="display:none"&gt;
               asp:Table runat ="server"&gt;
                  asp:TableRow&gt;
                    asp:TableCell&gt;
                        asp:Label ID="StudentIDLabel" runat ="server" Text="Student ID"&gt;/asp:Label&gt;
                    /asp:TableCell&gt;
                    asp:TableCell&gt;
                        asp:Label ID="StudentFirstNameLabel" runat ="server" Text="Student FirstName"&gt;&lt;/asp:Label&gt;
                    /asp:TableCell&gt;
                    asp:TableCell&gt;
                        asp:Label ID="StudentLastNameLabel" runat ="server" Text="Student LastName"&gt;&lt;/asp:Label&gt;
                    /asp:TableCell&gt;
                    asp:TableCell&gt;
                        asp:Label ID="StudentGenderLabel" runat ="server" Text="Student Gender"&gt;&lt;/asp:Label&gt;
                    /asp:TableCell&gt;
                    asp:TableCell&gt;
                        asp:Label ID="StudentGPALabel" runat ="server" Text="Student GPA"&gt;/asp:Label&gt;
                    /asp:TableCell&gt;
                    asp:TableCell&gt;
                        asp:Label ID="StudentImageLabel" runat ="server" Text="Student Image"&gt;&lt;/asp:Label&gt;
                    /asp:TableCell&gt;
                  /asp:TableRow&gt;
                    asp:TableRow&gt;
                        asp:TableCell&gt;
                            asp:TextBox ID="StudentIDTextBox" runat ="server"&gt;&lt;/asp:TextBox&gt; 
                        /asp:TableCell&gt;
                        asp:TableCell&gt;
                            asp:TextBox ID="StudentFirstNameTextBox" runat ="server"&gt;/asp:TextBox&gt; 
                        /asp:TableCell&gt;
                        asp:TableCell&gt;
                            asp:TextBox ID="StudentLastNameTextBox" runat ="server"&gt;/asp:TextBox&gt; 
                        /asp:TableCell&gt;
                        asp:TableCell&gt;
                            asp:TextBox ID="StudentGenderTextBox" runat ="server"&gt;/asp:TextBox&gt; 
                        /asp:TableCell&gt;
                        asp:TableCell&gt;
                            asp:TextBox ID="StudentGPATextBox" runat ="server"&gt;/asp:TextBox&gt; 
                        /asp:TableCell&gt;
                        asp:TableCell&gt;
                            asp:Image ID="StudentImage" ImageUrl="" Width="25px" Height ="25px" runat="server" /&gt;                          
                        /asp:TableCell&gt; 
                    /asp:TableRow&gt;
                        asp:TableRow&gt;
                           asp:TableCell ColumnSpan ="4" HorizontalAlign="Center"&gt;
                               asp:Button ID="OKButton" runat ="server" Text="Add"  /&gt;&amp;nbsp;
                               asp:Button ID="CancelButton" runat ="server" Text ="Cancel" /&gt;
                               /asp:TableCell&gt;                
                           asp:TableCell &gt;   
                        /asp:TableCell&gt;
                        asp:TableCell   HorizontalAlign="left"&gt;
                             asp:FileUpload ID="FileUpload2" runat="server" /&gt;
                             asp:Button ID="Button1" runat="server" Text="Upload"  OnClick="UploadButton_Click"  /&gt;
                        /asp:TableCell&gt;
                        /asp:TableRow&gt;
                /asp:Table&gt;
             /asp:Panel&gt; 
             cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
             TargetControlID="AddStudentRecord" PopupControlID="ModalPanel" BackgroundCssClass ="modalBackground" /&gt;
  </code></pre>
  
  <p>Please tell me who to solve this error and also consider my Image updating logic I am not sure that my logic is right or wrong.</p>