What's Here?
- Members: 340,125
- Replies: 920,439
- Topics: 154,931
- Snippets: 4,855
- Tutorials: 1,257
- Total Online: 4,053
- Members: 119
- Guests: 3,934
|
Welcome to Dream.In.Code |
|
|
Become an Expert!
Join 340,125 Programmers for FREE! Get instant access to thousands  of experts, tutorials, code snippets, and more! There are 4,053 people online right now. Registration is fast and FREE... Join Now!
Chat LIVE With a Expert
|
Grabbing connectionString from App.config
Grabbing connectionString from App.config
Rate Topic:
   
Posted 06 March 2008 - 03: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?
Posted 06 March 2008 - 05: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

- apt-get install DIC.bin
-
-
Group:
Admins
-
Posts:
16,211
-
Joined:
26-July 07
Dream Kudos: 12400
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery
Posted 06 March 2008 - 05: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 - 05:52 AM
Was This Post Helpful?
1

- Dreaming Coder
-
-
Group:
Expert w/DIC++
-
Posts:
4,725
-
Joined:
16-October 07
Dream Kudos: 575
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese
Posted 06 March 2008 - 05:55 AM
zakary, 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
Posted 06 March 2008 - 06:01 AM
you calling my an old timer baavgai LOL
This post has been edited by zakary: 06 March 2008 - 06:01 AM
Posted 06 March 2008 - 06: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 - 06:55 AM

- Dreaming Coder
-
-
Group:
Expert w/DIC++
-
Posts:
4,725
-
Joined:
16-October 07
Dream Kudos: 575
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese
Posted 06 March 2008 - 08:33 AM
Footsie, 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.

- New D.I.C Head
-
-
Group:
New Members
-
Posts:
1
-
Joined:
17-October 08
Dream Kudos: 0
Posted 17 October 2008 - 10:42 PM
Footsie, 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?
Footsie, 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

- New D.I.C Head
-
-
Group:
New Members
-
Posts:
1
-
Joined:
31-March 09
Dream Kudos: 0
Posted 30 November 2009 - 07: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 - 07:36 AM
5 User(s) are reading this topic
0 members, 5 guests, 0 anonymous users
|
Be Social
Programming
Web Development
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|