Solved

c# question code errors

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now