Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Need for my app
Have database  2017,2018,2019...I add IN every form connection string



string BSS_2017 = Properties.Settings.Default.MG_SISTEM_2017CS;     
string BSS_2018 = Properties.Settings.Default.MG_SISTEM_2018CS;  
string BSS_2019 = Properties.Settings.Default.MG_SISTEM_2019CS; 
string con;  


Then in label chek value (2017,2018 or2019)


public popis()  
        {  
            InitializeComponent();  
            yearlabel.Text = getYear();  
        }  


And connect


private String getGodina()  
       {  
  
           string value = login.godina.ToString();  
  
           return value;  
       }  
  
  
       public void PullData()  
       {  
           if (yearlabel.Text == "2017")  
           {  
               con = BSS_2019;  
           }  
           else if (yearlabel.Text == "2018")  
           {  
               con = BSS_2018;  
           }  
  
           else if (yearlabel.Text == "2019")  
           {  
              con = BSS_2019;  
           }  
  
       }  


What I have tried:

That work fine...but...I must put this in every form in my app....need help
To create some class with this value.
Help?
Posted
Updated 24-Jul-19 23:44pm

Why are you using all these hard coded values, and even worse, text labels, on your form?

Create a class that can list the names of all the databases that exist (use Directory.GetFiles Method (System.IO) | Microsoft Docs[^]). You can then present those names in a listbox which allows the user to select the one they are interested in. All you then need to do is to format that name into your connection string template.
 
Share this answer
 
Comments
Goran Bibic 25-Jul-19 5:09am    
Now I want to do that
Goran Bibic 25-Jul-19 5:10am    
Why files? Need conn string to db?
Richard MacCutchan 25-Jul-19 5:13am    
Each of your databases has a filename (I assume). So getting the list of names from wherever they are stored means that you do not need to hard code the names in your application, or update it every year.
A public static method (e.g. GetConnectionString) of your application Program class could be appropriate.
 
Share this answer
 
I create class

class ConnectionClass
   {

      // static string BSS_2017 = Properties.Settings.Default.MG_SISTEM_2017CS;
      // static string BSS_2018 = Properties.Settings.Default.MG_SISTEM_2018CS;
       static string BSS_2019 = Properties.Settings.Default.MG_SISTEM_2019CS;
       static string con;

       public static string PullData(string numYear)
       {
           switch (numYear)
           {
              // case "2017":
              //     con = BSS_2017;
               //    break;
              // case "2018":
               //    con = BSS_2018;
               //    break;
               case "2019":
                   con = BSS_2019;
                   break;
           }
           return con;
       }

   }



How to implement to forms?

I put in from

string conString = ConnectionClass.PullData(yearlabel.Text);  


Butt error is
connection string proprerty has not intialized.  



private void Ulazni_racun_roba_lista_fill()  
        {  
              
//your string  
            string conString = ConnectionClass.PullData(godinalabel.Text);  
  
            using (SqlConnection openCon = new SqlConnection(conString))  
  
            {  
  
 String saveStaff = "SELECT * from dbo.popis_lista";  
  
  
                Console.WriteLine(saveStaff);  
  
//Here erorr  connection string proprerty has not intialized.  
                    openCon.Open();  
  
                    using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(saveStaff, conString))  
  
                    {  
  
                        DataTable dt = new DataTable();  
  
  
                        querySaveStaff.Fill(dt);  
                        PopisListaDataGridView.DataSource = dt;  
                    PopisListaDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;  
  
 PopisListaDataGridView.Update();  
                    PopisListaDataGridView.Refresh();  
  
}  
  
                }  
  
  
  
            }  
 
Share this answer
 
Comments
Richard MacCutchan 25-Jul-19 6:25am    
You are still using hard coded values which is really the wrong way to go about this. All you need to do is to pass the year number to a static class method which will use a template and that value to format a connection string. You can then use that in any part of your application to get the correct connection string at any time or place.

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