Click to See Complete Forum and Search --> : [RESOLVED] No Constructor Defined??


Bill Crawley
December 18th, 2006, 05:19 AM
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?

Shuja Ali
December 18th, 2006, 05:35 AM
A class cannot exist without a constructor. There should be atleast a default constructor (it may be empty, but the constructor should be there.

Bill Crawley
December 18th, 2006, 06:02 AM
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?

Shuja Ali
December 18th, 2006, 06:17 AM
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.

Bill Crawley
December 18th, 2006, 06:53 AM
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.

Shuja Ali
December 18th, 2006, 07:41 AM
Your code should be like this clsSQL Sql = new clsSQL;
System.Data.OleDb.OleDbDataReader TreeData = Sql.ReturnData(Sql.ReturnAllStructures);

TheCPUWizard
December 18th, 2006, 07:49 AM
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.

Bill Crawley
December 18th, 2006, 09:49 AM
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;
}

Shuja Ali
December 19th, 2006, 12:47 AM
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 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
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.

Bill Crawley
December 19th, 2006, 12:57 AM
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.

Shuja Ali
December 19th, 2006, 01:09 AM
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 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.