datagrid删除数据时出现异常
我的代码是这么写的:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace sbumit.admin
{
/// <summary>
/// degree_m 的摘要说明。
/// </summary>
public class degree_m : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button btndegree;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.TextBox tbxdegree;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(Session[ "usertype "]==null || Session[ "usertype "].GetHashCode() < 1 || Session[ "usertype "].GetHashCode() > 1)
{
//没有登录或身份不是管理员
Response.Redirect( "login.aspx ") ;
}
else
{
this.bindtodatagrid();
}
// 在此处放置用户代码以初始化页面
}
public void bindtodatagrid()
{
//绑定数据库
SqlConnection con=db.dbcon();
con.Open();
SqlDataAdapter sda_d=new SqlDataAdapter();//数据适配器
sda_d.SelectCommand=new SqlCommand( "select * from degree ",con);//检索出现有的学位
DataSet set_d=new DataSet();
sda_d.Fill(set_d, "degree ");
con.Close();
this.DataGrid1.DataSource=set_d.Tables[ "degree "];
this.DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand_1);
this.btndegree.Click += new System.EventHandler(this.btndegree_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btndegree_Click(object sender, System.EventArgs e)
{
SqlConnection con=db.dbcon();
con.Open();
SqlCommand addp=new SqlCommand( "insert into degree(degree) values ( ' "+this.tbxdegree.Text+ " ') ",con);
try
{
addp.ExecuteNonQuery();
this.Label1.Text=message.Show( "学位添加成功! ");
}
catch
{
this.Label1.Text=message.Show( "学位添加失败! ");
}
}
private void DataGrid1_DeleteCom