2012-03-30 88 views
0

我想要刪除並插入記錄,但我有索引超出範圍的錯誤。必須是非負的,並且小於集合的大小。參數名稱:索引我無法解決它,請告訴我如何解決它,是的我有更新圖像的問題也請告訴我如何更新圖像以及。指數超出範圍。必須是非負數且小於集合的大小。參數名稱索引

我的更新和刪除命令DAC碼

public static bool UpdateStudent(string FirstName, string LastName, char Gender, float GPA, Byte[] MyImage) 
     { 
      bool success = false; 
      string sql = "UPDATE Student SET [email protected],[email protected]," + 
       "Gender = @prmGender,[email protected],[email protected]" + 
        "WHERE [email protected]"; 
      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; 
     } 
    } 

我aspx.cs代碼爲

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

我的.aspx代碼

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" /> 

請告訴我,誰解決這個錯誤,也考慮我的圖像更新邏輯我不確定我的邏輯是對還是錯。

+2

哇,考慮編輯您的帖子的易讀性。將你的評論與你的代碼分開。 – Khan 2012-03-30 16:36:30

回答

0

嗯,這站出來直客

Gender = @prmGender,[email protected],[email protected]" + "WHERE [email protected]"; 

應該

Gender = @prmGender,[email protected],[email protected]" + " WHERE [email protected]"; 

,因爲在目前的性別會

"@prmGender,[email protected],[email protected] [email protected]"; 

我猜字段名FristName可能你的下一個問題也是如此。 :)

+0

我解決firstName問題,以及BU我不明白關於上述兩個線以及這個脫穎而出直接和應該是一樣會請告訴我你是什麼意思關於這些上述線 – 2012-03-30 18:17:36

+0

和錯誤索引出的範圍。必須是非負數,小於集合的大小。參數名索引仍然存在,因爲它是 – 2012-03-30 18:20:04

+0

@prmMyImage和WHERE之間沒有空格。 – 2012-03-31 19:05:52

相關問題