为什么我的输入数据无法在数据库中更新?

时间:2016-08-16 01:31:29

标签: c# asp.net

我目前正在建立一个asp.net网站,注册用户可以在名为“编辑个人资料”的页面中更新他们的详细信息。

“编辑个人资料”页面的工作方式是注册用户的数据将通过文本框显示,数据通过会话功能显示。

然后,用户可以通过相同的文本框编辑他们的数据,并且应该在数据库中更新特定用户的新输入数据。

这是我的.cs文件中的代码......

int profileid;
string username = "";
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if(Session["NonAdmin"] !=null)
        {
            getlogininfo();
            getInfo();
        }
    }
    else
    {
        getInfo();
    }
}   

private void getlogininfo()
{
    username = (String)Session["NonAdmin"];
    MySqlConnection mcon = new MySqlConnection("My connectionString");
    MySqlCommand command = mcon.CreateCommand();
    mcon.Open();
    command.CommandText = "SELECT Username FROM pointofcontact WHERE Username='" + tbUsername.Text + "'";
    MySqlDataReader reader = command.ExecuteReader(); 
    while (reader.Read())
    {
        string u = reader["Username"].ToString();
        if(username == u)
        {
            profileid = Convert.ToInt32(reader["POCID"]);
        }
    }
}

private void getInfo()
{
    username = (String)Session["NonAdmin"];
    MySqlConnection mcon = new MySqlConnection("server=182.50.133.91;user id=Jonathan;password=****;persistsecurityinfo=True;database=ajactrac_;allowuservariables=True");
    MySqlCommand command = mcon.CreateCommand();
    mcon.Open();
    command.CommandText = "SELECT * from pointofcontact WHERE Username ='" + username + "'";
    MySqlDataReader reader = command.ExecuteReader();
    reader.Read();
    tbUsername.Text = reader["Username"].ToString();
    tbOldPassword.Text = reader["Password"].ToString();
    tbFirstName.Text = reader["FirstName"].ToString();
    tbLastName.Text = reader["LastName"].ToString();
    tbMobile.Text = reader["ContactNumber"].ToString();
    tbEmail.Text = reader["EmailAddress"].ToString();
    tbAddress.Text = reader["Address"].ToString();
    tbBackupContact.Text = reader["BackupContactNumber"].ToString();





}

protected void btnContinue_Click(object sender, EventArgs e)
{
    //Declaration of variable to update Profile Image
    string imageName, newContact;
    imageName = FileUpload1.FileName.ToString();
    newContact = tbMobile.Text.ToString();

    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + imageName);

    username = (String)Session["NonAdmin"];
    MySqlConnection mcon = new MySqlConnection("server=182.50.133.91;user id=Jonathan;password=****;persistsecurityinfo=True;database=ajactrac_;allowuservariables=True");
    MySqlDataAdapter sda = new MySqlDataAdapter("select * from pointofcontact where Username = '" + username.ToString() + "'", mcon);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    if (dt.Rows.Count.ToString() == "1")
    {

        MySqlCommand command = mcon.CreateCommand();
        MySqlCommand command1 = mcon.CreateCommand();
        MySqlCommand command2 = mcon.CreateCommand();
        MySqlCommand command3 = mcon.CreateCommand();
        MySqlCommand command4 = mcon.CreateCommand();
        MySqlCommand command5 = mcon.CreateCommand();
        MySqlCommand command6 = mcon.CreateCommand();
        MySqlCommand command7 = mcon.CreateCommand();

        command.CommandText = "update pointofcontact set Password = ?pwd where Username = '" + username.ToString() + "'";
        command1.CommandText = "update pointofcontact set FirstName = ?firstname where Username = '" + username.ToString() + "'";
        command2.CommandText = "update pointofcontact set LastName = ?lastname where Username = '" + username.ToString() + "'";
        command3.CommandText = "update pointofcontact set ContactNumber = ?contact where Username = '" + username.ToString() + "'";
        command4.CommandText = "update pointofcontact set EmailAddress = ?email where Username = '" + username.ToString() + "'";
        command5.CommandText = "update pointofcontact set Address = ?address where Username = '" + username.ToString() + "'";
        command6.CommandText = "update pointofcontact set BackupContactNumber = ?backupnumber where Username = '" + username.ToString() + "'";
        command7.CommandText = "update pointofcontact set ProfilePic = ?newimage where Username = '" + username.ToString() + "'";

        mcon.Open();
        if(tbNewPassword.Text == null)
        {
            command.Parameters.AddWithValue("?pwd", tbOldPassword.Text.Trim());
        }
        else
        {
            command.Parameters.AddWithValue("?pwd", tbNewPassword.Text.Trim());
        }
        command1.Parameters.AddWithValue("?firstname", tbFirstName.Text.Trim());
        command2.Parameters.AddWithValue("?lastname", tbLastName.Text.Trim());
        command3.Parameters.AddWithValue("?contact", tbMobile.Text.Trim());
        command4.Parameters.AddWithValue("?email", tbEmail.Text.Trim());
        command5.Parameters.AddWithValue("?address", tbAddress.Text.Trim());
        command6.Parameters.AddWithValue("?backupnumber", tbBackupContact.Text.Trim());
        command7.Parameters.AddWithValue("?newimage", imageName);





        command.ExecuteNonQuery();
        command1.ExecuteNonQuery();
        command2.ExecuteNonQuery();
        command3.ExecuteNonQuery();
        command4.ExecuteNonQuery();
        command5.ExecuteNonQuery();
        command6.ExecuteNonQuery();
        command7.ExecuteNonQuery();

        mcon.Close();

但是当我检查数据库时,唯一更新的是密码。

感谢我对代码的任何帮助,因为我自己不知道如何修复我的代码。

感谢。 :)

0 个答案:

没有答案