Solved

Trying to learn C# please help!!!

Posted on 2009-05-05
3
233 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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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