user2505748
user2505748

Reputation: 25

Database update error No Value given for one or more required parameters

I was trying to use the update command to update my database at ms access and there is an error of No Value given for one or more required parameters whenever i try to execute it.

This is my code

private void btnupdate_Click_1(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ShopRecords.accdb");
        OleDbDataAdapter ad = new OleDbDataAdapter();
        try
        {
            ad.UpdateCommand = new OleDbCommand("UPDATE ShopRecords SET ProductDescription = '" +tbproductdescrip.Text + "' WHERE (ID= " + tbupdate.Text + ")", con);

            con.Open();
            ad.UpdateCommand.ExecuteNonQuery();
            con.Close();
        }
         catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Upvotes: 1

Views: 1065

Answers (1)

Unix von Bash
Unix von Bash

Reputation: 730

try following the next structure:

try
    {
        using (OleDbConnection con = new OleDbConnection(cs))
        {
            con.Open();
            OleDbTransaction tran = con.BeginTransaction();
            OleDbCommand cmd = new OleDbCommand("UPDATE ... SET ... WHERE ...", con);
            cmd.Transaction = tran;
            cmd.ExecuteNonQuery();
            tran.Commit();
            con.Close();
        }
    }
    catch (OleDbException ex)
    {
        Console.WriteLine(ex);
    }

also, a good example: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbtransaction.commit%28v=vs.110%29.aspx

Upvotes: 1

Related Questions