读书人

关于GridView与SQL的疑点

发布时间: 2013-11-29 13:49:33 作者: rapoo

关于GridView与SQL的疑问
初学SQL,老师叫我们可以自己去弄一个登录注册的网页,我对我的网页很满意,只是碰到了一个重大缺陷,为什么我这个不能修改数据库的数据啊,程序如下:


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.Data;

public partial class information : System.Web.UI.Page
{
private string number = HttpContext.Current.Request.QueryString["num"];//从登录窗口取得登陆成功的号码
private string name;
private static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
GetData();
SqlConnection con = new SqlConnection("Server=localhost;User=sa;database=mylib;password=112233456");
SqlCommand cmd = new SqlCommand("select 昵称 from QQnum where 账号='" + number + "'", con);//取得登陆成功号码的昵称
con.Open();
name = Convert.ToString(cmd.ExecuteScalar());
con.Close();
if (i == 0)//如果第一次登录,显示**欢迎回来
{
Label1.Text = "亲爱的" + name + ",欢迎回来!";
con.Close();
}
else
Label1.Text = name+"个人资料";
}
public void GetData()//刷新页面
{
SqlConnection con = new SqlConnection("Server=localhost;User=sa;database=mylib;password=112233456");
SqlCommand cmd = new SqlCommand("Select * from QQnum where 账号='"+number+"'",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds,"QQnum");
GridView1.DataSource=ds.Tables[0].DefaultView;
this.GridView1.DataBind();

}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)//按下更新按钮时
{
this.GridView1.EditIndex = 1;
string j;
string name = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();//从当前行提取出第2个个元素,重点是这个,返回的居然只是原本的值,并没修改,整个页面这个肯定错了,怎么改呢?
string passwd=((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string sex=((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
string info = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
SqlConnection con = new SqlConnection("Server=localhost;User=sa;Database=mylib;Password=112233456");
SqlCommand cmd = new SqlCommand("update QQnum set 昵称='"+name+"',密码='"+passwd+"',性别='"+sex+"',个人说明='"+info+"' where 账号= '"+number+"'", con);
con.Open();
j=cmd.ExecuteNonQuery().ToString();
con.Close();
i++;
this.GridView1.EditIndex = -1;
GetData();
Label1.Text = "修改成功";
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;


i++;
GetData();
Label1.Text = "修改资料";

}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)//注销账户
{
SqlConnection con = new SqlConnection("Server=localhost;User=sa;database=mylib;password=112233456");
SqlCommand cmd = new SqlCommand("delete from QQnum where 账号='" + number + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("~/login.aspx");
}

protected void Button1_Click1(object sender, EventArgs e)
{
i++;
GetData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
i++;
GetData();
}
}

GridView SQL
[解决办法]
写在GridView1_RowUpdated里面
[解决办法]
这个是很多人刚用到GridView控件时都会遇到的,我前段时间也遇到了这种问题,后来解决了,这是就是要把数据绑定放在Page_Load下的if(!IsPostBack){}里,这个IsPostBack是个很奇妙的东西,特别是对于GridView控件,希望你找点这方面的资料好好看看,很有用的。
[解决办法]
引用:
Quote: 引用:

写在GridView1_RowUpdated里面

行吗,改了显示e.RowIndex 都没定义!


当然你都要做相应的修改。RowUpdated在更新后触发,才能取到更新的值。
[解决办法]
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt32(Convert.ToInt32(this.GridView1.SelectedValue));//获取选中行的ID
//后面是处理数据
}

用这个事件取id
[解决办法]
if (!Page.IsPostBack)
在Page_Load里不加上,你会发现很有问题

读书人网 >asp.net

热点推荐