Connection String problem
-
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
-
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
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) -
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
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
-
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
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
-
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
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.
-
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.
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
-
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
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.