Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7784127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:02:44+00:00 2026-06-01T20:02:44+00:00

I want to Delete and Insert the Record but I have the error which

  • 0

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.

My Updating and Delete command DAC code is

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;
        }
    }

My aspx.cs Code is

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;
        }
    }

My .aspx code is

asp:ScriptManager ID="asm" runat ="server">
    /asp:ScriptManager>
    asp:Label ID="StatusLabel" runat="server"></asp:Label>
    div style="vertical-align: top; height:300px; width:100%; overflow:auto;">
    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" >
         Columns>
         asp:TemplateField  HeaderText="StudentID">
                ItemTemplate>
                  asp:Label ID ="txtStudentID" runat ="server" Text='<%# Eval("StudentID") %>' />
                /ItemTemplate>
               /asp:TemplateField>
              asp:TemplateField HeaderText ="FirstName">
               ItemTemplate>
                 asp:TextBox ID="txtStudentFName" runat ="server" Text ='<%# Eval("FirstName") %>' ></asp:TextBox>
               /ItemTemplate>
              /asp:TemplateField>
              asp:TemplateField HeaderText ="LastName">
               ItemTemplate>
                 asp:TextBox ID="txtStudentLName" runat ="server" Text ='<%# Eval("LastName") %>' />
               /ItemTemplate>
              /asp:TemplateField>
              asp:TemplateField HeaderText ="Gender">
               ItemTemplate>
                 asp:TextBox ID="txtStudentGender" runat ="server" Text ='<%# Eval("Gender") %>' />
               /ItemTemplate>
              /asp:TemplateField>
              asp:TemplateField HeaderText ="GPA">
               ItemTemplate>
                 asp:TextBox ID="txtStudentGPA" runat ="server" Text ='<%# Eval("GPA") %>' />
               /ItemTemplate>
              /asp:TemplateField>
               asp:TemplateField  HeaderText ="Images">
                ItemTemplate>
                 asp:Image ID ="Image" runat ="server" Height ='30px' Width ='30px' ImageUrl ='<%# "Handler.ashx?StudentID="+Eval("StudentID") %>' />

                /ItemTemplate>
               /asp:TemplateField>
               asp:CommandField ShowDeleteButton="true" CausesValidation ="false" />
               asp:CommandField ShowEditButton="true" CausesValidation="false" />
              /Columns>     
        HeaderStyle BackColor ="#0000B4" Font-Bold ="true" ForeColor ="White" Font-Size="Small" />
        EditRowStyle BackColor ="#00ABC" Font-Bold="true" ForeColor="White" font-Size="Small" />
        AlternatingRowStyle BackColor="#DCDCDC" Font-Size="Small" />
        SelectedRowStyle BackColor ="#00ABAC" Font-Bold="true" ForeColor ="White" Font-Size="Small" />
    /asp:GridView>
    /div>
    asp:Table runat="server" Height="16px" Width="78px">
     asp:TableRow>
     asp:TableCell ><asp:Image ID ="AddStudentRecord" runat ="server"  Width="100" Height ="50" ImageUrl="~/images/AddStudentRecord.JPG"/></asp:TableCell>
     /asp:TableRow>
     asp:TableRow>
        asp:TableCell>
           asp:Panel CssClass="modal" ID ="ModalPanel"  ScrollBars="Vertical" runat ="server" Width="950" Height="90" style="display:none">
             asp:Table runat ="server">
                asp:TableRow>
                  asp:TableCell>
                      asp:Label ID="StudentIDLabel" runat ="server" Text="Student ID">/asp:Label>
                  /asp:TableCell>
                  asp:TableCell>
                      asp:Label ID="StudentFirstNameLabel" runat ="server" Text="Student FirstName"></asp:Label>
                  /asp:TableCell>
                  asp:TableCell>
                      asp:Label ID="StudentLastNameLabel" runat ="server" Text="Student LastName"></asp:Label>
                  /asp:TableCell>
                  asp:TableCell>
                      asp:Label ID="StudentGenderLabel" runat ="server" Text="Student Gender"></asp:Label>
                  /asp:TableCell>
                  asp:TableCell>
                      asp:Label ID="StudentGPALabel" runat ="server" Text="Student GPA">/asp:Label>
                  /asp:TableCell>
                  asp:TableCell>
                      asp:Label ID="StudentImageLabel" runat ="server" Text="Student Image"></asp:Label>
                  /asp:TableCell>
                /asp:TableRow>
                  asp:TableRow>
                      asp:TableCell>
                          asp:TextBox ID="StudentIDTextBox" runat ="server"></asp:TextBox> 
                      /asp:TableCell>
                      asp:TableCell>
                          asp:TextBox ID="StudentFirstNameTextBox" runat ="server">/asp:TextBox> 
                      /asp:TableCell>
                      asp:TableCell>
                          asp:TextBox ID="StudentLastNameTextBox" runat ="server">/asp:TextBox> 
                      /asp:TableCell>
                      asp:TableCell>
                          asp:TextBox ID="StudentGenderTextBox" runat ="server">/asp:TextBox> 
                      /asp:TableCell>
                      asp:TableCell>
                          asp:TextBox ID="StudentGPATextBox" runat ="server">/asp:TextBox> 
                      /asp:TableCell>
                      asp:TableCell>
                          asp:Image ID="StudentImage" ImageUrl="" Width="25px" Height ="25px" runat="server" />                          
                      /asp:TableCell> 
                  /asp:TableRow>
                      asp:TableRow>
                         asp:TableCell ColumnSpan ="4" HorizontalAlign="Center">
                             asp:Button ID="OKButton" runat ="server" Text="Add"  />&nbsp;
                             asp:Button ID="CancelButton" runat ="server" Text ="Cancel" />
                             /asp:TableCell>                
                         asp:TableCell >   
                      /asp:TableCell>
                      asp:TableCell   HorizontalAlign="left">
                           asp:FileUpload ID="FileUpload2" runat="server" />
                           asp:Button ID="Button1" runat="server" Text="Upload"  OnClick="UploadButton_Click"  />
                      /asp:TableCell>
                      /asp:TableRow>
              /asp:Table>
           /asp:Panel> 
           cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
           TargetControlID="AddStudentRecord" PopupControlID="ModalPanel" BackgroundCssClass ="modalBackground" />

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:02:46+00:00Added an answer on June 1, 2026 at 8:02 pm

    Well this stood out straight off

    Gender = @prmGender,GPA=@prmGPA,MyImage=@prmMyImage" + "WHERE StudentID=@prmStudentID"; 
    

    should be

    Gender = @prmGender,GPA=@prmGPA,MyImage=@prmMyImage" + " WHERE StudentID=@prmStudentID"; 
    

    Because at the moment Gender will be

    "@prmGender,GPA=@prmGPA,MyImage=@prmMyImageWHERE StudentID=@prmStudentID"; 
    

    I’m guessing the field name FristName may be your next issue as well. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have an 'updateinfo' table in order to record every update/insert/delete operations
I want to insert and delete some chars in the middle of a file.
I want to delete a range of data from a text file using PHP.
I want to delete part of a record. For example, a column called message
I want to delete a row in the grid, but the grid always post's
I have an AFTER INSERT OR UPDATE OR DELETE trigger that I'm writing to
I have read about this error, but none of the answers seem to apply
So I have a slightly funny model: I want to record the list of
In my app i have reset button which delete all the data of tables
I have a table and want to insert the current values of records that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.