这个错误是什么? ExecuteNonQuery:CommandText属性尚未初始化

时间:2014-08-21 16:50:23

标签: executenonquery

请帮帮我们。它在没有当前密码和新密码的情况下更新时工作正常但是如果我将包括更新密码,则会引发错误。这是我的代码:

    protected void UpdateBtn_Click(object sender, EventArgs e)
    {
        connection.Open();
        int queryStrId = Convert.ToInt32(Request.QueryString.Get("Id"));
        string query = "";

// Works fine here..
        if (CurrPass.Text == "" && NewPass.Text == "")
        {
            query = "UPDATE RegDetails SET Firstname = '" + FirstName.Text + "', Lastname = '" + LastName.Text + "', Gender = " + gender + ", AddressLocation = '" + Address.Text + "', Position = '" + Position.Text + "', CurrentStatus = " + status + ", Username = '" + Username.Text + "' WHERE Id = " + queryStrId;
        }
// Does not work. Display error "ExecuteNonQuery: CommandText property has not been initialized". What's wrong with this?
        else
        {
            if (CurrPass.Text != "" && NewPass.Text != "" && CurrPass.Text == currentPassword)
            {
                query = "UPDATE RegDetails SET Firstname = '" + FirstName.Text + "', Lastname = '" + LastName.Text + "', Gender = " + gender + ", AddressLocation = '" + Address.Text + "', Position = '" + Position.Text + "', CurrentStatus = " + status + ", Username = '" + Username.Text + "', Password = '" + NewPass.Text + "' WHERE Id = " + queryStrId;
            }
        }

        SqlCommand commandUpdate = new SqlCommand(query, connection);
        commandUpdate.ExecuteNonQuery();
        connection.Close();
        Response.Redirect("Details.aspx");
    }

1 个答案:

答案 0 :(得分:1)

这必须评估为false:

if (CurrPass.Text != "" && NewPass.Text != "" && CurrPass.Text == currentPassword)

另外,为了皮特而使用参数化的SQL。