Solved

Trying to learn C# please help!!!

Posted on 2009-05-05
3
234 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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