A community member has associated this post with a similar question:
Error when checking if a date in Access Database table

Only moderators can edit this content.

Error when deleting records from Access database.

VAer 756 Reputation points
2021-01-16T20:03:09.477+00:00

Backend database: Access file. The connection string is fine, it works fine with other codes (such as inserting record), but it comes with error when dealing with date format.

Access date format: yyyy-mm-dd. I tried to set yyyy-MM-dd in Access field property, but Access converts to yyyy-mm-dd automatically. However, it shows correctly in Access file. But it should have nothing to do with it.

Attached screenshot: same code compiled in machine with Visual Studio Community Edition

It shows Data Type mismatch when running on another machine (same code compiled with Visual Studio Enterprise Edition)

Thanks.

57208-unhandled-exception-error.jpg

                using (OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString))    
                {  
 //Delete records from Access database  
  
                    string sqlDelete = "DELETE FROM TableABC WHERE Username = '" + Environment.UserName + "' AND ABCDate BETWEEN '" + dateTimePickerBegin.Value.ToString("yyyy-MM-dd") + "' AND '" + dateTimePickerEnd.Value.ToString("yyyy-MM-dd") + "'";  
  
                    MessageBox.Show(sqlDelete);  
  
                    OdbcCommand cmdDelete = new OdbcCommand(sqlDelete, Cn);  
                    Cn.Open();  
  
                    cmdDelete.ExecuteNonQuery();  
  
  
                }  



System.Runtime.InteropServices.SEHException  
  HResult=0x80004005  
  Message=External component has thrown an exception.  
  Source=System.Data  
  StackTrace:  
   at System.Data.Common.UnsafeNativeMethods.SQLDriverConnectW(OdbcConnectionHandle hdbc, IntPtr hwnd, String connectionstring, Int16 cbConnectionstring, IntPtr connectionstringout, Int16 cbConnectionstringoutMax, Int16& cbConnectionstringout, Int16 fDriverCompletion)  
   at System.Data.Odbc.OdbcConnectionHandle.Connect(String connectionString)  
   at System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr, OdbcEnvironmentHandle environmentHandle)  
   at System.Data.Odbc.OdbcConnectionOpen..ctor(OdbcConnection outerConnection, OdbcConnectionString connectionOptions)  
   at System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)  
   at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)  
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)  
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)  
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)  
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)  
   at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)  
   at System.Data.Odbc.OdbcConnection.Open()  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,827 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,235 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
938 questions
{count} votes