在GridView中添加了一个删除列,点击删除记录。为什么不行啊?vs2005版 代码如下:
    ClassDataBase cdb = new ClassDataBase();
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }    protected void BindData()
    {
        string sqlstr = "select * from GPSInfor";
        DataSet ds = cdb.selectData(sqlstr,"GPSTable");
        this.GridView1.DataSource = ds;
        this.GridView1.DataKeyNames = new string[] { "id" };
        this.GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
        cn.Open();        SqlCommand cm = new SqlCommand("delete from GPSInfor where id='" + this.GridView1.DataKeys[e.RowIndex].Values.ToString() + "'", cn);
            cm.ExecuteNonQuery();
            Response.Write("<script language=javascript>alert('删除成功!')</script>");
            cn.Close();
            BindData();
     }错误提示如下:将 varchar 值 'System.Collections.Specialized.OrderedDictionary' 转换为数据类型为 int 的列时发生语法错误。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 将 varchar 值 'System.Collections.Specialized.OrderedDictionary' 转换为数据类型为 int 的列时发生语法错误。不懂,高手指教啊。

解决方案 »

  1.   

    GridView1.DataKeys[e.RowIndex].Values不就是INT?
    为什么要GridView1.DataKeys[e.RowIndex].Values.ToString(),
     this.GridView1.DataKeyNames = new string[] { "id" };
    你的id是int的吧!
      

  2.   

    SqlCommand cm = new SqlCommand("delete from GPSInfor where id='" + this.GridView1.SelectedValue.ToString() + "'", cn);
      

  3.   

    SqlCommand cm = new SqlCommand("delete from GPSInfor where id='" + this.GridView1.DataKeys[e.RowIndex].Values.ToString() + "'", cn);这里id应该是整行的  不用加'
    SqlCommand cm = new SqlCommand("delete from GPSInfor where id=" + this.GridView1.DataKeys[e.RowIndex].Values.ToString(), cn);
      

  4.   

    如果id是int 的话,SQL语句不用加单引号
      

  5.   

    你们都错了,我自己找到错误了,
    应该是SqlCommand cm = new SqlCommand("delete from GPSInfor where id='" + this.GridView1.DataKeys[e.RowIndex].Value.ToString() + "'", cn);
    而不是SqlCommand cm = new SqlCommand("delete from GPSInfor where id='" + this.GridView1.DataKeys[e.RowIndex].Values.ToString() + "'", cn);
    注意不是Values而是Value,还是自己粗心造成的,与大家共勉。^_^