レコードを削除して挿入したいが、インデックスが範囲外だったというエラーがあります。非負で、コレクションのサイズより小さくなければなりません。パラメータ名:インデックス私はそれを解決する方法を教えてください。はい私は問題を抱えています。また、イメージを更新する方法を教えてください同じように。インデックスが範囲外でした。負でなく、コレクションのサイズより小さくなければなりません。パラメータ名インデックス
私の更新とコマンドDACコードを削除するコードが
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" />
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" />
である
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が誰であるかを解決するために教えてくださいこのエラーはまた私の論理が正しいか間違っているか確信していない私のイメージ更新ロジックを考慮してください。
Whoa、読みやすいように投稿を編集することを検討してください。コメントをコードから分離します。 – Khan