Solved

c# question code errors

Posted on 2011-03-12
5
216 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 143

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 143

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

838 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