ADO Connection String in C#

Hello all,

I have wanted to learn C# for some time now so I am taking the plunge and converting an existing MS Access DB (2007) and converting it over to C#.  It really is more of a front end to run stored procedures and then generate Excel Workbooks all done with VBA. So not a full on database.

 I am working on this line by line from my code.  

I have created my form.

I created System Properties and added the data references to my project.

Now I am just working on getting my connection to the database and I am getting an error  a field initializer cannot reference the nonstatic field method or property in connection string I did a bunch of google searches but could not really figure it out.  Something about my variables could be set to nothing before the string is created.  

It is failing on the construction of ConnectionString where I have my variables.

Here is my code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using ADODB;

namespace widget
{
    class widget
    {

        string myServer = Properties.Settings.Default.Server; 
        string mySQLServer = Properties.Settings.Default.SQLServer;
        string myDatabase = Properties.Settings.Default.Database;
        string myConnectionUser = Properties.Settings.Default.ConnectionUser;
        string myPassword = Properties.Settings.Default.Password;

         ADODB.Connection conn = new ADODB.Connection();

        string ConnectionString = "Driver=SQL Server;Server=" + mySQLServer + ";Database=" + myDatabase + ";UID=" + myConnectionUser + ";PWD=" + myPassword + ";";


       

    }
}

Open in new window


Thanks,
Rodger
LVL 4
RodgerSystems AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
You are getting this error because your fields are non-static, reference values retrieved from a static property (the settings class Default property is static) and, therefore, have no value yet.  So you cannot initialize ConnectionString using their values.  You can either initialize ConnectionString in a constructor (the preferred method); e.g. -
using System;

namespace EE_Q29071236
{
    class Program
    {
        string myServer = Properties.Settings.Default.Server;
        string mySQLServer = Properties.Settings.Default.SQLServer;
        string myDatabase = Properties.Settings.Default.Database;
        string myConnectionUser = Properties.Settings.Default.ConnectionUser;
        string myPassword = Properties.Settings.Default.Password;
        string connectionString;

        Program()
        {
            connectionString = "Driver=SQL Server;Server=" + mySQLServer + ";Database=" + myDatabase + ";UID=" + myConnectionUser + ";PWD=" + myPassword + ";";
        }

        static void Main(string[] args)
        {
            Console.WriteLine(new Program().connectionString);
            Console.ReadLine();
        }
    }
}

Open in new window

Or mark your fields as static; e.g. -
using System;

namespace EE_Q29071236
{
    class Program
    {
        static string myServer = Properties.Settings.Default.Server;
        static string mySQLServer = Properties.Settings.Default.SQLServer;
        static string myDatabase = Properties.Settings.Default.Database;
        static string myConnectionUser = Properties.Settings.Default.ConnectionUser;
        static string myPassword = Properties.Settings.Default.Password;
        string connectionString = "Driver=SQL Server;Server=" + mySQLServer + ";Database=" + myDatabase + ";UID=" + myConnectionUser + ";PWD=" + myPassword + ";";

        static void Main(string[] args)
        {
            Console.WriteLine(new Program().connectionString);
            Console.ReadLine();
        }
    }
}

Open in new window

Each of which produce the following output -Capture.PNG
-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RodgerSystems AnalystAuthor Commented:
OK, I was able to get what I want I am still not sure a little unclear about it all.  I know that it wants to be sure that when I am assigning the values it will be at the right point.  

Like  I said I am new to C#, but I will get there.  We all start somewhere.  

No one to get it all connected and get the Stored Procedure to execute from the form and get my variable.

Loo for another post.

Thanks again.
Rodger
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.