我用GridView 对某一项进行编辑时,数据库不能获取编辑后的值,是怎么回事,谁能帮帮我呢?
问一下下面代码里有没有错误啊!
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class backmanage_memberscoremanage : System.Web.UI.Page
{
SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn"]);
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["admin"] == null)
{
Server.Transfer("backlogin.aspx");
}
dsdatabind();
}
protected void dsdatabind()
{
strcon.Open();
string str = "SELECT id,username,score,address,tel from tb_users order by score desc ";
SqlDataAdapter datap = new SqlDataAdapter(str, strcon);
datap.Fill(ds);
GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
strcon.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
strcon.Open();
string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
SqlCommand comm = new SqlCommand("delete from tb_users where id=" + id, strcon);
comm.ExecuteNonQuery();
strcon.Close();
dsdatabind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
dsdatabind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
dsdatabind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
strcon.Open();
string uid = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = this.GridView1.Rows[e.RowIndex];
string str = "update tb_users set score='" + ((TextBox)(row.Cells[2].Controls[0])).Text.ToString() + "' where id='" + uid+"'";
SqlCommand goodsin = new SqlCommand(str, strcon);
goodsin.ExecuteNonQuery();
strcon.Close();
GridView1.EditIndex = -1;
dsdatabind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
dsdatabind(); }
}
问一下下面代码里有没有错误啊!
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class backmanage_memberscoremanage : System.Web.UI.Page
{
SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn"]);
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["admin"] == null)
{
Server.Transfer("backlogin.aspx");
}
dsdatabind();
}
protected void dsdatabind()
{
strcon.Open();
string str = "SELECT id,username,score,address,tel from tb_users order by score desc ";
SqlDataAdapter datap = new SqlDataAdapter(str, strcon);
datap.Fill(ds);
GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
strcon.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
strcon.Open();
string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
SqlCommand comm = new SqlCommand("delete from tb_users where id=" + id, strcon);
comm.ExecuteNonQuery();
strcon.Close();
dsdatabind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
dsdatabind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
dsdatabind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
strcon.Open();
string uid = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = this.GridView1.Rows[e.RowIndex];
string str = "update tb_users set score='" + ((TextBox)(row.Cells[2].Controls[0])).Text.ToString() + "' where id='" + uid+"'";
SqlCommand goodsin = new SqlCommand(str, strcon);
goodsin.ExecuteNonQuery();
strcon.Close();
GridView1.EditIndex = -1;
dsdatabind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
dsdatabind(); }
}
解决方案 »
- 为什么保存cookie的方法上传到服务器报错
- lifetype问题
- GridView动态绑定 数据库nvarchar类型数据 实现不了分段?
- 如何将一个Flash放在页面上啊
- 在GridView中插入或删除后的数据绑定!!!!
- 如何在数据导入,导出的时候,用进度条显示进度~!
- 20 新手问个问题:首页登陆提交并验证用户名名和密码怎么做(用Form 的 action吗? 还是……)
- 怎么附值,谢谢(c#)
- 我的机器运行aspx错误,请教!错误信息如下
- 我在C#中用FileStream对象建一个ASP文件,然后用StreamWriter对象向这个文件写中文,打开文件一看,全是乱码??
- oracle10g文件夹共享问题
- 多帧图片如何加水印?
if(!Page.IsPostBack)
{
//你的代码
}