using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class info : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=SSPI;");
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TextBox5.Text = Convert.ToString(Session["usename"]);
        conn.Open();
        SqlCommand bb = new SqlCommand("select * from logininfo where usename='" + this.TextBox5.Text + "'", conn);
        SqlDataReader cc = bb.ExecuteReader();
        if (cc.Read())
        {
            this.TextBox2.Text = cc["usepwd"].ToString();
            this.TextBox3.Text = cc["email"].ToString();
            this.TextBox4.Text = cc["photo"].ToString();
            string b;
            b = cc["sex"].ToString();
            if (b=="男")
                this.DropDownList1.SelectedValue = "男";
            else
                this.DropDownList1.SelectedValue = "女";
            }
            conn.Close();
        }
    protected void Button1_Click(object sender, EventArgs e)
    {        conn.Open();
        SqlCommand cmd = new SqlCommand("update logininfo set usepwd='" + this.TextBox2.Text.ToString() + ",sex='" + this.DropDownList1.SelectedValue.ToString() + "',email='" + this.TextBox3.Text.ToString() + "',photo='" + this.TextBox4.Text.ToString() + "' where usename='"+this.TextBox5.Text+"'", conn);
        SqlCommand cmd = new SqlCommand();
        cmd.ExecuteNonQuery();
        conn.Close();
        Response.Write("<script>alert('修改成功,返回登录!');location.href='login.aspx';</script>");
    }
}经过几次测试 发现update logininfo set usepwd='" + this.TextBox2.Text.ToString() + ",sex='" + this.DropDownList1.SelectedValue.ToString() + "',email='" + this.TextBox3.Text.ToString() + "',photo='" + this.TextBox4.Text.ToString() + "' 有错,传值不了 ,若我换成实参就可以。
数据库是
CREATE TABLE [master].[dbo].[logininfo] (
[id] int NOT NULL, 
[usename] nvarchar (50) NOT NULL, 
[usepwd] varchar (50) NOT NULL, 
[sex] char (50) NOT NULL, 
[email] varchar (50) NULL, 
[photo] varchar (50) NULL
)
求助,弄了一个下午,不知道怎么弄啊!!!!!!

解决方案 »

  1.   

    update logininfo set usepwd='" + this.TextBox2.Text.ToString() + "',sex='" + this.DropDownList1.SelectedValue.ToString() + "',email='" + this.TextBox3.Text.ToString() + "',photo='" + this.TextBox4.Text.ToString() + "'"你的usepwd少了一个单引号!
      

  2.   

    update logininfo set usepwd='" + this.TextBox2.Text.ToString() + "',sex='" + this.DropDownList1.SelectedValue.ToString() + "',email='" + this.TextBox3.Text.ToString() + "',photo='" + this.TextBox4.Text.ToString() + "'
      

  3.   

    楼上正解~~其实这种错误你自己调试一下就知道了么
    SqlCommand cmd = new SqlCommand("update logininfo set usepwd='" + this.TextBox2.Text.ToString() + "',sex='" + this.DropDownList1.SelectedValue.ToString() + "',email='" + this.TextBox3.Text.ToString() + "',photo='" + this.TextBox4.Text.ToString() + "' where usename='"+this.TextBox5.Text+"'", conn);
    这一段你拆成两句:
    string updatesql="update logininfo set usepwd='" + this.TextBox2.Text.ToString() + "',sex='" + this.DropDownList1.SelectedValue.ToString() + "',email='" + this.TextBox3.Text.ToString() + "',photo='" + this.TextBox4.Text.ToString() + "' where usename='"+this.TextBox5.Text+"'";
    SqlCommand cmd = new SqlCommand(updatesql, conn);
    断点设在stirng updatesql那里,你就会知道啦。
    自己动手 丰衣足食·
      

  4.   

    最好把这类常用的select,insert,update,delete方法,封装起来,只要传参,sql语句的拼接,根据字段名,字段类型,写共通的方法自动拼接,对于外界使用者,只要知道增删改查4个方法就行了~手动写sql语句,就容易出问题
      

  5.   

     Response.Write("<script>alert('修改成功,返回登录!');location.href='login.aspx';</script>");
    这句还有错吧
      

  6.   


    [usename] nvarchar (50) NOT NULL, "update logininfo set usepwd='" + this.TextBox2.Text.ToString() + ",sex='" + this.DropDownList1.SelectedValue.ToString() + "',email='" + this.TextBox3.Text.ToString() + "',photo='" + this.TextBox4.Text.ToString() + "' where usename=N'"+this.TextBox5.Text+"'"是不是因为少了个 N ?
      

  7.   

    打断点,看看,你那条sql语句到底生成什么了!
      

  8.   

    这种问题,直接把SQL语句 write出来,执行下就知道是不是语句的问题
      

  9.   

    楼主提醒一下,你没有根据SqlCommand cmd = new SqlCommand();
      cmd.ExecuteNonQuery();
      conn.Close();
      Response.Write("<script>alert('修改成功,返回登录!');location.href='login.aspx';</script>");
    数据库的返回值就能说明修改成功了
      

  10.   

    update logininfo set usepwd='11',sex='女',email='[email protected]',photo='12345678' where usename='11' 
    原来我是输入22 男 的
      发现语句是没问题的    只是按提交按钮后 把我原先的数据给还原了 没赋值上   所以数据库没改到杯具!!!
      

  11.   

    狂汗  多了句SqlCommand cmd = new SqlCommand();由于 我这边代码用了好多方法 复制是没注意 不好意思!!!
      

  12.   

    update logininfo set usepwd='" + this.TextBox2.Text.ToString() + "',sex='" + this.DropDownList1.SelectedValue.ToString() + "',email='" + this.TextBox3.Text.ToString() + "',photo='" + this.TextBox4.Text.ToString() + "'
      

  13.   

    protected void Page_Load(object sender, EventArgs e)
    {
    if(!ispostback){
    ......}}