Solved

c# question code errors

Posted on 2011-03-12
5
215 Views
Last Modified: 2012-06-21
I got a error:

A field initializer cannot reference the nonstatic field, method, or property

in this line of code

      private System.Data.SqlClient.SqlConnection Connection = CreateConnection();

I am converting from vb.net to c#
0
Comment
Question by:vbnetcoder
  • 2
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35118879
>CreateConnection();

I presume that the method you call there is part of the same class?
which needed to be a static method (of the same or another class), which is what the error message tells you.

0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 35118880
change to

private System.Data.SqlClient.SqlConnection Connection = null;

and in the constructor put

Connection = CreateConnection();
0
 

Author Comment

by:vbnetcoder
ID: 35118965
Ok that worked but now this:

string strConnString = conString.ConnectionString;


Gives me the error


'string' does not contain a definition for 'ConnectionString'      

public class clsData
{
	private System.Data.SqlClient.SqlConnection Connection = null;
	private System.Data.SqlClient.SqlConnection CreateConnection()
	{

        string conString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString();      
        string strConnString = conString.ConnectionString;

		System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
		con.ConnectionString = strConnString;

		if (con.State == ConnectionState.Closed)
		{
			con.Open();
		}

		return con;

	}

Open in new window

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35118975
remove:
string strConnString = conString.ConnectionString;

and change:
con.ConnectionString = strConnString;

into:
con.ConnectionString = conString;
0
 

Author Closing Comment

by:vbnetcoder
ID: 35119013
ty
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question