Solved

c# question code errors

Posted on 2011-03-12
5
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 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