Goran Bibic

Goran Bibic

  • 475
  • 2.9k
  • 201k

Select conection string from label value c#

Sep 5 2018 4:36 AM
I have two conections added in app
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.     <configSections>  
  4.     </configSections>  
  5.     <connectionStrings>  
  6.         <add name="test_2017_2018.Properties.Settings.TEST_2017" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=2017;Integrated Security=True"  
  7.             providerName="System.Data.SqlClient" />  
  8.         <add name="test_2017_2018.Properties.Settings.TEST_2018" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=2018;Integrated Security=True"  
  9.             providerName="System.Data.SqlClient" />  
  10.     </connectionStrings>  
  11.     <startup>   
  12.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />  
  13.     </startup>  
  14. </configuration> 
 Define conections in c# app winform
 
  1. string cs2017 = Properties.Settings.Default.TEST_2017;  
  2. string cs2018 = Properties.Settings.Default.TEST_2018; 
 

When login choose in combobox value (2017 or 2018)

Use that

  1. InitializeComponent();  
  2. godinalabel.Text = getGodina(); 
 That is ok...now i need
  1. public void PullData()  
  2.        {  
  3.   
  4.            if (godinalabel.Text == "2017")  
  5.            {  
  6.                SqlConnection connString = new SqlConnection(cs2017);  
  7.            }  
  8.            else if (godinalabel.Text == "2018")  
  9.            {  
  10.                SqlConnection connString = new SqlConnection(cs2018);  
  11.            }  
  12.   
  13.        } 
 To fill datagrid
 
  1. private void Izvjestajoprodanimartiklima()  
  2.         {  
  3.   
  4.             using (SqlConnection openCon = new SqlConnection(PullData))  
  5.   
  6.             {  
  7.   
  8.                 String saveStaff = "SELECT  * from partners  ";  
  9.   
  10.   
  11.   
  12.                 Console.WriteLine(saveStaff);  
  13.   
  14.   
  15.                 openCon.Open();  
  16.                 using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(saveStaff, cs))  
  17.   
  18.                 {  
  19.                     DataTable dt = new DataTable();  
  20.                     DataColumn column = new DataColumn();  
  21.                     column.DataType = System.Type.GetType("System.Int32");  
  22.                     column.ColumnName = "Redni broj";  
  23.                     column.AutoIncrement = true;  
  24.                     column.AutoIncrementSeed = 1;  
  25.                     column.AutoIncrementStep = 1;  
  26.                     dt.Columns.Add(column);  
  27.                     int index = -1;  
  28.                     foreach (DataRow row in dt.Rows)  
  29.                     {  
  30.                         row.SetField(column, ++index);  
  31.                     }  
  32.   
  33.   
  34.                     querySaveStaff.Fill(dt);  
  35.                     dataGridView1.DataSource = dt;  
  36.                     dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;  
  37.                     dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;  
  38.   
  39.   
  40.                     dataGridView1.Update();  
  41.                     dataGridView1.Refresh();  
  42.                 }  
  43.   
  44.             }  
  45.   
  46.   
  47.   
  48.         } 
 Error is this line Pulldata when fill datagrid
 
  1. using (SqlConnection openCon = new SqlConnection(PullData)) 
 
Some help_
 
 

Answers (5)