Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Connection String problem

Connection String problem

Scheduled Pinned Locked Moved C#
helpcsharpdatabaseasp-netwcf
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    si_69
    wrote on last edited by
    #1

    Hi I am having trouble building my database connection string, i have to read the server name, db, un, and password from an INI file (dont ask) This is no problem, but im having trouble using these values to build my connection string currently im getting the error A field initializer cannont reference the nonstatic field, method or property my knowledge of C# is basic to say the least, can anyone point me in the correct diretion or help at all? thanks Simon Code Below

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    using System.Data.SqlClient;
    using System.IO;
    using System.Text;
    using System.Net;
    using System.Xml;

    using System.Runtime.InteropServices;
    namespace WebService1
    {
    /// /// Summary description for Service1.
    ///
    public class Service1 : System.Web.Services.WebService
    {
    string con;
    string ini_path;
    string tc_server="";
    string tc_database="";
    string tc_un="";
    string tc_pwd="";
    string tc_server_str="";
    public Service1()
    {
    //CODEGEN: This call is required by the ASP.NET Web Services Designer
    InitializeComponent();
    ReadIniSettings();
    con=CreateConnectionString();
    }
    SqlConnection myConnectionCP2 = new SqlConnection(con);

    	#region Component Designer generated code
    	
    	//Required by the Web Services Designer 
    	private IContainer components = null;
    			
    	/// /// Required method for Designer support - do not modify
    	/// the contents of this method with the code editor.
    	/// 
    	private void InitializeComponent()
    	{
    	}
    
    	/// /// Clean up any resources being used.
    	/// 
    	protected override void Dispose( bool disposing )
    	{
    		if(disposing && components != null)
    		{
    			components.Dispose();
    		}
    		base.Dispose(disposing);		
    	}
    	
    	#endregion
    	void ReadIniSettings()
    	{
    		ini\_path="F:\\\\Web\\\\tr\\\\htdocs\\\\cpbl4\\\\";
    		tc\_server = IniFile.ReadValue(ini\_path,"TC","Server");
    		tc\_database=IniFile.ReadValue(ini\_path,"TC","Database");
    		tc\_un=IniFile.ReadValue(ini\_path,"TC","UN");
    		tc\_pwd=IniFile.ReadValue(ini\_path,"TC","Pwd");
    	
    	}
    	string CreateConnectionString()
    	{
    		tc\_server\_str=SQLConn.CreateConnStr(tc\_server,tc\_un,tc\_pwd,tc\_database);
    		return tc\_server\_str;
    		
    	}
    }
    public class IniFile
    {
    	\[DllImport("kernel32")\]
    	private static extern long WritePrivateP
    
    I P D L 4 Replies Last reply
    0
    • S si_69

      Hi I am having trouble building my database connection string, i have to read the server name, db, un, and password from an INI file (dont ask) This is no problem, but im having trouble using these values to build my connection string currently im getting the error A field initializer cannont reference the nonstatic field, method or property my knowledge of C# is basic to say the least, can anyone point me in the correct diretion or help at all? thanks Simon Code Below

      using System;
      using System.Collections;
      using System.ComponentModel;
      using System.Data;
      using System.Diagnostics;
      using System.Web;
      using System.Web.Services;
      using System.Data.SqlClient;
      using System.IO;
      using System.Text;
      using System.Net;
      using System.Xml;

      using System.Runtime.InteropServices;
      namespace WebService1
      {
      /// /// Summary description for Service1.
      ///
      public class Service1 : System.Web.Services.WebService
      {
      string con;
      string ini_path;
      string tc_server="";
      string tc_database="";
      string tc_un="";
      string tc_pwd="";
      string tc_server_str="";
      public Service1()
      {
      //CODEGEN: This call is required by the ASP.NET Web Services Designer
      InitializeComponent();
      ReadIniSettings();
      con=CreateConnectionString();
      }
      SqlConnection myConnectionCP2 = new SqlConnection(con);

      	#region Component Designer generated code
      	
      	//Required by the Web Services Designer 
      	private IContainer components = null;
      			
      	/// /// Required method for Designer support - do not modify
      	/// the contents of this method with the code editor.
      	/// 
      	private void InitializeComponent()
      	{
      	}
      
      	/// /// Clean up any resources being used.
      	/// 
      	protected override void Dispose( bool disposing )
      	{
      		if(disposing && components != null)
      		{
      			components.Dispose();
      		}
      		base.Dispose(disposing);		
      	}
      	
      	#endregion
      	void ReadIniSettings()
      	{
      		ini\_path="F:\\\\Web\\\\tr\\\\htdocs\\\\cpbl4\\\\";
      		tc\_server = IniFile.ReadValue(ini\_path,"TC","Server");
      		tc\_database=IniFile.ReadValue(ini\_path,"TC","Database");
      		tc\_un=IniFile.ReadValue(ini\_path,"TC","UN");
      		tc\_pwd=IniFile.ReadValue(ini\_path,"TC","Pwd");
      	
      	}
      	string CreateConnectionString()
      	{
      		tc\_server\_str=SQLConn.CreateConnStr(tc\_server,tc\_un,tc\_pwd,tc\_database);
      		return tc\_server\_str;
      		
      	}
      }
      public class IniFile
      {
      	\[DllImport("kernel32")\]
      	private static extern long WritePrivateP
      
      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      Probably this line: SqlConnection myConnectionCP2 = new SqlConnection(con); You're trying to create the SQL connection in a field initializer... Not a good thing. That should be initialized inside the constructor (Or inside another function).

      Proud to have finally moved to the A-Ark. Which one are you in?
      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

      1 Reply Last reply
      0
      • S si_69

        Hi I am having trouble building my database connection string, i have to read the server name, db, un, and password from an INI file (dont ask) This is no problem, but im having trouble using these values to build my connection string currently im getting the error A field initializer cannont reference the nonstatic field, method or property my knowledge of C# is basic to say the least, can anyone point me in the correct diretion or help at all? thanks Simon Code Below

        using System;
        using System.Collections;
        using System.ComponentModel;
        using System.Data;
        using System.Diagnostics;
        using System.Web;
        using System.Web.Services;
        using System.Data.SqlClient;
        using System.IO;
        using System.Text;
        using System.Net;
        using System.Xml;

        using System.Runtime.InteropServices;
        namespace WebService1
        {
        /// /// Summary description for Service1.
        ///
        public class Service1 : System.Web.Services.WebService
        {
        string con;
        string ini_path;
        string tc_server="";
        string tc_database="";
        string tc_un="";
        string tc_pwd="";
        string tc_server_str="";
        public Service1()
        {
        //CODEGEN: This call is required by the ASP.NET Web Services Designer
        InitializeComponent();
        ReadIniSettings();
        con=CreateConnectionString();
        }
        SqlConnection myConnectionCP2 = new SqlConnection(con);

        	#region Component Designer generated code
        	
        	//Required by the Web Services Designer 
        	private IContainer components = null;
        			
        	/// /// Required method for Designer support - do not modify
        	/// the contents of this method with the code editor.
        	/// 
        	private void InitializeComponent()
        	{
        	}
        
        	/// /// Clean up any resources being used.
        	/// 
        	protected override void Dispose( bool disposing )
        	{
        		if(disposing && components != null)
        		{
        			components.Dispose();
        		}
        		base.Dispose(disposing);		
        	}
        	
        	#endregion
        	void ReadIniSettings()
        	{
        		ini\_path="F:\\\\Web\\\\tr\\\\htdocs\\\\cpbl4\\\\";
        		tc\_server = IniFile.ReadValue(ini\_path,"TC","Server");
        		tc\_database=IniFile.ReadValue(ini\_path,"TC","Database");
        		tc\_un=IniFile.ReadValue(ini\_path,"TC","UN");
        		tc\_pwd=IniFile.ReadValue(ini\_path,"TC","Pwd");
        	
        	}
        	string CreateConnectionString()
        	{
        		tc\_server\_str=SQLConn.CreateConnStr(tc\_server,tc\_un,tc\_pwd,tc\_database);
        		return tc\_server\_str;
        		
        	}
        }
        public class IniFile
        {
        	\[DllImport("kernel32")\]
        	private static extern long WritePrivateP
        
        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        This is the offending line:

        SqlConnection myConnectionCP2 = new SqlConnection(con);

        Basically, you are using the value of one field while attempting to initialise another outside a method. This is not allowed - after all, what value is it? What you need to do is create the SqlConnection object, and then initialise it inside a method (although, in your example, you aren't using it anywhere).

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        1 Reply Last reply
        0
        • S si_69

          Hi I am having trouble building my database connection string, i have to read the server name, db, un, and password from an INI file (dont ask) This is no problem, but im having trouble using these values to build my connection string currently im getting the error A field initializer cannont reference the nonstatic field, method or property my knowledge of C# is basic to say the least, can anyone point me in the correct diretion or help at all? thanks Simon Code Below

          using System;
          using System.Collections;
          using System.ComponentModel;
          using System.Data;
          using System.Diagnostics;
          using System.Web;
          using System.Web.Services;
          using System.Data.SqlClient;
          using System.IO;
          using System.Text;
          using System.Net;
          using System.Xml;

          using System.Runtime.InteropServices;
          namespace WebService1
          {
          /// /// Summary description for Service1.
          ///
          public class Service1 : System.Web.Services.WebService
          {
          string con;
          string ini_path;
          string tc_server="";
          string tc_database="";
          string tc_un="";
          string tc_pwd="";
          string tc_server_str="";
          public Service1()
          {
          //CODEGEN: This call is required by the ASP.NET Web Services Designer
          InitializeComponent();
          ReadIniSettings();
          con=CreateConnectionString();
          }
          SqlConnection myConnectionCP2 = new SqlConnection(con);

          	#region Component Designer generated code
          	
          	//Required by the Web Services Designer 
          	private IContainer components = null;
          			
          	/// /// Required method for Designer support - do not modify
          	/// the contents of this method with the code editor.
          	/// 
          	private void InitializeComponent()
          	{
          	}
          
          	/// /// Clean up any resources being used.
          	/// 
          	protected override void Dispose( bool disposing )
          	{
          		if(disposing && components != null)
          		{
          			components.Dispose();
          		}
          		base.Dispose(disposing);		
          	}
          	
          	#endregion
          	void ReadIniSettings()
          	{
          		ini\_path="F:\\\\Web\\\\tr\\\\htdocs\\\\cpbl4\\\\";
          		tc\_server = IniFile.ReadValue(ini\_path,"TC","Server");
          		tc\_database=IniFile.ReadValue(ini\_path,"TC","Database");
          		tc\_un=IniFile.ReadValue(ini\_path,"TC","UN");
          		tc\_pwd=IniFile.ReadValue(ini\_path,"TC","Pwd");
          	
          	}
          	string CreateConnectionString()
          	{
          		tc\_server\_str=SQLConn.CreateConnStr(tc\_server,tc\_un,tc\_pwd,tc\_database);
          		return tc\_server\_str;
          		
          	}
          }
          public class IniFile
          {
          	\[DllImport("kernel32")\]
          	private static extern long WritePrivateP
          
          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #4

          The error is being generated on this line;

          si_69 wrote:

          SqlConnection myConnectionCP2 = new SqlConnection(con);

          which appears at present after the block shown below, with a bit of jiggery pokery, move the object declaration and the initilisation so you end up with the code refactored to;

                      SqlConnection myConnectionCP2;               //Declare the object here
          
          	public Service1()
          	{
          		//CODEGEN: This call is required by the ASP.NET Web Services Designer
          		InitializeComponent();
          		ReadIniSettings();
          		con=CreateConnectionString();
                              myConnectionCP2 = new SqlConnection(con);   //Initialise the object here
          	}
          

          That should hopefully fix the problem

          Dave Find Me On: Web|Facebook|Twitter|LinkedIn


          Folding Stats: Team CodeProject

          1 Reply Last reply
          0
          • S si_69

            Hi I am having trouble building my database connection string, i have to read the server name, db, un, and password from an INI file (dont ask) This is no problem, but im having trouble using these values to build my connection string currently im getting the error A field initializer cannont reference the nonstatic field, method or property my knowledge of C# is basic to say the least, can anyone point me in the correct diretion or help at all? thanks Simon Code Below

            using System;
            using System.Collections;
            using System.ComponentModel;
            using System.Data;
            using System.Diagnostics;
            using System.Web;
            using System.Web.Services;
            using System.Data.SqlClient;
            using System.IO;
            using System.Text;
            using System.Net;
            using System.Xml;

            using System.Runtime.InteropServices;
            namespace WebService1
            {
            /// /// Summary description for Service1.
            ///
            public class Service1 : System.Web.Services.WebService
            {
            string con;
            string ini_path;
            string tc_server="";
            string tc_database="";
            string tc_un="";
            string tc_pwd="";
            string tc_server_str="";
            public Service1()
            {
            //CODEGEN: This call is required by the ASP.NET Web Services Designer
            InitializeComponent();
            ReadIniSettings();
            con=CreateConnectionString();
            }
            SqlConnection myConnectionCP2 = new SqlConnection(con);

            	#region Component Designer generated code
            	
            	//Required by the Web Services Designer 
            	private IContainer components = null;
            			
            	/// /// Required method for Designer support - do not modify
            	/// the contents of this method with the code editor.
            	/// 
            	private void InitializeComponent()
            	{
            	}
            
            	/// /// Clean up any resources being used.
            	/// 
            	protected override void Dispose( bool disposing )
            	{
            		if(disposing && components != null)
            		{
            			components.Dispose();
            		}
            		base.Dispose(disposing);		
            	}
            	
            	#endregion
            	void ReadIniSettings()
            	{
            		ini\_path="F:\\\\Web\\\\tr\\\\htdocs\\\\cpbl4\\\\";
            		tc\_server = IniFile.ReadValue(ini\_path,"TC","Server");
            		tc\_database=IniFile.ReadValue(ini\_path,"TC","Database");
            		tc\_un=IniFile.ReadValue(ini\_path,"TC","UN");
            		tc\_pwd=IniFile.ReadValue(ini\_path,"TC","Pwd");
            	
            	}
            	string CreateConnectionString()
            	{
            		tc\_server\_str=SQLConn.CreateConnStr(tc\_server,tc\_un,tc\_pwd,tc\_database);
            		return tc\_server\_str;
            		
            	}
            }
            public class IniFile
            {
            	\[DllImport("kernel32")\]
            	private static extern long WritePrivateP
            
            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            compilers usually produce messages including filenames and line numbers. Use them to your advantage, and make sure your IDE always shows line numbers in source editor windows. For Visual Studio, check menu Tools/Options/Text Editor/All Languages/General: "Display Line Numbers". :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            P 1 Reply Last reply
            0
            • L Luc Pattyn

              compilers usually produce messages including filenames and line numbers. Use them to your advantage, and make sure your IDE always shows line numbers in source editor windows. For Visual Studio, check menu Tools/Options/Text Editor/All Languages/General: "Display Line Numbers". :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Let's not forget that double clicking the error should take you to the exact line.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              L 1 Reply Last reply
              0
              • P Pete OHanlon

                Let's not forget that double clicking the error should take you to the exact line.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                When using VS and all happens to be well, which could be a stretch. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups