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