本人刚学习ASP.net ,做了个数据库通过GRIDVIEW更新数据的程序,问题是在VS里调试时可以更新,但是发布出来,在IE里面却无法更新,提示是“操作必须使用一个可更新的查询。”,代码如下:恳请各位解答!谢谢了!
public partial class _Default : System.Web.UI.Page
{
public void bind()
{
DataTable dt = new DataTable();
OleDbConnection myconn = new OleDbConnection();
myconn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\db\users.mdb;User Id=;Password=;";
string sql = "select userid,username,password,enable,trycount from passwd order by username";
OleDbDataAdapter adap = new OleDbDataAdapter(sql, myconn);
try
{
myconn.Open();
adap.Fill(dt);
}
catch (Exception)
{
}
finally
{
myconn.Close();
}
GridView1.DataSource = dt;
GridView1.DataKeyNames = new string[] { "UserID" };
GridView1.DataBind();
} protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string username = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string passwd = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
bool enable = ((CheckBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Checked;
string trycount = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
int flag;
if (enable == true)
{
flag = 1;
}
else
{
flag = 0;
}
string sql = "update passwd set [Username]= '"+username+"',[password]='"+passwd+"',[enable]='"+flag+"',[trycount]='"+trycount+"' where [UserID]=" + Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "";
OleDbConnection myconn = new OleDbConnection();
myconn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\db\users.mdb;User Id=;Password=;";
OleDbCommand command = new OleDbCommand(sql, myconn);
try
{
myconn.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
myconn.Close();
}
GridView1.EditIndex = -1;
bind();
} protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
}
public partial class _Default : System.Web.UI.Page
{
public void bind()
{
DataTable dt = new DataTable();
OleDbConnection myconn = new OleDbConnection();
myconn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\db\users.mdb;User Id=;Password=;";
string sql = "select userid,username,password,enable,trycount from passwd order by username";
OleDbDataAdapter adap = new OleDbDataAdapter(sql, myconn);
try
{
myconn.Open();
adap.Fill(dt);
}
catch (Exception)
{
}
finally
{
myconn.Close();
}
GridView1.DataSource = dt;
GridView1.DataKeyNames = new string[] { "UserID" };
GridView1.DataBind();
} protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string username = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string passwd = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
bool enable = ((CheckBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Checked;
string trycount = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
int flag;
if (enable == true)
{
flag = 1;
}
else
{
flag = 0;
}
string sql = "update passwd set [Username]= '"+username+"',[password]='"+passwd+"',[enable]='"+flag+"',[trycount]='"+trycount+"' where [UserID]=" + Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "";
OleDbConnection myconn = new OleDbConnection();
myconn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\db\users.mdb;User Id=;Password=;";
OleDbCommand command = new OleDbCommand(sql, myconn);
try
{
myconn.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
myconn.Close();
}
GridView1.EditIndex = -1;
bind();
} protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
}
操作必须使用一个可更新的查询。”,查询语句应该未执行
你试试像下面这样去拿控件 (GridView1.Rows[GridView1.EditIndex].FindControl("txtStuName") as TextBox).Text;