473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

OleDbDataReader as parameter


I wish to supply the formating as a parameter, I have tried OleDbDataReader
but get a null ref.
just can't seem to get this to work. I think this has something to do with
ExecuteReader.

also can't seem to use this as the first pram
OleDbDataReader reader = new OleDbDataReader()

i.e
public static string Read(OleDbDataReader reader, string query, string
format )

this is what I would like as a parameter if possible. "<p> +
reader["Text"].ToString() + "</p>" + "\n"
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
public static string Read(OleDbDataReader reader, string query, string
pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn.Open();

OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();

while( reader.Read() )
{
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
}
}

// catch (Exception e)
// {
// Response.Write(e.Message);
// Response.End();
// }
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
return returndata;
}

Thanks

Dion
Nov 15 '05 #1
10 5567
Why do you want to pass the DataReader into the method?!?
Nov 15 '05 #2
Dion:

In your example, you It doesn't look like you are getting anything from
passing it in. If you declared it locally and returned it, you'd be in the
same place. Problem with a Reader and the way you are using it is that it
needs an open connection to iterate through itself. A reader that can't
talk to the DB can't do anything for you other than throw exceptions. So
you could conciveably return a reader from a method in another class, and
everything it needs could be out of scope / rather, unaccessible.

If you used the same code though, and returned a reader as the Datatype
you'd be in the same boat, although I don't think either will work.

Couldn't you just iterate through the reader and declare it locally, and
just return a string or stringbuilder with all the data formatted?

I probably don't totally understand the methodology, but we can probably get
whatever you want to do to work nonetheless.

Cheers,

Bill
"Dion Heskett" <dh******@llewellyn.co.uk> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...

I wish to supply the formating as a parameter, I have tried OleDbDataReader but get a null ref.
just can't seem to get this to work. I think this has something to do with
ExecuteReader.

also can't seem to use this as the first pram
OleDbDataReader reader = new OleDbDataReader()

i.e
public static string Read(OleDbDataReader reader, string query, string
format )

this is what I would like as a parameter if possible. "<p> +
reader["Text"].ToString() + "</p>" + "\n"
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
public static string Read(OleDbDataReader reader, string query, string
pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn.Open();

OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();

while( reader.Read() )
{
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
}
}

// catch (Exception e)
// {
// Response.Write(e.Message);
// Response.End();
// }
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
return returndata;
}

Thanks

Dion

Nov 15 '05 #3
It if do this then i will need to create this function on every page that i
use it, i'am trying to avoid this.

I have an overloaded version that works for a repeater/datagrid.

all i do is declare a repeater or datagrid object and pass in a control and
query, check if it's a datagrid or repeater and bind, then close the
database connection.
ie Read(repeater1, "MyQuery")

You also say it needs an open connection, the below code opens the DB

conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn.Open();
Dion

"William Ryan" <do********@nospam.comcast.net> wrote in message
news:u4**************@TK2MSFTNGP10.phx.gbl...
Dion:

In your example, you It doesn't look like you are getting anything from
passing it in. If you declared it locally and returned it, you'd be in the same place. Problem with a Reader and the way you are using it is that it
needs an open connection to iterate through itself. A reader that can't
talk to the DB can't do anything for you other than throw exceptions. So
you could conciveably return a reader from a method in another class, and
everything it needs could be out of scope / rather, unaccessible.

If you used the same code though, and returned a reader as the Datatype
you'd be in the same boat, although I don't think either will work.

Couldn't you just iterate through the reader and declare it locally, and
just return a string or stringbuilder with all the data formatted?

I probably don't totally understand the methodology, but we can probably get whatever you want to do to work nonetheless.

Cheers,

Bill
"Dion Heskett" <dh******@llewellyn.co.uk> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...

I wish to supply the formating as a parameter, I have tried

OleDbDataReader
but get a null ref.
just can't seem to get this to work. I think this has something to do with ExecuteReader.

also can't seem to use this as the first pram
OleDbDataReader reader = new OleDbDataReader()

i.e
public static string Read(OleDbDataReader reader, string query, string
format )

this is what I would like as a parameter if possible. "<p> +
reader["Text"].ToString() + "</p>" + "\n"
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
public static string Read(OleDbDataReader reader, string query, string
pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn.Open();

OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();

while( reader.Read() )
{
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" ); }
}

// catch (Exception e)
// {
// Response.Write(e.Message);
// Response.End();
// }
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
return returndata;
}

Thanks

Dion


Nov 15 '05 #4

So i can access the reader["Fields"]

"Neno Loje [MSP]" <ne*******@nenoloje.de> wrote in message
news:uE**************@TK2MSFTNGP10.phx.gbl...
Why do you want to pass the DataReader into the method?!?

Nov 15 '05 #5
Hi Dion,

I'm still not sure why you pass this OleDbDataReader parameter to the
function. Because you didn't use it in the Read() function but assigned a
value to it. This can only result in exceptions. Did you get any error
messages? I recommend you to declare this DataReader locally, so that it
might work. Here's a code example:

public string Read(string query, string pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn.Open();

OleDbCommand cmd = new OleDbCommand(query, conn);
//cmd.CommandType = CommandType.Text;
OleDbDataReader reader = cmd.ExecuteReader();

while( reader.Read() )
{
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
}
}
catch (Exception e)
{
Response.Write(e.Message);
Response.End();
}
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
return returndata;
}
If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 15 '05 #6
Ok now i get

An object reference is required for the nonstatic filed, method or property
reader
public OleDbDataReader reader = null;
Read("qryNews", reader["Text"].ToString());
private static string Read(string query, string pramx)
{
OleDbConnection conn = null;
//OleDbDataReader reader = null;
try
{
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=MyDatabase.mdb");
conn.Open();

OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.CommandType = CommandType.StoredProcedure;

reader = cmd.ExecuteReader();

while (reader.Read())
{
returndata += (pramx);
}

}
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
return returndata;
}

"Kevin Yu" <v-****@online.microsoft.com> wrote in message
news:jE*************@cpmsftngxa06.phx.gbl...
Hi Dion,

I'm still not sure why you pass this OleDbDataReader parameter to the
function. Because you didn't use it in the Read() function but assigned a
value to it. This can only result in exceptions. Did you get any error
messages? I recommend you to declare this DataReader locally, so that it
might work. Here's a code example:

public string Read(string query, string pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection(MySite.GlobalSettings.ConnectionSt ring);
conn.Open();

OleDbCommand cmd = new OleDbCommand(query, conn);
//cmd.CommandType = CommandType.Text;
OleDbDataReader reader = cmd.ExecuteReader();

while( reader.Read() )
{
//returndata += ("<p> + reader["Text"].ToString() + "</p>" + "\n" );
}
}
catch (Exception e)
{
Response.Write(e.Message);
Response.End();
}
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
return returndata;
}
If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 15 '05 #7
Hi Dion,

Your code of Read() works fine on my computer. In your post, you got an "An
object reference is required for the nonstatic filed, method or property
reader" error message in your program. Would you please tell me on which
statement did this error occur?

I noticed these two statements in your code.
public OleDbDataReader reader = null;
Read("qryNews", reader["Text"].ToString());


Which function are these two lines located? If you run these two statements
a NullReferenceException will be thrown, because the reader didn't refer to
any object.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 15 '05 #8
Did you see the attached code ?

when GlobalSettings.Read("qryNews", reader["Text"].ToString()); from
WebForm1.aspx.cs
is compile I get this.

The name 'reader' does not exist in the class or nameapace.

This is because the function does not know anything about the reader, it not
created untill
reader = cmd.ExecuteReader(); is called in the GlobalSettings.Read()
function located in
GlobalSettings.cs file.

How do access pass in the the reader and access this information in a static
function ? is this even possible !

Thnaks

Dion
"Kevin Yu" <v-****@online.microsoft.com> wrote in message
news:hV**************@cpmsftngxa06.phx.gbl...
Hi Dion,

Your code of Read() works fine on my computer. In your post, you got an "An object reference is required for the nonstatic filed, method or property
reader" error message in your program. Would you please tell me on which
statement did this error occur?

I noticed these two statements in your code.
public OleDbDataReader reader = null;
Read("qryNews", reader["Text"].ToString());
Which function are these two lines located? If you run these two

statements a NullReferenceException will be thrown, because the reader didn't refer to any object.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 15 '05 #9
Hi Dion,

I've read all the attached codes. If you want to access the static member
of a class, you have to specify the class name of it. For example,
GloabalSettings.reader. Before you call GlobalSettings.Read(), you have to
assign initial value to GlobalSettings.reader. Or a NullReferenceException
will be thrown. You can initialize the GlobalSettings.reader like the
following:

if(this.oleDbConnection1.State == ConnectionState.Closed)
this.oleDbConnection1.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "SELECT * FROM Table1";
cmd.Connection = this.oleDbConnection1;
GlobalSettings.reader = cmd.ExecuteReader();
reader.Read();
MySite.GlobalSettings.Read("qryNews",
GlobalSettings.reader["Text"].ToString());

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 15 '05 #10
Ok , Thanks..

"Kevin Yu" <v-****@online.microsoft.com> wrote in message
news:W9****************@cpmsftngxa06.phx.gbl...
Hi Dion,

I've read all the attached codes. If you want to access the static member
of a class, you have to specify the class name of it. For example,
GloabalSettings.reader. Before you call GlobalSettings.Read(), you have to
assign initial value to GlobalSettings.reader. Or a NullReferenceException
will be thrown. You can initialize the GlobalSettings.reader like the
following:

if(this.oleDbConnection1.State == ConnectionState.Closed)
this.oleDbConnection1.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "SELECT * FROM Table1";
cmd.Connection = this.oleDbConnection1;
GlobalSettings.reader = cmd.ExecuteReader();
reader.Read();
MySite.GlobalSettings.Read("qryNews",
GlobalSettings.reader["Text"].ToString());

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 15 '05 #11

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

Similar topics

3
by: Buddy | last post by:
Hello, How do I get the number of row retrieved on a SELECT query when using OleDbDataReader? I am using Framework 1.0.
7
by: Smitty | last post by:
I have a function that imports an Excel file into an Access table using SQL. I then close the OleDataReader and the OleDbConnection, then dispose the OleDbCommand, then OleDbConnection. The calling...
1
by: Claudia Fong | last post by:
Hi, I'm using the oleDbDataReader HasRows properties to detect wheter my table contains a record with the parameter below: oleDbConnection1.Close(); objreturnst.Parameters.Value =...
0
by: Steve1 via DotNetMonster.com | last post by:
Hi all, I''ve created an OleDbDataReader that will contain the number of records that the used SQL query will return. I'm then using a while loop to loop while the OleDBDataReader object has...
1
by: Keith | last post by:
Hi, if have this method in a cs file that connections to a database and returns a datareader to my web form. public OleDbDataReader GetDataReader(string theSQL) { theConn = new...
1
by: Steven K | last post by:
Hello, I am using the following code which works. My question is does the OleDbDataReader need to be closed and then re-executed for every control? Is there a more effecient way of doing this...
1
by: Rob Richardson | last post by:
Greetings! I have a database created in MS Access that has some fields containing Currency data. Every time I try to use one of the OleDbDataReader's GetXXX() methods to read it, I get an...
2
by: Peter | last post by:
Hi, everybody, The codes below run under VS2003 for a long time. I want to upgrade it to VS2005. VS2005 gives me some varning messages such as "Varibles shouldn't be used before being assigned". ...
1
by: Mel | last post by:
I am performing the same recordset multiple times, just passing different parameters each time. Is there a way to do this more efficiently without having to close and re-open the connection and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.