提交C#后更新语句不改变任何内容

时间:2015-05-05 19:03:40

标签: c# sql-update tableadapter

我要做的是获取一些用户的数据,向他显示,如果他/她想要,她可以更改一些值,然后在按钮点击时提交更改。我正在使用表自适应,并且在这里使用的那个我生成了一个UpdateQuery,它只需要几个db的列和ID来引用该行。

在页面加载时,我从数据库中获取数据以使用信息填充文本框。然后在按钮点击我分配文本框'值到局部变量并尝试使用它们更新数据库。代码通过UpdateQuery()运行而不会捕获任何异常,但不会更改任何内容。

如果我在生成Query builder时从UpdateQuery运行查询,则一切顺利并进行更改。我错过了什么?以下是此解释背后的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        UserDetailsTableAdapters.UserDetailsTableAdapter userDetails = new UserDetailsTableAdapters.UserDetailsTableAdapter();
        UserDetails.UserDetailsDataTable loginDetailsTable = userDetails.GetData(); 

        foreach (DataRow row in loginDetailsTable.Rows)
        { 
            Int32 id = System.Convert.ToInt32(Request.Cookies["UserInformation"]["ID"]);
            if (id == System.Convert.ToInt32(row["ID"])) 
            {
                txt_email.Text = System.Convert.ToString(row["email"]);
                txt_address.Text = System.Convert.ToString(row["address"]);
                txt_number.Text = System.Convert.ToString(row["mobileNum"]);
                txt_firstName.Text = System.Convert.ToString(row["firstName"]);
                txt_lastName.Text = System.Convert.ToString(row["lastName"]);
            }
        }
        lbl_info.Text = System.Convert.ToString(Request.Cookies["UserInformation"]["ID"]);//this is here to check that the right cookie ID is passed.
    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        UserDetailsTableAdapters.UserDetailsTableAdapter userDetails = new UserDetailsTableAdapters.UserDetailsTableAdapter();
        UserDetails.UserDetailsDataTable loginDetailsTable = userDetails.GetData(); 

        string email = txt_email.Text;
        string address = txt_address.Text;
        string number = txt_number.Text;
        string firstName = txt_firstName.Text;
        string lastName = txt_lastName.Text;

        foreach (DataRow row in loginDetailsTable.Rows)
        { 
            Int32 id = System.Convert.ToInt32(Request.Cookies["UserInformation"]["ID"]);
            if (id == System.Convert.ToInt32(row["ID"])) 
            {
                try
                {
                    userDetails.UpdateQuery(email, address, number, firstName, lastName, id);
                    //Response.Redirect("MyProfile.aspx");
                }
                catch (Exception Exception)
                {
                    btn_submit.Text = Exception.ToString();
                }
            }
        }
    }
}

修改:我的自定义UpdateQuery如下所示:

UPDATE UserDetails
SET    email = ?, address = ?, mobileNum = ?, firstName = ?, lastName = ?
WHERE  (ID = ?)

通过tableAdapter的查询生成器完成,并在查询配置向导中执行时运行良好。 UpdateQuery方法需要6个元素:email, address, mobileNum, firstName, lastNameID

0 个答案:

没有答案