c# database connection won't open when application auto startup

0

I was trying the alarm clock code on this site http://foxlearn.com/article/how-to-make-an-alarm-clock-with-sound-in-csharp-249.html

I used a SQLite database to store values of time so that when the alarm application is closed, then opened, it keeps that time value which triggers the alarm. That was preparatory work for making the application auto start up with Windows.

I always store the connection string in a class called SessionInfo like this

namespace AlarmClock.DataAccessLayer
{
    class SessionInfo
    {
        public static SQLiteConnection sqlconnection = new SQLiteConnection();
        public static string connectionstring = @"Data Source=AlarmClockDB.db;Version=3;";
    }
}

Then I open the connection in the program.cs like this - that is to make one connection to the entire project:

namespace AlarmClock
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            DataAccessLayer.SessionInfo.sqlconnection.ConnectionString DataAccessLayer.SessionInfo.connectionstring;         

            DataAccessLayer.SessionInfo.sqlconnection.Open();         
            Application.Run(new Form1());
        }
    }
}

In the alarm code block, I created the registry key as shown in that piece of code

namespace AlarmClock
{
    public partial class Form1: Form
    {
        System.Timers.Timer timer; 
        BusinessLayer.CLASS_ALARM alarm = new BusinessLayer.CLASS_ALARM();
        SoundPlayer player = new SoundPlayer();
        /*-->*/RegistryKey reg =Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

        public Form1()
        {
            /*-->*/reg.SetValue("AlarmClock", "\"" + Application.ExecutablePath.ToString() + "\"");
            InitializeComponent();           

            try
            {
                dateTimePicker1.Value = DateTime.Parse(alarm.AlarmGetDate());
            }
            catch (Exception)
            { }
        }

        //rest of code...
    }
}

Now the alarm works perfectly, and the registry key is created and all is good, but when I restart my computer the application just won't start up, it just throws an error:

Application stopped working

and it quickly disappears before I can even see it!

I checked the event log and it shows that message which says that the problem is in opening connection line in Program.cs:

DataAccessLayer.SessionInfo.sqlconnection.Open();

I don't know what the problem could be ... I have always used that method of connection in my application and never ever got an error with opening the connection.

The event log message with source title (.NET Runtime):

    Application: AlarmClock.exe
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.Data.SQLite.SQLiteException
    at System.Data.SQLite.SQLite3.Open(System.String, System.String, 
    System.Data.SQLite.SQLiteConnectionFlags, 
    System.Data.SQLite.SQLiteOpenFlagsEnum, Int32, Boolean)
    at System.Data.SQLite.SQLiteConnection.Open()
    at AlarmClock.Program.Main()

The second one with source title (Application Error)

    Faulting application name: AlarmClock.exe, version: 1.0.0.0, time stamp: 
    0x59bd4e44
    Faulting module name: KERNELBASE.dll, version: 6.3.9600.18666, time 
    stamp: 0x58f33794
    Exception code: 0xe0434352
    Fault offset: 0x00000000000095fc
    Faulting process id: 0xfd0
    Faulting application start time: 0x01d32f11151a66e5
    Faulting application path: C:\Users\dabbour\Documents\Visual Studio 
    2012\Projects\AlarmClock\AlarmClock\bin\Debug\AlarmClock.exe
    Faulting module path: C:\Windows\system32\KERNELBASE.dll
    Report Id: 53db87a5-9b04-11e7-82b4-8c89a5fd578a
    Faulting package full name: 
    Faulting package-relative application ID: 

I tried my best to explain the situation, please tell me if any more info is needed.

c#
sqlite
asked on Stack Overflow Sep 16, 2017 by Dabbour • edited Sep 16, 2017 by marc_s

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0