解决方案 »

  1.   

     protected void Page_Load(object sender, EventArgs e)
        {
            string usname = Session["username"].ToString();
            DropDownList1.Items.Add("你的名字?");
            DropDownList1.Items.Add("你喜欢的人?");
            DropDownList1.Items.Add("你的生日?");
            string str = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bookshop.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            Label2.Text = usname;
            string sql = "select * from users where userName='" + Label2.Text + "'";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            TextBox1.Text = reader["truename"].ToString();
            RadioButtonList1.SelectedValue = reader["Sex"].ToString();
            TextBox2.Text = reader["answer"].ToString();
            DropDownList1.SelectedValue = reader["question"].ToString();
            TextBox3.Text = reader["email"].ToString();
            con.Close();    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string usname = Session["username"].ToString();
                string str = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bookshop.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                SqlConnection con = new SqlConnection(str);
                con.Open();
                string sql = "update users set truename=@truename,sex=@sex,answer=@answer,question=@question,email=@email where  userName='" + usname + "'";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@trusname", TextBox1.Text);
                cmd.Parameters.AddWithValue("@sex", RadioButtonList1.SelectedValue);
                cmd.Parameters.AddWithValue("@answer", TextBox2.Text);
                cmd.Parameters.AddWithValue("@email", TextBox3.Text);
                cmd.Parameters.AddWithValue("@question", DropDownList1.SelectedValue);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException e1)
                {
                    Response.Write(e1);
                }
                Response.Write("<script>alert('修改成功')</script>");
                Response.Redirect("main.aspx");
            }
        }
    代码
      

  2.   

    按照提示错误 应该是没有绑定数据源 或者绑定空的数据源 而又指定SelectValue的原因.
    你看看.
      

  3.   

    添加dropdownlist元素的时候 用下别的重载函数试试 你这里不对 
    /*
    DropDownList1.Items.Add(new ListItem(字段));
    添加的是ListItem类型的数据
    DropDownList1.Items.Add(字段)
    添加的是String类型的数据
    因为DropDownList1.Items.Add方法下有ListItem和String等多种重载方式
    实现的功能是完全一样的.
    */改下试试.
      

  4.   

    调试下绑定dropDownList绑定那块,或者你的参数有问题。原因就是dropDownList不存在你选中的那一项
      

  5.   

    调试一下DropDownList有哪些选项,而你指定选择的值在选项中是否存在,错误提示应该是你指定了下拉列表选择了一个不存在的值
      

  6.   

    看看你的DropDownList里面有没有对应的reader["Question"]值,
    这个错误应该是reader["Question"]的值,DropDownList里面没有造成的
      

  7.   

    这个错误应该是reader["Question"]的值,DropDownList里面没有造成的 
      

  8.   

    这个问题我也遇到过,你这样试试:
    DropDownList1.SelectedItem.Text
      

  9.   

    获取列表控件中选定项的值,或选择列表控件中包含指定值的项。 (继承自 ListControl。)这个属性只能取值不能赋值。
      

  10.   

    看错误 明显是你塞给dropdownlist的值 不存在那个dropdownlist里面