Solved

Trying to learn C# please help!!!

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

8 Experts available now in Live!

Get 1:1 Help Now