GridView控件编辑键语法语法异常
GridView控件编辑键语法语法错误
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
public SqlConnection GetConnection()
{
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select*from tb_class";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
GridView1.DataKeyNames = new string[] { "ClassID" };
GridView1.DataBind();
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int ClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string CName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string sqlStr = "update tb_Class set ClassName=" + CName + "where classID=" + ClassID;
SqlConnection myConn = GetConnection();
myConn.Open();
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.ExecuteNonQuery();
myCmd.Dispose();
myConn.Close();
GridView1.EditIndex = -1;
this.bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
this.bind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}

是不是config里面的错误?
------解决方案--------------------
string sqlStr = "update tb_Class set ClassName=‘" + CName + "’ where classID=" + ClassID;
------解决方案--------------------
string sqlStr = "select * from tb_class";
string sqlStr = "update tb_Class set ClassName='" + CName + "' where classID=" + ClassID;
sql语句错了就是细节问题 ,可以先到查询分析器里面运行下,再贴过来
------解决方案--------------------
你的更新语句有问题: set ClassName=" + CName + "
因为ClassName是字段串类型,所以,它的值必须包在单引号内,比如 set ClassName=\'" + CName + "\'
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
public SqlConnection GetConnection()
{
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select*from tb_class";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
GridView1.DataKeyNames = new string[] { "ClassID" };
GridView1.DataBind();
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int ClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string CName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string sqlStr = "update tb_Class set ClassName=" + CName + "where classID=" + ClassID;
SqlConnection myConn = GetConnection();
myConn.Open();
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.ExecuteNonQuery();
myCmd.Dispose();
myConn.Close();
GridView1.EditIndex = -1;
this.bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
this.bind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
是不是config里面的错误?
------解决方案--------------------
string sqlStr = "update tb_Class set ClassName=‘" + CName + "’ where classID=" + ClassID;
------解决方案--------------------
string sqlStr = "select * from tb_class";
string sqlStr = "update tb_Class set ClassName='" + CName + "' where classID=" + ClassID;
sql语句错了就是细节问题 ,可以先到查询分析器里面运行下,再贴过来
------解决方案--------------------
你的更新语句有问题: set ClassName=" + CName + "
因为ClassName是字段串类型,所以,它的值必须包在单引号内,比如 set ClassName=\'" + CName + "\'