简体   繁体   中英

Getting Error while trying to update record in Ms Access 2007 using c#

Trying update record dont know why i am getting error Exception Details: System.Data.OleDb.OleDbException: No value given for one or more required parameters.

this is my code please guide me.

  public static string lasttable;
  public static string newtable;

  newtable = "c" + cont.ToString();
  lasttable = input;

 string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AGENTJ.AGENTJ-PC\Documents\Visual Studio 2010\WebSites\mfaridalam\App_Data\mfaridalam1.accdb;    Persist Security Info=False;";
      OleDbConnection conn = new OleDbConnection(connectionString);
                    conn.Open();
        string query = "UPDATE [LastInfo] SET [LastAlbum]=@newtable WHERE [LastAlbum]=@lasttable";
        OleDbCommand comd = new OleDbCommand();
        comd.Parameters.Add("@LastAlbum", OleDbType.VarChar);
        comd.Parameters["@LastAlbum"].Value = newtable;
        comd.CommandText = query;
        comd.Connection = conn;
        comd.ExecuteNonQuery();
        conn.Close();   

You are using OleDb and OleDb doesn't care about parameter names.
However you need to add a parameter for every placeholder present in the command text and in the same order in which they appear.
You have two parameter ( @newtable and @lasttable ) but you add just one parameter (and you name it wrongly, but, as I have said, that doesn't matter for OleDb).

You need to add the second parameter @lasttable

  string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AGENTJ.AGENTJ-PC\Documents\Visual Studio 2010\WebSites\mfaridalam\App_Data\mfaridalam1.accdb;    Persist Security Info=False;";
  using(OleDbConnection conn = new OleDbConnection(connectionString))
  {
      conn.Open();
      string query = "UPDATE [LastInfo] SET [LastAlbum]=@newtable WHERE [LastAlbum]=@lasttable";
      OleDbCommand comd = new OleDbCommand();

      comd.Parameters.Add("@newTable", OleDbType.VarChar);
      comd.Parameters["@newTable"].Value = newtable;

      comd.Parameters.Add("@lastTable", OleDbType.VarChar);
      comd.Parameters["@lastTable"].Value = lasttable;

      comd.CommandText = query;
      comd.Connection = conn;
      comd.ExecuteNonQuery();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM