Henry Vuong

Henry Vuong

  • NA
  • 27
  • 0

Derived class cannot find external text file

Jan 17 2010 3:47 PM
I am using Microsoft Visual Studio 2005 to write my code. I created a MasterForm class like this:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace ProList
{
    public partial class MasterForm : Form
    {
        private SqlConnection conn;

        public MasterForm()
        {
            InitializeComponent();
        }

        //readonly property that return the connection object
        public SqlConnection Conn
        {
            get { return conn; }
        }

        private void MasterForm_Load(object sender, EventArgs e)
        {
            //Establish a connection with the database
            //An external text file is used to store the connection string
            //so that when the host computer or the database is changed
            //the information can be updated by changing the text in the text file
            
            string connStr = File.ReadAllText("DatabaseConnectionString.txt");

            try
            {
                conn = new SqlConnection(connStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
                            
        }
    }
}



where "DatabaseConnectionString.txt" is a text file that contains the connection string. I placed this file in the "Debug" folder of the program. The purpose of using an external file to store the connection string is when I need to switch the database after the program is deployed, I only need to edit the text file instead of the code. I also have the ChildForm class that inherits MasterForm.

The program compiles and runs without an error. But when I open the ChildForm design view in Visual Studio, the form visual elements do not appear. Instead there's a bunch of HTML codes and in the error window the following warning is generated:

"Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\DatabaseConnectionString.txt'    

This warning is not generated when I open the MasterForm design view, it only happens with its derived forms (classes).

Does anyone have any idea why this happens? Thanks.

Answers (5)