473,508 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class for DB connection to return dataset

I'm new to C#, so forgive me if this is n00b stuff. I've been
researching creating classes to hold commonly used methods, etc across
multiple WebForms. To a degree I understand this and managed to get a
"Hello World" example running where in my common.cs file I had:

public class dbConnection {
public const string iGreeting = "Hello World!";
}

and in my aspx files I simply put:

Response.Write(dbConnection.iGreeting.ToString());

what I'd like this method, dbConnection, to do is take a string (the
SQL Query, called 'sSQLQuery' (inventive, eh?)) and return a dataset.

my attempt failed (naturally, or I'd not be posting here), so I wonder
if a) this can be done at all, and b) what's wrong with my code (please
be gentle!!)

Expand|Select|Wrap|Line Numbers
  1. public object sReturnedDataSet() {
  2. string oradb = "Data Source=XXX;User Id=YYY;Password=ZZZ;";
  3. OracleConnection conn = new OracleConnection(oradb);
  4. // define the query & command
  5. string sSQLQuery = "SELECT * FROM table1";
  6.  
  7. // define the command
  8. OracleCommand cmd = new OracleCommand(sSQLQuery,conn);
  9.  
  10. // open the connection
  11. conn.Open();
  12.  
  13. cmd.CommandType = CommandType.Text;
  14. OracleDataReader dr = cmd.ExecuteReader();
  15.  
  16. return dr;
  17. }
  18.  
then I attempted to use this, like so:
Expand|Select|Wrap|Line Numbers
  1. Response.Write(dbConnection.sReturnedDataSet["field_name"].ToString();
  2.  
However, this is where it went belly up and died on me. When I tried to
browse the ASPX file, I got the following 'build' error:

'An object reference is required for the nonstatic field, method, or
property 'reports.dbConnection.sReturnedDataSet()'

can anyone help me out with some pointers, some sample code or a
website that might help explain it?

All help appreciated.

Nov 17 '05 #1
8 1889
Your sReturnedDataSet() method needs to be marked as static.

Nov 17 '05 #2
so I need to update

Expand|Select|Wrap|Line Numbers
  1. public object sReturnedDataSet() {
  2.  
to be

Expand|Select|Wrap|Line Numbers
  1. public static object sReturnedDataSet() {
  2.  
and the way I'm using it should work? i.e.

Expand|Select|Wrap|Line Numbers
  1. Response.Write(dbConnection.sReturnedDataSet["field_name"].ToString();
  2.  
Thanks for the response, so far

Nov 17 '05 #3
No, you're either going to need a strongly typed return value or cast
the return value (the former is better).

ie
public static OracleDataReader ReturnedDataReader();

-or-

Response.Write ( ( (OracleDataReader)
dbConnection.sReturnedDataSet)["field_name"].ToString());

Also a word of advice if you're going to be using an Oracle DB, Use the
Oracle managed provider instead of the microsoft provided one
(especially if you're going to be dealing with transaction savepoints).
http://www.oracle.com/technology/tec...net/index.html

Nov 17 '05 #4
just because I can ask, and won't be in the office to try for another
few hours.. let me see if I got this straight:

I should use, in common.cs
public static OracleDataReader ReturnedDataReader();

and then I can use in WebForm1.aspx
Response.Write(dbConnection.sReturnedDataSet["field_name"].ToString();

that about right?

Nov 17 '05 #5
dbConnection.sReturnedDataSet()["field_name"].ToString();

Nov 17 '05 #6
Thanks for the help so far. I'm still having some problems though. Here
are my 2 files, as they stand after going through this thread:

[code "common.cs"]
public sealed class dbConnection
{
public const string iGreeting = "Hello World!";

public static OracleDataReader sReturnedDataReader()
{
string oradb = "Data Source=xxx;User Id=yyy;Password=zzz;";
OracleConnection conn = new OracleConnection(oradb);
string sSQLQuery = "SELECT * FROM members WHERE rownum < 500";

OracleCommand cmd = new OracleCommand(sSQLQuery,conn);

conn.Open();

cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();

return dr;

}

}
[/code]
AND

[code "WebForm1.aspx.cs"]
Response.Write(dbConnection.sReturnedDataReader()["member_id"].ToString());
[/code]
when I browse my webform, I get the following error: "Invalid attempt
to read when no data is present"

I'm fairly confident that the data is being returned (if I change the
method to return a string, I can create a string that equals the row
count of the datareader), but for some reason when I try and output
anything from the returned "dr" I get this message.

any ideas?

Nov 17 '05 #7
you need to do a dr.Read() which returns a boolean specifying whether
there is any more data so it's probably best done on the client.

OracleDataReader dr = dbConnection.sReturnedDataReader();

while(dr.Read())
{
Response.Write(dr["member_id"].ToString());

}

Nov 17 '05 #8
Awesome! Once I realized I had to keep "return dr;" in common.cs, as
well as add the new dr = line to my webform, I was able to get this
working!!

Many thanks for the help.. it's very much appreciated.

(now all I need to do is figure out how to pass the sSQLQuery string to
the method, so that I can call this same method will a multitude of
different queries.. but I wanna try and figure something out myself.
That said.. I might be posting again soon <g>)

Thanks again.

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2117
by: Jim | last post by:
Is it possible to have a Connection class that opens up connection to my DB and leaves it open until I decide to close it? I run many SQL statements so I was wondering if it's possible to call a...
2
1541
by: Jim | last post by:
I'm writing an Invoicing Windows app but I'm writing it to make the code as easy to maintain as possible. Basically, to get any records from my DB, I use two classes: one that sets up the SQL...
6
1584
by: Peter Afonin | last post by:
Hello: I'm trying to convert the connection class that I created in VB.Net for ASP.Net application into C#. I'm doing something incorrectly, because I'm getting the following error when trying...
1
1194
by: freddy | last post by:
I have a class with all the database stuff in there like path,add, delete, and update. I also have a form that calls the class like so: DatabaseManager manager = new DatabaseManager();...
5
2586
by: Lasse Edsvik | last post by:
Hello Im trying to create a simple testclass that connects to a db on localhost and a method that returns a dataset. I get these errors: Unhandled Exception: System.InvalidOperationException:...
2
2284
by: Lior | last post by:
Hi, I have an ASP.NET website that crashes under heavy load. I use a SQL Server DB. I get around 5500 hits per day. I keep getting the timeout expieried connection pool error. Sometimes it even...
9
2483
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
10
9074
by: Fred Mertz | last post by:
I'm wondering what some of the important considerations are regarding implementing the DAL as a static class vs creating instances of it as needed. I'm writing a .NET 2.0 Windows application...
14
1589
by: gggram2000 | last post by:
Can anyone tell me if this class will function properly...I'm using MS Visual Studio 2005, using C#. I notice there's no C# forum but hopefully u guys are familiar with the language...thanks in...
1
2761
by: DarkGiank | last post by:
Hi, im new to csharp and im trying to create a class that can change the application database without no rewriting all connection code... but cause some reason it is not working... it tells me that...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.