Click here to Skip to main content
13,902,997 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 14 Jul 2014
Licenced CPOL

ConnectionString for Winform and Reportsource from a txt File

, 14 Jul 2014
Rate this:
Please Sign up or sign in to vote.
To set the Connectionstring and the other configuration variables in a file and access it dynamically


One of the main issues faced by the winform developers at the time of deployment is the issue of changing the connectionstring and the locations pointer variables which are already used inside the application. This tutorial aims at giving an alternative way, i.e., keeping all those types of connectionstring and reportsources, etc. from a text file.


I am developing an application where I use my production system as the SQL server and set all the required locations within the system. Now I need to deploy it to multiple clients who may be using a distributed Server or Network folders for storing images and files. So I decided to make the application read from the text file located in the C drive the parameters and connectionstrings required.

Using the Code

I created public static variables in the Program.CS for all the required parameters and created a class databasePicker for setting the values to these variables from textfile.

So my Program.cs will look like this:

static class Program
    {   public static String ConnStr = "";
        public static String OurReportSource = "";
        public static String OurLogSource = "";
        public static String OurImagelocation = "";
        public static String database = "";
        public static String  Server = "";
        public static String dbUsername = "";
        public static String dbPassword = ""; 
 public static int USERPK;
        public static String UserType;
        public static int LOCTNPK;
        public static String UserName;
        public static String LOCATIONCODE;
        public static String EmpName;
        public static DateTime Datetoday=DateTime.Now ;
 public static Transactions.DatabasePicker databasepcker = null;
        public static int usernampk;
/// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
            databasepcker = new Transactions.DatabasePicker();
           Application.Run(new MainForm());        

And in my DatabasePicker class, I had assumed that the textfile with name DBKey will be available on the C drive of client system which have all the required information for the application including connectionstring,reportsource, logfile source, image location, filestorage location, etc.

And if the DBKey does not exist, a filedialog box will be displayed to user and ask him to copy the DB key from any of the network folders or location (USB, CD, etc.).

class DatabasePicker
        private const string FILE_NAME = "C:\\DBkey.txt";

        public void SetConnctionString()
            if (!File.Exists(FILE_NAME))
                DialogResult dialogResult = MessageBox.Show(
                    "Database configuration file not found  Do you want to Reset it ? ", 
                    "Database Key not Found", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                    OpenFileDialog openFileDialog1 = new OpenFileDialog();

                    openFileDialog1.InitialDirectory = "c:\\";
                    openFileDialog1.Filter = "Text|*.txt|All|*.*";
                    openFileDialog1.RestoreDirectory = true;

                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                            String Sourcefile = openFileDialog1.FileName;
                            File.Copy(Sourcefile, "c:\\DBkey.txt");
                            MessageBox.Show("Data Key Recieved", "Data Key");
                        catch (Exception ex)
                            MessageBox.Show("Error: Could not read file from disk. 
                            Original error: " + ex.Message, "Data Key");
                else if (dialogResult == DialogResult.No)

                string line;

                // Read the file and display it line by line.
                System.IO.StreamReader file =
                   new System.IO.StreamReader("c:\\DBkey.txt");

                string[] lines = File.ReadAllLines("c:\\DBkey.txt"); //File is in System.IO
                // for Dubai
                string firstLine = lines[1]; //constring
                string secondline = lines[2]; //reportlocation
                string ThirdLine = lines[3]; //log
                string FourthLine = lines[4]; //images
                string fifthline = lines[5]; //servername
                string sixthline = lines[6]; //databasename
                string seventhline = lines[7]; //userid
                string eightline = lines[8];//password               

                           Program.ConnStr = firstLine;
                Program.OurReportSource = secondline;
                Program.OurLogSource = ThirdLine;
                Program.OurImagelocation = FourthLine;
                Program.Server = fifthline;
                Program.database = sixthline;
                Program.dbUsername = seventhline;
                Program.dbPassword = eightline;

Now the Connectionstring can be called from anywhere using the static variable program ConnStr.

Points of Interest

This method will help in clearing the difficulty caused when the database server or the application server changes.


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


About the Author

Software Developer
United Arab Emirates United Arab Emirates
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 14 Jul 2014
Article Copyright 2014 by SREENATH GANGA
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid