Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
====The Error====
 Changes = '((System.Data.SQLite.SQLiteConnection)cnn).Changes' threw an exception of type 'System.InvalidOperationException'
 ====App.Config===

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name ="Default" connectionString="Data Source=.\DemoDB.db;Version=3;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

====My Code====
 public static List<DepartmentModel> LoadDepartment()
        {
            using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
            {
                var output = cnn.Query<DepartmentModel>("select * from Department", new DynamicParameters());
                return output.ToList();
            }
        }

        public static void SaveDepartment(DepartmentModel department)
        {
            using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
            {
                cnn.Execute("insert into Department (department_name,department_code, school_id) values (@DepartmentName,@DepartmentCode,@SchoolId)", department);
            }
        }

        private static string LoadConnectionString(string id = "Default")
        {
            return ConfigurationManager.ConnectionStrings[id].ConnectionString;
        }


What I have tried:

Single-stepping through,checked file path,
Posted
Updated 27-Aug-19 9:33am
Comments
Richard MacCutchan 27-Aug-19 3:25am    
You need to catch the exception and examine its contents to find out exactly what values/parameters/object reference is the cause of the problem. You also need to indicate which line it occurs on.

1 solution

You just need the path where file exists physically, here is an example

C#
private readonly SQLiteConnection _connection;
private Database()
{
	var databaseFile = GetDatabasePath();
	if (!File.Exists(databaseFile))
	{
		SQLiteConnection.CreateFile(databaseFile);
	}
	_connection = new SQLiteConnection($"Data Source={databaseFile};Version=3;");
}


we used Dapper for SQLite db communication.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900