在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 的列时发生语法错误。不懂,高手指教啊。
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 的列时发生语法错误。不懂,高手指教啊。
解决方案 »
- 读取XML某一节点下面的所有子节点的值
- 浏览器兼容问题
- hosts文件的设定,和iis的配置
- ASP.net简单的问题
- 还是用户控件问题,急啊!!!
- 求CuteEditor不放置在根目录的解决办法
- asp.net自动提交问题,救求!
- DataGrid 中如何实现一行显示两条记录!
- 热烈庆祝Microsoft Windows Server 2003 vs Microsoft Visual Studio .NET 2003 中文版今日发布!
- 如何判断用户在第一次登录时才弹出提示框?
- 请教,if (!IsPostBack)的问题
- |M| 300分求:这样的要求ASP.NET能否做到 掉用FTP上面的WinRAR压缩某个FTP上面的某个文件夹 谢谢
为什么要GridView1.DataKeys[e.RowIndex].Values.ToString(),
this.GridView1.DataKeyNames = new string[] { "id" };
你的id是int的吧!
SqlCommand cm = new SqlCommand("delete from GPSInfor where id=" + this.GridView1.DataKeys[e.RowIndex].Values.ToString(), cn);
应该是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,还是自己粗心造成的,与大家共勉。^_^