GridView1.DataKeys[e.RowIndex].Value.ToString() 必须为非负值并小于集合大小 参数名:index

解决GridView主键配置问题

必须为非负值并小于集合大小 参数名:index

报错在:GridView1.DataKeys[e.RowIndex].Value.ToString()

问题在于:只要在GridView列表属性的DataKeyNames项里添加所设为主键的字段名就可以

数据表中设置相应的主键!

注意,是DataKeyNames,而不是DataMember

aspx.代码如下<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="选课表.aspx.cs" Inherits="学生选课系统0.WebForm6" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <style type="text/css"> body { background-image:url(../assets/images/5.jpg); background-size:cover; } </style> <form id="form1" runat="server"> <div> </div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="serial_no" DataSourceID="SqlDataSource1" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:BoundField DataField="serial_no" HeaderText="serial_no" ReadOnly="True" SortExpression="serial_no" /> <asp:BoundField DataField="student_id" HeaderText="student_id" SortExpression="student_id" /> <asp:BoundField DataField="course_id" HeaderText="course_id" SortExpression="course_id" /> <asp:BoundField DataField="course_name" HeaderText="course_name" SortExpression="course_name" /> <asp:BoundField DataField="select_time" HeaderText="select_time" SortExpression="select_time" /> <asp:CommandField ShowEditButton="True" /> <asp:CommandField ShowDeleteButton="True" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:学生选课系统ConnectionString %>" SelectCommand="SELECT * FROM [selection]"></asp:SqlDataSource> <asp:Button ID="Button1" runat="server" PostBackUrl="~/选课信息添加.aspx" style="margin-bottom: 0px" Text="添加" /> </form> </body> </html> .cs代码如下using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace 学生选课系统0 { public partial class WebForm6 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string serial_no = GridView1.DataKeys[e.RowIndex].Value.ToString(); string student_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString(); string course_id = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString(); string course_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString(); string sqlStr = "update selection set student_id='" + student_id + "',course_id=" + course_id + " , course_name ='" + course_name + "', select_time = Getdate() where serial_no= " + serial_no; String str = "Server=10.13.111.79;User Id=cc;Pwd=Lkp007958;DataBase=学生选课系统"; SqlConnection conn = new SqlConnection(str); conn.Open(); SqlCommand cmd = new SqlCommand(sqlStr, conn); SqlDataSource1.UpdateCommand = sqlStr; int num = cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close(); GridView1.EditIndex = -1; GridView1.DataBind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string serial_no = GridView1.DataKeys[e.RowIndex].Value.ToString(); string sqlStr = "DELETE FROM selection WHERE serial_no = @serial_no"; string str = "Server=10.13.111.79;User Id=cc;Pwd=Lkp007958;DataBase=学生选课系统"; SqlConnection conn = new SqlConnection(str); conn.Open(); SqlCommand cmd = new SqlCommand(sqlStr, conn); cmd.Parameters.AddWithValue("@serial_no", serial_no); int num = cmd.ExecuteNonQuery(); cmd.Dispose(); conn.Close(); GridView1.EditIndex = -1; GridView1.DataBind(); Response.Redirect(Request.Url.PathAndQuery); } } }如何添加一个按student_id查询功能
最新发布
06-18
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值