不多说,直接来问题:
后台代码:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
string 学号 = GridView1.DataKeys[e.RowIndex].Value.ToString();
string 课程号 = (GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text.ToString();(出错!指定参数超出有效范围 参数名:index)
string 成绩 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
string 备注 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
String mys = "server=HCZ-PC;database=xscjgl; User ID=sa;pwd=sa;Trusted_Connection=no";
SqlConnection mycon1 = new SqlConnection(mys);
mycon1.Open();
string sql = "update sc set 成绩='" + 成绩 + "', 备注='" + 备注 + "'where 学号='" + 学号 + "' and 课程号='" + 课程号 + "' ";
SqlCommand mycom2 = new SqlCommand(sql, mycon1);
mycom2.ExecuteNonQuery();
mycom2.Dispose();
mycon1.Close();
GridView1.EditIndex = -1;
bind(); }
catch(Exception ee)
{
Response.Write("<script>alert('"+ ee.Message+"')</scrip>");
}
}
附:在string 学号 = GridView1.DataKeys[e.RowIndex].Value.ToString();学号有值,但是到课程号的就没有值,我用try ……catch检测不到出错!
各位高手帮帮忙啊!
在网上找了半天的方法,也试了半天就是搞不出来,我是新手!
后台代码:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
string 学号 = GridView1.DataKeys[e.RowIndex].Value.ToString();
string 课程号 = (GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text.ToString();(出错!指定参数超出有效范围 参数名:index)
string 成绩 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
string 备注 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
String mys = "server=HCZ-PC;database=xscjgl; User ID=sa;pwd=sa;Trusted_Connection=no";
SqlConnection mycon1 = new SqlConnection(mys);
mycon1.Open();
string sql = "update sc set 成绩='" + 成绩 + "', 备注='" + 备注 + "'where 学号='" + 学号 + "' and 课程号='" + 课程号 + "' ";
SqlCommand mycom2 = new SqlCommand(sql, mycon1);
mycom2.ExecuteNonQuery();
mycom2.Dispose();
mycon1.Close();
GridView1.EditIndex = -1;
bind(); }
catch(Exception ee)
{
Response.Write("<script>alert('"+ ee.Message+"')</scrip>");
}
}
附:在string 学号 = GridView1.DataKeys[e.RowIndex].Value.ToString();学号有值,但是到课程号的就没有值,我用try ……catch检测不到出错!
各位高手帮帮忙啊!
在网上找了半天的方法,也试了半天就是搞不出来,我是新手!
解决方案 »
- 关于textbox控件内容更新的问题。
- XXX.apsx页面,在浏览器中每打开一次,是不是就声明了一个这个类的实例? 能否这样理解?
- ASP.NET中GirdView选择数据时处理的难题
- button 奇怪了!非常人所见!!!!!!
- 如何将excel里的数据导入SQL
- 动网新闻的截图功能是怎么实现的,请了解的讲下大概思路
- 求助!ASP.NET连接SQL失败!
- 怎样获得网卡的地址
- 请教高手使用pop.gmail.com接邮件
- 【调查】如何理解Nhibernate?使用的场合及优缺点如何?
- 关于控制asp页面中控件的代码
- ObjectStateManager 中已存在具有同一键的对象。现有对象处于 Modified 状态。只有在该对象处于 added 状态后,才能再次将它添加到
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="false"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
BorderWidth="1px" >
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField DataField="学号" HeaderText="学号" ReadOnly="true" />
<asp:BoundField DataField="姓名" HeaderText="姓名" ReadOnly="true" />
<asp:BoundField DataField="专业" HeaderText="专业" ReadOnly="true" />
<asp:BoundField DataField="年级" HeaderText="年级" ReadOnly="true" />
<asp:BoundField DataField="课程号" HeaderText="课程号" ReadOnly="true" />
<asp:BoundField DataField="课程名称" HeaderText="课程名称" ReadOnly="true" />
<asp:BoundField DataField="开课学期" HeaderText="开课学期" ReadOnly="true" />
<asp:BoundField DataField="成绩" HeaderText="成绩 "/>
<asp:BoundField DataField="备注" HeaderText="备注"/>
</Columns>
</asp:GridView>求指点啊 !!!!
string xuehao = GridView1.DataKeys[e.RowIndex].Value.ToString();
//去的文本框中输入的内容
string kechenghao = (GridView1.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox).Text.ToString();
string chengji = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
string beizhu = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
string kechenghao = GridView1.Rows[e.RowIndex].Cells[5].Text.ToString();
string chengji = GridView1.Rows[e.RowIndex].Cells[8].Text.ToString();
string beizhu =GridView1.Rows[e.RowIndex].Cells[9].Text.ToString();
这样不存在上面的问题,但是新的问题出来了:
chengji,beizhu就是读不出来值,其他的列的值都能够读出来,就只有这读不出来。我把除成绩,备注的其他列设置的属性为只读。大侠能解答吗??