Select connection string from label value c#

  • 283 Views
  • Last Post 05 September 2018
  • Topic Is Solved
Stylus STYLUS posted this 05 September 2018

I have two connections added in app

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="test_2017_2018.Properties.Settings.TEST_2017" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=2017;Integrated Security=True"
            providerName="System.Data.SqlClient" />
        <add name="test_2017_2018.Properties.Settings.TEST_2018" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=2018;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

Define connections in c# app winform

string cs2017 = Properties.Settings.Default.TEST_2017;
string cs2018 = Properties.Settings.Default.TEST_2018;

When login choose in combobox value (SQL 2017 or SQL 2018)

Use that

InitializeComponent();
godinalabel.Text = getGodina();

That is ok...now i need

 public void PullData()
        {            
            if (godinalabel.Text == "2017")
            {
                SqlConnection connString = new SqlConnection(cs2017);
            }
            else if (godinalabel.Text == "2018")
            {
                SqlConnection connString = new SqlConnection(cs2018);
            }
        }

To fill datagrid

private void Izvjestajoprodanimartiklima()
        {            
            using (SqlConnection openCon = new SqlConnection(PullData))
            {
                String saveStaff = "SELECT  * from partners";
                Console.WriteLine(saveStaff);
                openCon.Open();
                using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(saveStaff, cs))
                {
                    DataTable dt = new DataTable();
                    DataColumn column = new DataColumn();
                    column.DataType = System.Type.GetType("System.Int32");
                    column.ColumnName = "Redni broj";
                    column.AutoIncrement = true;
                    column.AutoIncrementSeed = 1;
                    column.AutoIncrementStep = 1;
                    dt.Columns.Add(column);
                    int index = -1;
                    foreach (DataRow row in dt.Rows)
                    {
                        row.SetField(column, ++index);
                    }
                    querySaveStaff.Fill(dt);
                    dataGridView1.DataSource = dt;
                    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;                 
                    dataGridView1.Update();
                    dataGridView1.Refresh();
                }
            }
        }

Error is this line Pulldata when fill datagrid

using (SqlConnection openCon = new SqlConnection(PullData))

Some help?

Order By: Standard | Newest | Votes
admin posted this 05 September 2018

You can't add a void datatype to SqlConnection

You should create a properties to get your connection string as the following

public string ConnectionString
{
    get{ 
    if (godinalabel.Text == "2017")
        return cs2017;
    return cs2018;
    }
}

then change the SqlConnection in Izvjestajoprodanimartiklima method as below

using (SqlConnection openCon = new SqlConnection(ConnectionString))
{
   //...
}
Stylus STYLUS posted this 05 September 2018

Thank you

Close