Solved

Trying to learn C# please help!!!

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

15 Experts available now in Live!

Get 1:1 Help Now