OleDbDataReader as parameter

  • Thread starter Thread starter Dion Heskett
  • Start date Start date
D

Dion Heskett

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.ConnectionString);
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
 
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
 
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.ConnectionString);
conn.Open();


Dion

William Ryan said:
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 said:
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.ConnectionString);
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
 
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.ConnectionString);
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."
 
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;
}
 
Back
Top