Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9229523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:35:35+00:00 2026-06-18T05:35:35+00:00

I am trying to update a record in an access file (.accdb). I am

  • 0

I am trying to update a record in an access file (.accdb). I am trying to use the .net OleDbCommand and OleDbParameters. I am also trying to use a generic model and store all of the commands and parameters in the System.Data.Common abstract equivalents so that I can easily switch over to SQL Server (which I do plan to do)

So here is the actual command being used

EDIT 2/2/2013 – 9:10pm
the command.ExecuteNonQuery is inside the method named ExecuteNonQuery()
the connectionString and command are defined in the DataAccess class constructor

public class DataAccess
{

    private string connectionString;
    private DbConnection connection;
    private DbCommand command;
    private DbDataReader reader;
    private DataTable data;

    public DataAccess()
    {
        connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

        switch (ConfigurationSettings.AppSettings["DataBaseType"])
        {
            case "oledb":
                connection = new OleDbConnection(connectionString);
                command = new OleDbCommand(string.Empty, (OleDbConnection)connection);
                break;
            case "SQL":                 
                connection = new SqlConnection(connectionString);
                command = new SqlCommand(string.Empty, (SqlConnection)connection);
                break;
            default:
                break;
        }

    }

    public void ExecuteNonQuery(string SQL, params DbParameter[] parameters)
    {
        command.CommandType = CommandType.Text;
        command.CommandText = SQL;
        command.Parameters.AddRange(parameters);

        try
        {
            command.Connection.Open();

            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                command.Connection.Close();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public DbParameter NewParameter(string name, object value)
    {
        DbParameter param;

        switch (ConfigurationSettings.AppSettings["DataBaseType"])
        {
            case "oledb":
                param = new OleDbParameter(name, value);
                break;
            case "SQL":
                param = new SqlParameter(name, value);
                break;
            default:
                param = null;
                break;
        }

        return param;
    }

These are the properties in the App.Config File

<add key="DataBaseType" value="oledb"/>

<add key="ConnectionString" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=data.accdb"/>

Now the problem is when using parameters in an update statement, the update never happens and also never throws an error. Here is the code for it.

EDIT 2/2/2013 – 9:10pm
the function DataAccess.NewParameter is in the first code block

DALayer.ExecuteNonQuery("UPDATE TileTypes SET Title = @Title, Picture = @Picture, Color = @Color WHERE ID = @ID",
 DALayer.NewParameter("@Title", titleTextBox.Text.Trim()),
 DALayer.NewParameter("@Picture", typePictureBox.ImageLocation),
 DALayer.NewParameter("@Color", colorButton.BackColor.ToArgb()),
 DALayer.NewParameter("@ID", id));

I have copied the query into access and replaced all of the parameter names with the actual data being passed, this works fine. I have tried replacing all of the parameters in the SQL text to the ? character to no effect. I have tried enclosing all of the table and column names in brackets [] also to no effect.

  • ID is an AutoNumber field
  • Title is a Text field
  • Picture is a Text field
  • Color is a Long Integer field

This is some example data that was copied directly from the parameters in the watch window for Visual Studio:

  • “Edit” (title)
  • -1 (color)
  • “data\images\Edit_000000.jpg” (picture)
  • 740 (id)

That ID does exist in the database and was unchanged after the query executed.

EDIT 2/2/2013 – 9:10pm
I am not sure how to check which database is actually being updated, the only thing I could think of was that using the same connection string and connection object I did an insert statement with the same ExecuteNonquery method and it worked in the database I was viewing. And the update statement works just fine like this (without parameters):

DALayer.ExecuteNonQuery("UPDATE TileTypes SET Title = '" + titleTextBox.Text + 
"', Color = " + colorButton.BackColor.ToArgb() + ", Picture = '" + 
imageLocation + "' WHERE ID = " + id);

EDIT 2/2/2013 – 9:41pm
I have used everything.exe to search my computer for all of the data.accdb files on my computer, I have found no actual .accdb files besides the original but I did find these .lnk files, I do not believe they could have altered this process but I will mention it anyway

data.accdb.LNK

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:35:36+00:00Added an answer on June 18, 2026 at 5:35 am

    What you are trying to do is something I too have done in the past, but allowed to connect to OleDB (such as Access, Visual FoxPro, etc), SQL-Server, SyBase SQLAnywhere and maybe my implementation might help you. First, each of the elements you would use for connecting work on a common interface, such as IDbConnection, IDbCommand, IDbParameter, etc.

    The following I’m posting is a small segment of how I originally structured such multi-database connection type. I’ve stripped a bunch out and not actually tested this stripped version, but it SHOULD be a good baseline for you to run with.

    The premise is a baseline “MyConnection” to almost be like an abstract, but has properties and some “common” methods that would exist under EITHER subclassed definition. From this, each of the functions and parameter types are based on the “I”nterface, not a specific. However, each of the derived will create its OWN proper type. This removes the need to “Case” everything. Hope this helps you along with your Data Access Layer development.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    // for OleDB (Access, VFP, etc)
    using System.Data.OleDb;
    // for SQL-Server
    using System.Data.SqlClient;
    
    namespace DataMgmt
    {
        public class MyConnection
        {
            // no matter the connection to server, it will require some "handle"
            // that is of type "IDbConnection"
            protected IDbConnection sqlConnectionHandle;
    
            // when querying, ANY query could have an exception that needs to have
            // possible further review for handling
            public Exception LastException
            { get; protected set; }
    
            // When calling an execute command (select, insert, update, delete), 
            // they all can return how many rows affected
            public int RowsAffectedByQuery
            { get; protected set; }
    
            // different databases could have different connection strings. Make
            // virtual and throw exception so sub-classed must return proper formatted.
            public virtual string GetConnectionString()
            { throw new Exception("GetConnectionString() method must be overridden."); }
    
            // each has its own "IDbConnection" type too
            protected virtual IDbConnection SQLConnectionHandle()
            { return sqlConnectionHandle; }
    
            public virtual IDbCommand GetSQLDbCommand()
            { throw new Exception("GetSQLDbCommand() method must be overridden."); }
    
            // generic routine to get a data parameter...
            public virtual IDbDataParameter AddDbParmSpecificValue(string ParmName, object UnknownValue)
            { throw new Exception("AddDbParmSpecificValue() method must be overwritten per specific connection."); }
    
            // generic "Connection" since they are all based on IDbCommand...
            public override bool SQLConnect()
            {
                // pre-blank exception in case remnant from previous activity
                LastException = null;
    
                if (sqlConnectionHandle.State != System.Data.ConnectionState.Open)
                    try
                    {
                        // if not open, always make sure we get updated connection string
                        // if ever changed by some other "unknown" condition...
                        sqlConnectionHandle.ConnectionString = GetConnectionString();
                        sqlConnectionHandle.Open();
                    }
                    catch (Exception ex)
                    {
                        // Preserve in generic sqlException" property for analysis OUTSIDE this function
                        LastException = ex;
                    }
    
                // if NOT connected, display message to user and set error code and exception
                if (sqlConnectionHandle.State != System.Data.ConnectionState.Open)
                    LastException = new Exception("Unable to open database connection.");
    
                // return if it IS successful at opening the connection (or was already open)
                return sqlConnectionHandle.State == System.Data.ConnectionState.Open;
            }
    
            // likewise disconnect could be common
            public void SQLDisconnect()
            {
                if (sqlConnectionHandle != null)
                    if (sqlConnectionHandle.State == ConnectionState.Open)
                        sqlConnectionHandle.Close();
            }
    
    
            public bool SqlExecNonQuery( IDbCommand SQLCmd, DataTable oTbl)
            {
                // pre-clear exception
                LastException = null;
    
                // fill the table...
                SQLConnect();
                try
                {
                    RowsAffectedByQuery = SQLCmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    LastException = e;
                    throw e;
                }
                finally
                {
                    SQLDisconnect();
                }
    
                // Its all ok if no exception error
                return LastException == null;
            }
    
        }
    
    
        // Now, build your connection manager per specific type
        public class MyAccessConnection : MyConnection
        {
            public MyAccessConnection()
            {   sqlConnectionHandle =  new OleDbConnection();   }
    
            public override string GetConnectionString()
            {   return "Your Connection String from AppSettings.. any changes if OleDb vs SQL"; }
    
            public override IDbCommand GetSQLDbCommand()
            {   return new OleDbCommand( "", (OleDbConnection)sqlConnectionHandle ); }
    
            public override IDbDataParameter AddDbParmSpecificValue(string ParmName, object UnknownValue)
            {   return new OleDbParameter( ParmName, UnknownValue );    }
    
        }
    
        public class MySQLConnection : MyConnection
        {
            public MySQLConnection()
            {   sqlConnectionHandle = new SqlConnection();  }
    
            public override string GetConnectionString()
            { return "Your Connection String from AppSettings... any alterations needed??? "; }
    
            public override IDbCommand GetSQLDbCommand()
            { return new SqlCommand ("", (SqlConnection)sqlConnectionHandle); }
    
            public override IDbDataParameter AddDbParmSpecificValue(string ParmName, object UnknownValue)
            { return new SqlParameter(ParmName, UnknownValue); }
        }
    
    
    
        // Now to implement... pick one... Access or SQL-Server for derivation...
        public class MyDataLayer : MyAccessConnection
        {
            public void SomeSQLCall()
            {
                IDbCommand sqlcmd = GetSQLDbCommand();
                sqlcmd.CommandText = "UPDATE TileTypes SET Title = @Title, "
                                    + "Picture = @Picture, "
                                    + "Color = @Color "
                                    + "WHERE ID = @ID";
                sqlcmd.Parameters.Add( AddDbParmSpecificValue( "@Title", titleTextBox.Text.Trim() ));
                sqlcmd.Parameters.Add( AddDbParmSpecificValue( "@Picture", typePictureBox.ImageLocation) );
                sqlcmd.Parameters.Add( AddDbParmSpecificValue( "@Color", colorButton.BackColor.ToArgb()) );
                sqlcmd.Parameters.Add( AddDbParmSpecificValue(  "@ID", id));
    
            if( SqlExecNonQuery(sqlcmd))
                // Good to go
                DoSomethingWithTheData;
            else
                // Notify of whatever error thrown....
    
            }
        }
    }
    

    So.. as you can see, my last class specifically is derived from EITHER Access OR SQL. Then, I can create my methods to get data, call updates, whatever. Get a SQL Command (which returns proper type and automatically is attached to its corresponding “Connection Handle” object, prepare the text, add your parameters, execute it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to update a record from an Ms-Access table with VB.NET and ASP.NET.
I am trying to update a record in Access through VB.NET. This is my
I'm trying to update the records in an Access database where the column Group
I am trying to update an existing record in a database using CoreData, but
I'm trying to update a single record in a table, but when I run
I am trying to get cakePHP to update a record in the database as
I am trying to update all records in a column so that they start
Im trying to update a record in my table however im receiving the following
I'm trying to update a record via the SalesForce API (Enterprise WSDL). The code
I am getting a comparison error message when trying to update a record using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.