9 Replies - 325812 Views - Last Post: 30 June 2010 - 01:11 PM Rate Topic: -----

#1 Footsie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 24
  • View blog
  • Posts: 370
  • Joined: 20-September 07

Grabbing connectionString from App.config

Posted 06 March 2008 - 04:30 AM

I'm trying to grab a connection string out of my App.config file. I'm more used to doing this in ASP.net so am fairly new to this in Windows forms.

Here is my App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <connectionStrings>
	<add name="DigiBrd"
		 connectionString="server=localhost;user id=****;Password=****;database=DigiBrd"
	providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  
</configuration>
 


Everything that I've read seems to indicate that this should work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace DigiBrd
{
    public partial class Form1 : Form
    {
      public Form1()
        {
            InitializeComponent();
            string cnString = ConfigurationManager.ConnectionStrings["DigiBrd"].ConnectionString;

            MySqlConnection myConn = new MySqlConnection(cnString);

            DataSet dsTracking = new DataSet();
            string selectStr = "SELECT * FROM tracking";
           
            MySqlDataAdapter adapter = new MySqlDataAdapter(selectStr, myConn);

            adapter.Fill(dsTracking, "Tracking");

            grdView1.DataSource = dsTracking;
            grdView1.DataMember = "Tracking";
            
        }  

    }
}


But it says that the name 'ConfigurationManager does not exist in the current context'. Intellisense doesn't pick it up either.
I thought that adding the using System.Configuration; would work but it doesn't.

What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Grabbing connectionString from App.config

#2 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Grabbing connectionString from App.config

Posted 06 March 2008 - 06:17 AM

your App.config file should have

<add key="YOUR_KEY_STRING" value="your SQL connection string"/>



your c# code
add this reference to your project C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll
System.Configuration.ConfigurationManager.AppSettings["YOUR_KEY_STRING"];


Was This Post Helpful? 1

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Grabbing connectionString from App.config

Posted 06 March 2008 - 06:51 AM

To make things easier on you, you could create a function to rturn that, then just call the function whenever you need the connection string, like this


//This returns the connection string
public static string GetConnectionString(string connName)
{
	string strReturn = string.Empty;
	if (!(string.IsNullOrEmpty(connName)))
	{
		strReturn = ConfigurationManager.ConnectionStrings[connName].ConnectionString;
	}
	else
	{
		strReturn = ConfigurationManager.ConnectionStrings["YourConnectionName"].ConnectionString;
	}
	return strReturn;
}



You're ging to need to add 2 Namespaces to your class


using System.Configuration;
using System.Configuration.ConfigurationSettings



You're also more than likely going to need to add a reference to the System.Configuration Namespace, go Project > Add Reference. Then in the .Net scroll down and add System.Configuration. Then when you need your connection string (lets say this is in its own class) you would use something like


SqlConnection conn = new SqlConnection(MyClass.GetConnectionString("YourConnectionName").ToString()));




Hope that helps :)

This post has been edited by PsychoCoder: 06 March 2008 - 06:52 AM

Was This Post Helpful? 2

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6979
  • View blog
  • Posts: 14,600
  • Joined: 16-October 07

Re: Grabbing connectionString from App.config

Posted 06 March 2008 - 06:55 AM

View Postzakary, on 6 Mar, 2008 - 08:17 AM, said:

your App.config file should have

<add key="YOUR_KEY_STRING" value="your SQL connection string"/>



your c# code
add this reference to your project C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll
System.Configuration.ConfigurationManager.AppSettings["YOUR_KEY_STRING"];



This would be correct prior to .NET 2.0, but doesn't seem to address this question.

From what's been given you have a WinForms C# .NET 2.0 project. The app.config file does indeed have a connectionStrings section. Here's what mine looks like after doing a DataSet drag and drop.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Baavgai.Demo.Data.Properties.Settings.AwSnapConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\AwSnap.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>



When this file is changed or generated by the system, the Settings object is updated and code is written. Try looking under Properties -> Settings.settings to see what's going it. Under that is a Settings.Designer.cs file that has some generated code.

That said, I doesn't look like your name is fully qualified in the project. At the very least, it should read something like Properties.Settings.DigiBrd.

Do this: go the project's properties and choose settings. Enter the connection name you want and choose type connection string at the bottom of the list. You'll get to walk through a conection wizard. Once done, your app.config should have a new entry. Even better, you should be able to access it in your application by choosing something like DigiBrd.Properties.Settings.Default.<Name>.

Hope this helps.
Was This Post Helpful? 1

#11 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Grabbing connectionString from App.config

Posted 06 March 2008 - 07:01 AM

you calling my an old timer baavgai LOL

This post has been edited by zakary: 06 March 2008 - 07:01 AM

Was This Post Helpful? 0
  • +
  • -

#12 Footsie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 24
  • View blog
  • Posts: 370
  • Joined: 20-September 07

Re: Grabbing connectionString from App.config

Posted 06 March 2008 - 07:52 AM

Cool. But now I'm not able to bind the dataset to the datagrid.

Zakary and Psycho - I was missing those references, no wonder the Intellisense wasn't working. And yes I will use that connection class, Psycho it does make things a bit easier.

baavgai - this is what the connectionString looks like now after doing what you said - looks like it wasn't the full name after all:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
  </configSections>
  <connectionStrings>
    <add name="DigiBrd.Properties.Settings.Setting" connectionString="server=localhost;user id=***;database=DigiBrd;password=***;persist security info=True"
      providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
</configuration>



This is my updated code. DataAccess is the class I'm using to get the connection string (as per Psycho's suggestion).
    public Form1()
        {
            InitializeComponent();

            MySqlConnection myConn = new MySqlConnection(DataAccess.GetConnectionString("DigiBrd.Properties.Settings.Setting"));

            DataSet dsTracking = new DataSet();
            string selectStr = "SELECT * FROM tracking";
           
            MySqlDataAdapter adapter = new MySqlDataAdapter(selectStr, myConn);
            try
            {
                adapter.Fill(dsTracking, "Tracking");
            }
            catch
            {
            }
            finally 
            {
            }
            grdView1.DataSource = dsTracking.Tables[0];
            //usually in asp.net I would put a DataBind() here but WinForms doesn't seem to have it??

           }  


Right now my screen is still blank when I test.
How can I bind my grid to the dataset?

EDIT: and do I always have to refer to my connection string with the name DigiBrd.Properties.Settings.Setting it seems a bit unwieldy.

This post has been edited by Footsie: 06 March 2008 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6979
  • View blog
  • Posts: 14,600
  • Joined: 16-October 07

Re: Grabbing connectionString from App.config

Posted 06 March 2008 - 09:33 AM

View PostFootsie, on 6 Mar, 2008 - 09:52 AM, said:

EDIT: and do I always have to refer to my connection string with the name DigiBrd.Properties.Settings.Setting it seems a bit unwieldy.


As PsychoCoder mentioned, your best bet is to have some class with a static method called GetConnection and just always call that. However, that is a horrid name. Go into settings and make it something nicer. ;)

If you're working with datagrids, consider using typed datasets. Here's the wizard way:

Create a DataSet item in your project. Open it up in designer. Right click in the empty space and you'll see a dropdown of known connections, the first being those already defined in your project. Walk through the wizard. Alternately, find the table you want in the Server Explorer and drag it into the dataset.

Now you have a typed dataset. More than that, you have a dataadapter already written for you. Open you form. Depending on what you drag onto your form, a lot of code can get written for you. Play with it.

What I usually do is drag the typed dataset into the form, without all the extra connectors and adapters. The bind the objects to the form to that dataset. In the form code, call the data adapters to load data into the data set that belongs to the form. If you're doing something simple, you really don't need to write any code if you don't want to. If you like to write code, the automatic stuff will point you toward what you need to do manually.
Was This Post Helpful? 0
  • +
  • -

#14 manish.2004  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 17-October 08

Re: Grabbing connectionString from App.config

Posted 17 October 2008 - 11:42 PM

View PostFootsie, on 6 Mar, 2008 - 04:30 AM, said:

I'm trying to grab a connection string out of my App.config file. I'm more used to doing this in ASP.net so am fairly new to this in Windows forms.

Here is my App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <connectionStrings>
	<add name="DigiBrd"
		 connectionString="server=localhost;user id=****;Password=****;database=DigiBrd"
	providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  
</configuration>
 


Everything that I've read seems to indicate that this should work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace DigiBrd
{
    public partial class Form1 : Form
    {
      public Form1()
        {
            InitializeComponent();
            string cnString = ConfigurationManager.ConnectionStrings["DigiBrd"].ConnectionString;

            MySqlConnection myConn = new MySqlConnection(cnString);

            DataSet dsTracking = new DataSet();
            string selectStr = "SELECT * FROM tracking";
           
            MySqlDataAdapter adapter = new MySqlDataAdapter(selectStr, myConn);

            adapter.Fill(dsTracking, "Tracking");

            grdView1.DataSource = dsTracking;
            grdView1.DataMember = "Tracking";
            
        }  

    }
}


But it says that the name 'ConfigurationManager does not exist in the current context'. Intellisense doesn't pick it up either.
I thought that adding the using System.Configuration; would work but it doesn't.

What am I doing wrong?



View PostFootsie, on 6 Mar, 2008 - 04:30 AM, said:

I'm trying to grab a connection string out of my App.config file. I'm more used to doing this in ASP.net so am fairly new to this in Windows forms.

Here is my App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <connectionStrings>
	<add name="DigiBrd"
		 connectionString="server=localhost;user id=****;Password=****;database=DigiBrd"
	providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  
</configuration>
 


Everything that I've read seems to indicate that this should work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace DigiBrd
{
    public partial class Form1 : Form
    {
      public Form1()
        {
            InitializeComponent();
            string cnString = ConfigurationManager.ConnectionStrings["DigiBrd"].ConnectionString;

            MySqlConnection myConn = new MySqlConnection(cnString);

            DataSet dsTracking = new DataSet();
            string selectStr = "SELECT * FROM tracking";
           
            MySqlDataAdapter adapter = new MySqlDataAdapter(selectStr, myConn);

            adapter.Fill(dsTracking, "Tracking");

            grdView1.DataSource = dsTracking;
            grdView1.DataMember = "Tracking";
            
        }  

    }
}


But it says that the name 'ConfigurationManager does not exist in the current context'. Intellisense doesn't pick it up either.
I thought that adding the using System.Configuration; would work but it doesn't.

What am I doing wrong?


Please Add the reference of System.Configuration.dll
Was This Post Helpful? 1

#15 davefeag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-March 09

Re: Grabbing connectionString from App.config

Posted 30 November 2009 - 08:35 AM

Thank You psycho, with very little mod it worked perfect. 1 & 1/2 day headache done.

This post has been edited by davefeag: 30 November 2009 - 08:36 AM

Was This Post Helpful? 0
  • +
  • -

#16 Guest_LNg*


Reputation:

Re: Grabbing connectionString from App.config

Posted 30 June 2010 - 01:11 PM

View Postmanish.2004, on 17 October 2008 - 10:42 PM, said:

View PostFootsie, on 6 Mar, 2008 - 04:30 AM, said:

I'm trying to grab a connection string out of my App.config file. I'm more used to doing this in ASP.net so am fairly new to this in Windows forms.

Here is my App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <connectionStrings>
	<add name="DigiBrd"
		 connectionString="server=localhost;user id=****;Password=****;database=DigiBrd"
	providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  
</configuration>
 


Everything that I've read seems to indicate that this should work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace DigiBrd
{
    public partial class Form1 : Form
    {
      public Form1()
        {
            InitializeComponent();
            string cnString = ConfigurationManager.ConnectionStrings["DigiBrd"].ConnectionString;

            MySqlConnection myConn = new MySqlConnection(cnString);

            DataSet dsTracking = new DataSet();
            string selectStr = "SELECT * FROM tracking";
           
            MySqlDataAdapter adapter = new MySqlDataAdapter(selectStr, myConn);

            adapter.Fill(dsTracking, "Tracking");

            grdView1.DataSource = dsTracking;
            grdView1.DataMember = "Tracking";
            
        }  

    }
}


But it says that the name 'ConfigurationManager does not exist in the current context'. Intellisense doesn't pick it up either.
I thought that adding the using System.Configuration; would work but it doesn't.

What am I doing wrong?



View PostFootsie, on 6 Mar, 2008 - 04:30 AM, said:

I'm trying to grab a connection string out of my App.config file. I'm more used to doing this in ASP.net so am fairly new to this in Windows forms.

Here is my App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <connectionStrings>
	<add name="DigiBrd"
		 connectionString="server=localhost;user id=****;Password=****;database=DigiBrd"
	providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  
</configuration>
 


Everything that I've read seems to indicate that this should work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace DigiBrd
{
    public partial class Form1 : Form
    {
      public Form1()
        {
            InitializeComponent();
            string cnString = ConfigurationManager.ConnectionStrings["DigiBrd"].ConnectionString;

            MySqlConnection myConn = new MySqlConnection(cnString);

            DataSet dsTracking = new DataSet();
            string selectStr = "SELECT * FROM tracking";
           
            MySqlDataAdapter adapter = new MySqlDataAdapter(selectStr, myConn);

            adapter.Fill(dsTracking, "Tracking");

            grdView1.DataSource = dsTracking;
            grdView1.DataMember = "Tracking";
            
        }  

    }
}


But it says that the name 'ConfigurationManager does not exist in the current context'. Intellisense doesn't pick it up either.
I thought that adding the using System.Configuration; would work but it doesn't.

What am I doing wrong?


Please Add the reference of System.Configuration.dll


YES, I got the same problem, after adding reference of System.Configuration, it's solved! THANKS!!!
Was This Post Helpful? 0

Page 1 of 1