473,785 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(OleDbDataR eader 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(OleDbDataR eader reader, string query, string
pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection (MySite.GlobalS ettings.Connect ionString);
conn.Open();

OleDbCommand cmd = new OleDbCommand(qu ery, conn);
cmd.CommandType = CommandType.Sto redProcedure;
reader = cmd.ExecuteRead er();

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 5607
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******@llewe llyn.co.uk> wrote in message
news:uT******** ******@TK2MSFTN GP09.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(OleDbDataR eader 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(OleDbDataR eader reader, string query, string
pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection (MySite.GlobalS ettings.Connect ionString);
conn.Open();

OleDbCommand cmd = new OleDbCommand(qu ery, conn);
cmd.CommandType = CommandType.Sto redProcedure;
reader = cmd.ExecuteRead er();

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.GlobalS ettings.Connect ionString);
conn.Open();
Dion

"William Ryan" <do********@nos pam.comcast.net > wrote in message
news:u4******** ******@TK2MSFTN GP10.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******@llewe llyn.co.uk> wrote in message
news:uT******** ******@TK2MSFTN GP09.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(OleDbDataR eader 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(OleDbDataR eader reader, string query, string
pram3 )
{

OleDbConnection conn = null;
//OleDbDataReader reader = null;
string returndata = null;
try
{
conn = new OleDbConnection (MySite.GlobalS ettings.Connect ionString);
conn.Open();

OleDbCommand cmd = new OleDbCommand(qu ery, conn);
cmd.CommandType = CommandType.Sto redProcedure;
reader = cmd.ExecuteRead er();

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*******@neno loje.de> wrote in message
news:uE******** ******@TK2MSFTN GP10.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.GlobalS ettings.Connect ionString);
conn.Open();

OleDbCommand cmd = new OleDbCommand(qu ery, conn);
//cmd.CommandType = CommandType.Tex t;
OleDbDataReader reader = cmd.ExecuteRead er();

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=Micr osoft.Jet.OLEDB .4.0; Data
Source=MyDataba se.mdb");
conn.Open();

OleDbCommand cmd = new OleDbCommand(qu ery, conn);
cmd.CommandType = CommandType.Sto redProcedure;

reader = cmd.ExecuteRead er();

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

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

"Kevin Yu" <v-****@online.mic rosoft.com> wrote in message
news:jE******** *****@cpmsftngx a06.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.GlobalS ettings.Connect ionString);
conn.Open();

OleDbCommand cmd = new OleDbCommand(qu ery, conn);
//cmd.CommandType = CommandType.Tex t;
OleDbDataReader reader = cmd.ExecuteRead er();

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 NullReferenceEx ception 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.c s
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.ExecuteRead er(); 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.mic rosoft.com> wrote in message
news:hV******** ******@cpmsftng xa06.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 NullReferenceEx ception 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 NullReferenceEx ception
will be thrown. You can initialize the GlobalSettings. reader like the
following:

if(this.oleDbCo nnection1.State == ConnectionState .Closed)
this.oleDbConne ction1.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "SELECT * FROM Table1";
cmd.Connection = this.oleDbConne ction1;
GlobalSettings. reader = cmd.ExecuteRead er();
reader.Read();
MySite.GlobalSe ttings.Read("qr yNews",
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

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

Similar topics

3
8130
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
5389
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 function then attempts a File.Delete (also tried the FileInfo delete) on the Excel file, but gets the message "The process cannot access the file ... because it is being used by another process". I've stepped through the code and it steps...
1
2710
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 = comboBox2.Text; objreturnst.Parameters.Value = comboBox1.Text; oleDbConnection1.Open();
0
1378
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 records. I would like to know does the OleDbDataReader contain the actual information on each record i.e. the fields in the database and their values? Or does the ExecuteReader command only contain the number of records present from the used SQL...
1
1968
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 OleDbConnection(ConnectionString); theConn.Open(); OleDbCommand cmd = new OleDbCommand(theSQL, theConn);
1
272
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 other than using a loop? Thanks in advance, Steven Dim spTermType As OleDb.OleDbDataReader
1
2506
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 invalid cast exception. Is it possible to use the OleDbDataReader to get the value of a currency field in an Access database? If so, how? Rob, who probably shouldn't even ask this question, since the OleDbDataReader is too simplistic. I should...
2
1242
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". My question is: Is it necessary to close sDa in my code? '------------------------------------------------------------------------------- Private Function MethodIDExist(ByVal iMethodID As Integer) As Boolean Dim oCmd As New OleDbCommand
1
2087
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 reader? I thought there might be a cleaner way, this code seems to take "forever" to run. '-------BEGINNING OF CODE EXAMPLE------- Dim SBInvNum As String Dim strConInv As String = "Provider=Microsoft.JET.OLEDB.4.0;Data Source =" &...
0
9483
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10096
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9956
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7504
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.