Starting SQL Server Browser

A number of my books contain database examples in them—most of which rely on SQL Server. One of the issues I’ve encountered over the years is that a reader will write in to say that the example doesn’t work as written. In most cases, the procedure and the example source code is just fine. In addition, there is nothing wrong with the user’s system, the Visual Studio installation, or the version of SQL Server used. The problem lies in the fact that the SQL Server Browser service isn’t started. Visual Studio relies heavily on this particular service and it isn’t started by default, in most cases, when you install SQL Server or SQL Server Express.

Unfortunately, the error message you receive will say nothing at all about SQL Server Browser. In fact, the message will complain about something completely unrelated and it may not even point to the database as the problem. This particular error has caused me (and many others) a lot of lost time. In fact, it’s such a nuisance that I immediately check to ensure the service is actually running before I look for other sources of problems (unless there is another obvious problem source). When you encounter an odd problem, especially an odd problem with source code that has worked in the past, and the application relies on a database, consider checking the SQL Server Browser service to ensure its running.

There are commonly two places in which you can look for the SQL Server Browser service. Because the SQL Server tools don’t install with the copy of SQL Server Express that comes with Visual Studio, I’ll discuss the place that everyone can look first, which is the Services console found in the Administrative Tools folder of the Control Panel. Simply open the console up, locate the SQL Server Browser entry, and ensure that it’s running as shown here.

SQLServerBrowser01

Notice that the service is set to start automatically each time the machine is booted. Using this setting usually ensures that the service will be available when needed. However, don’t assume that this setting means the service is always going to be available. If the service encounters a problem during the boot process, there is every chance that it won’t be available when you need it. Always verify that the service is indeed running when you encounter problems.

You can start a service by clicking the Start link on the left side of the right pane. The figure shows the Stop, Pause, and Restart options because the services is already started, but the Start option will appear when the service is stopped. If the service isn’t set up to start automatically, right click the service’s entry in the list and choose Properties from the context menu. Set the Startup Type field value to Automatic and click OK to configure the service to start automatically.

The second place you can look for this setting (when you have the SQL Server tools installed) is the SQL Server Configuration Manager. You’ll find the SQL Server Browser entry in the SQL Server Services folder as shown here.

SQLServerBrowser02

Again, make sure the service is actually running and that you have it configured to start automatically. Otherwise, you’ll find that you spend a lot of time chasing ghost problems that don’t actually exist. Of course, I’m always here to help you with any book-related problems you might have. Feel free to contact me about any issues with my books at John@JohnMuellerBooks.com.

 

Watching a Directory for Changes

A lot of my books have examples that require reliable access to one or more files on the hard drive. For example, the example showing how to load an XML file from disk on page 472 of C# Design and Development requires that the file is present—you can’t load a file that doesn’t exist. In fact, the need to know that a file is present is an overwhelming requirement in most of my books. For the most part, I can rely on the File.Exists() method to ensure that the file is there. If it isn’t, then I provide code to:

  • Rebuild the file
  • Use a backup file
  • Ask the user to supply the file
  • Create a new file from scratch.


I’m sure there must be other techniques in some of my books to handle the missing file, but the point is that when a file is missing, you have to deal with it in some way or the application will crash. A reader recently asked about an alternative to dealing with the missing filepreventing the user from deleting it in the first place (or at least logging the deletion). Sure, the user could attempt to delete the file, but a Windows service would sit in the background and place the file in a safe location for later retrieval. The suggestion has merit. It avoids the whole process of figuring out what to do now that the user has made life difficult.

I’ve written about working with Windows services in the past. In fact, it’s relatively easy to create a Windows service using Visual Studio. You can read about the process in my article entitled, “Writing a Managed Windows Service with C#.” However, that article doesn’t discuss how to monitor a directory so that you can detect a deletion and log the deletion as a minimum. So, that’s what I’ll discuss in the remainder of this post.

The FileSystemWatcher class provides the means to monitor file system changes and react to them. The concept is relatively simple. You create code that monitors the directories used by your application for changes. When a change occurs, you do something about it. I’ve created a very simple example to demonstrate the basics of what you need to do. Before you do anything else, add a reference to System.IO to your application.

Now that you have the required reference, let’s create a FileSystemWatcher. The first task is to initialize the object. I’ve created a simple button named btnStart to perform this task. The following code is a good starting place.

private void btnStart_Click(object sender, EventArgs e)
{
   if (btnStart.Text == "&Start")
   {
      // Keep an eye on C:\Temp.
      FSW = new FileSystemWatcher(@"C:\Temp");
 
      // Change the button text.
      btnStart.Text = "&Stop";
   }
   else
   {
      // Delete the existing FileSystemWatcher.
      FSW = null;
 
      // Change the button.
      btnStart.Text = "&Start";
   }
}

Unfortunately, this code won’t do much. Sure, it creates or destroys the FileSystemWatcher as needed, but it doesn’t really handle anything. To perform this next step, you need to create an event handler in the first part of the if statement. Since this reader is interested in deletions, you create a Deleted event handler by typing FSW.Deleted +=. When you type the =, the IDE automatically starts helping you create the required code. Press Tab to create the required FileSystemEventHandler object and then press Tab again to create the event handler, which is FSW_Deleted() in this case. Just to make things easy, the example displays a message box whenever the user deletes anything from C:\Temp. To make things symmetrical, you also have to remove the event handler in the second part of the if statement. The full code for this example then is:

// Create the FileSystemWatcher.
FileSystemWatcher FSW;
 
private void btnStart_Click(object sender, EventArgs e)
{
   if (btnStart.Text == "&Start")
   {
      // Keep an eye on C:\Temp.
      FSW = new FileSystemWatcher(@"C:\Temp");
 
      // Make sure that the FileSystemWatcher has events enabled.
      FSW.EnableRaisingEvents = true;
 
      // Change the button text.
      btnStart.Text = "&Stop";
 
      // Create a deleted event handler.
      FSW.Deleted += new FileSystemEventHandler(FSW_Deleted);
   }
   else
   {
      // Remove the event handler.
      FSW.Deleted -= FSW_Deleted;
 
      // Delete the existing FileSystemWatcher.
      FSW = null;
 
      // Change the button.
      btnStart.Text = "&Start";
   }
}
 
void FSW_Deleted(object sender, FileSystemEventArgs e)
{
   // Display a message box.
   MessageBox.Show("The user deleted: " + e.Name);
}

 

When you click Start, the application will monitor C:\Temp for any deleted files. Make sure that you set EnableRaisingEvents to true or the FileSystemWatcher won’t raise any events. When a deletion occurs, you see a message box telling you which file has been deleted. This solution is incomplete at the moment, but I plan to revisit it in the future. In the meantime, you have a tool now for knowing when a deletion has occurred that will affect the stability of your application so you can do something about it before the application crashes. Let me know if you have any questions at John@JohnMuellerBooks.com.