Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Trying to learn C# please help!!!

Posted on 2009-05-05
3
Medium Priority
?
242 Views
Last Modified: 2012-05-06
This is my first attempt at actual C# code.   I come from VB and I am trying to learn.

I want to open the connection

The problem is the OpenCon(); statement is underlined stating that a get or set accessor is needed. Frankly, I plainly do not understand and any help would be good.
namespace ToysName
{
    class Toys
    {
        SqlConnection sqlcon = new SqlConnection("Server = BV-SQL1; Database = toys; Trusted_Connection = True; Connection Timeout = 0");
    public DataTable Toys
       {
          
OpenCon();
 
        
 
 
 
 
 
 
        }
        private void OpenCon()
        {
            sqlcon.Open();
          
        }
        private void CloseCon()
        {
            sqlcon.Close();
            sqlcon.Dispose();
          
        }
    
    
    
    }
}

Open in new window

0
Comment
Question by:suicehockey44
3 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 2000 total points
ID: 24309075
The error is actually on line 6.  You need parentheses:
public DataTable Toys()

Otherwise, it thinks that Toys is supposed to be a property name -- in which case you would need get and /or set accessors.
0
 
LVL 1

Expert Comment

by:pras_gupta
ID: 24309085
Your method
public DataTable Toys
       {
         
OpenCon();
}
returns a DataTable, so please either return a DataTable or Make the methid void.
It is underlining OpenCon(); because it expects that Toys will return something.
 
0
 
LVL 6

Expert Comment

by:RPCIT
ID: 24309104
most of the problem with your code set.. is that you need the parens () in the signature for Toys.  All methods in C# require an arguments section, even if it's empty.

public DataTable Toys()

On a side note, you will need to return a datatable inside that method...

something like below
            public DataTable Toys()
            {
                OpenCon();
                SqlCommand cmd = new SqlCommand("SELECT * FROM SomeTable",sqlcon);
                SqlDataReader rdr = cmd.ExecuteReader();
                DataTable dt = new DataTable("tableName");
                dt.Load(rdr);
                return dt;
            }

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

824 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