我的MS访问更新一行

时间:2018-05-29 16:02:43

标签: asp.net ms-access

我的MS访问更新了一行,但在尝试更新其他值后, 它的工作但最后一个值被删除似乎是什么问题? 感谢所有帮助的人。

protected void Btnupdate_Click(object sender, EventArgs e)
{
    try
    {
        foreach (RepeaterItem RI in rptEdit.Items)
        {
            Label Pid = RI.FindControl("Pid") as Label;
            TextBox prdname = RI.FindControl("prdname") as TextBox;
            TextBox prdprice = RI.FindControl("prdprice") as TextBox;
            TextBox prdshortdesc = RI.FindControl("prdshortdesc") as TextBox;
            TextBox prdtype = RI.FindControl("prdtype") as TextBox;
            TextBox prdbrand = RI.FindControl("prdbrand") as TextBox;
            Literal ltl = RI.FindControl("ltlmsg") as Literal;
            string myid = Pid.Text;

            string ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Table.accdb";
            string SqlString = "UPDATE ProductList SET Pname = '" + prdname.Text + "' WHERE Pid = ? ";

            using (OleDbConnection conn = new OleDbConnection(ConnString))
            {
                using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
                {
                    conn.Open();
                    cmd.Parameters.AddWithValue("Pid", OleDbType.Integer).Value = myid;
                    cmd.Parameters.AddWithValue("@Pname", prdname.Text);
                    cmd.ExecuteNonQuery();
                    ltl.Text = "<br/>Done.";
                    conn.Dispose();
                    conn.Close();
                }
            }
        }
    }
    catch (Exception ex)
    {
        foreach (RepeaterItem RI in rptEdit.Items)
        {
            Literal ltl = RI.FindControl("ltlmsg") as Literal;
            ltl.Text = "Error messegae: <br/>" + ex.Message;
        }
    }
}

0 个答案:

没有答案