Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day,

I have a C# application that starts with windows. The moment the application starts it immediately does checks on the database for any data. When the application is started normally everything works, but when I restart and allow it to start up with windows, it gives me "Program has stopped working" error. I Checked the even logs and it seems like the application is trying to connect to the database before SQL Server is started. How can this issue be overcome?

Like I said. After it gives me the error, I can simply double click the icon on the desktop and it starts perfectly. So the issue has to be with SQL Server not running properly when the application tries to start with windows.

What I have tried:

I have no idea what to try unfortunately. I have googled a bit, but seems like I am the only one with this problem.
Posted
Updated 20-Sep-16 19:44pm
Comments
Kevin Marois 20-Sep-16 17:38pm    
1) Do you have your own logging in place to see where's it failing?

2) if you think that the DB isn't ready, can you put in some pausing/testing to ensure that it is?

3) What DB are you using?
Dan Sporici 20-Sep-16 17:42pm    
Posting this as a comment because I'm not sure if I get it right...but why wouldn't you wrap the connection part in a try-catch block and then in a while loop. And just use a variable to signal if the connection succeeded (just add it below the connection method's call, if it fails the value isn't changed => retry); and also a sleep for a few seconds between failed connections.
Christopher Smit 20-Sep-16 17:58pm    
@Apex95, that is not a bad idea. I will test it. I think you can maybe post it as an answer, if it works I will accept it.

@Kevin, I am using a SQL database. I will try the method provided by Apex. This should be good enough for the pause/testing that you are talking about?

Use this to check the SQL Server is Running or not [^]
use Timer[^] to check the connection for every 30 seconds or 1 minute till it gets connected, once the connection is established then stop the timer in the elapsed event.
 
Share this answer
 
Ok, since you agreed with this, I'll extend the comment with some code here. This is how I imagined the stuff there.

C#
static void connectToDB()
{
    // considering that the connection always throws an Exception
    throw new Exception("DB is down");
}

static void Main(string[] args)
{
    bool connected = false;

    while (!connected)
    {
        try
        {
            connectToDB();
            connected = true;
        }
        catch (Exception ex)
        {
            System.Threading.Thread.Sleep(1000);
        }
    }
}


See if it helps; if not, we might need some more information.
 
Share this answer
 
Comments
Christopher Smit 21-Sep-16 13:41pm    
This is causing it not to start up, but not giving any errors. The splash screen is just loading forever. So I added an error reporting message and the error I am getting is "an attempt to attach an auto-named database for file xxxx failed.A database with the same name exists, or cannot be opened or it is located on a UNC share.

This has to be because SQL is not running yet. As far as I can tell. Because as I said, the moment I try running it while the pc is already started, it works
Christopher Smit 22-Sep-16 14:19pm    
For anyone else having the problem. What solved it for me was this. Seems like when I publish as ClickOnce, it does not work. But when I created my own installer with NSIS it worked successfully without having to change any code. Can't tell you what it is, but problem is definitely ClickOnce.

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