CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2000
    Location
    Essex, Uk
    Posts
    1,214

    [RESOLVED] No Constructor Defined??

    Hi All,

    I'm using Winforms.

    I have the following code in a form:

    clsSQL Sql = new clsSQL;
    System.Data.OleDb.OleDbDataReader TreeData = new System.Data.OleDb.OleDbDataReader(); //Offending Line Here
    TreeData = Sql.ReturnData(Sql.ReturnAllStructures);

    In clsSQL I have:

    Public OleDbDataReader ReturnData(string SQL)
    {

    .......
    ......

    System.Data.OleDb.OleDbDataReader myReader = myCommand.ExecuteReader();
    ReturnData = myReader;
    }

    Why is it expecting a constructor when I'm just declaring a new instance?
    If you find my answers helpful, dont forget to rate me

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: No Constructor Defined??

    A class cannot exist without a constructor. There should be atleast a default constructor (it may be empty, but the constructor should be there.

  3. #3
    Join Date
    Aug 2000
    Location
    Essex, Uk
    Posts
    1,214

    Re: No Constructor Defined??

    I've declared

    using System.Data;

    System.Data.OleDb.OleDbDataReader is part of that. How do I create a Constructor for this even an empty one? since I have no explicit class and reference it implicitly through the System Data Object?
    If you find my answers helpful, dont forget to rate me

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: No Constructor Defined??

    Ignore my previous post, I was thinking that you are asking about clsSql class.

    Can you tell us what is the exact error message? I don't see any reason why it should not work.

  5. #5
    Join Date
    Aug 2000
    Location
    Essex, Uk
    Posts
    1,214

    Re: No Constructor Defined??

    Hi Shuja,

    I'm using VS 2005. it's when I press F10. I receive 6 Errors in total, the first error says:

    The Type 'System.Data.OleDb.OleDBDataReader' has no constructors defined.

    When I double click on the Error it jumps to the line on the form in LoadTree where I'm creating TreeData.

    Basically I'm attempting to go get some data from an Access database and place it in a DataReader. I'll then use this to populate a treeview.
    If you find my answers helpful, dont forget to rate me

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: No Constructor Defined??

    Your code should be like this
    Code:
    clsSQL Sql = new clsSQL;
    System.Data.OleDb.OleDbDataReader TreeData = Sql.ReturnData(Sql.ReturnAllStructures);

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: No Constructor Defined??

    Bill,

    It should logically read "No accessible constructors available".

    You can NOT create a raw instance of a DataReader, instances are always created as the result of a method call on some DB related class.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Aug 2000
    Location
    Essex, Uk
    Posts
    1,214

    Re: No Constructor Defined??

    Ah,

    Thanks Shuja, I was following the MSDN Example. I've now cleared that Error, however I now get errors in clsSQL saying that ReturnAllStructures has invalid arguments and that it cannot convert from a method group to a string.



    My Class is made up:

    namespace MyNamespace
    {
    class clsSQL
    {
    Public string ReturnAllStructures()
    {
    Return "Select * from Product";
    }
    Public OleDbDataReader ReturnData(string SQL)
    {
    .........
    Return myReader;
    }
    If you find my answers helpful, dont forget to rate me

  9. #9
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: No Constructor Defined??

    There is no need to do it this way. What you need to it have a private string in clsSQL class that will contain your SQL Query String and then you can use it throughout your class. I would have written it this way
    Code:
    class clsSQL
    {
      Private string ReturnAllStructures = "Select * from Product";
      Public OleDbDataReader ReturnData()
      { 
    	//use ReturnAllStructures directly here to get the data
    	.........
    	Return myReader;
      }
    }
    And then I would call it this way
    Code:
    clsSQL Sql = new clsSQL; System.Data.OleDb.OleDbDataReader TreeData = Sql.ReturnData();


    And you are a Member+ and you should be using Code tags whenever you post code anywhere in this Forum.

  10. #10
    Join Date
    Aug 2000
    Location
    Essex, Uk
    Posts
    1,214

    Re: No Constructor Defined??

    Hi Shuja,

    The reason I'm trying to do it my way is so that ReturnData is generic. I'll then have a series of Available SQL statements that the calling form would use to pass to ReturnData. My current code shows that I am attempting to use a Method (ReturnAllStructures), when I probably really want to make this a read only property (Encapsulation).

    Although you have shown another way that should work, I dont understand why the way I've written it doesn't, even if it's poor coding.

    Sorry about not using code tags, I thought that Indenting did what I wanted all be it that it leaves a blank line between each line of code. I'll try the code tag next time.
    If you find my answers helpful, dont forget to rate me

  11. #11
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: No Constructor Defined??

    If all you want is a readonly property then the way you have written your code is wrong. You should declare it as a property like this
    Code:
    Public string ReturnAllStructures
    {
       get
       {  
           Return "Select * from Product";
       }
    }
    A property has a get and set modifiers and a readonly property will have only get, whereas write-only will only have set.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured