My MS Access database won't update asp.net

2019-03-06 07:52发布

I'm trying to update my database without success. This is how my table looks:

https://i.stack.imgur.com/Q6EDk.png

After opening the modal its look like this:

https://i.stack.imgur.com/JusUp.png

and all the table works with a repeater, this is my code.

protected void Btnupdate_Click(object sender, EventArgs e)
{
    try
    {
        foreach (RepeaterItem RI in rptEdit.Items)
        {
            Label id = 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 ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Table.accdb";
            string SqlString = "UPDATE ProductList SET Pname = ?";

            using (OleDbConnection conn = new OleDbConnection(ConnString))
            {
                using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("Pname", prdname.Text);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    ltl.Text = "<br/>Done.";
                }
            }
        }
    }
    catch (OleDbException ex)
    {
        foreach (RepeaterItem RI in rptEdit.Items)
        {
            Literal ltl = RI.FindControl("ltlmsg") as Literal;
            ltl.Text = ex.Message;
        }
    }
}

When I click the update button, it's refreshing but nothing happens.

Please help.

1条回答
劫难
2楼-- · 2019-03-06 08:45
you just check your update query:
string SqlString = "UPDATE ProductList SET Pname = ?";

change to this:
string SqlString = "UPDATE ProductList SET Pname =@Pname ";

and change command parameter to pass scaler variable:
 cmd.Parameters.AddWithValue("@Pname", prdname.Text);
查看更多
登录 后发表回答