Showing posts with label datareader. Show all posts
Showing posts with label datareader. Show all posts

Monday, March 26, 2012

Trouble getting row count from DataReader

I am using the DataReader to populate an ASP table dynamically. I would
like to set the tables "visible" property based on the the data from my
query. For example, if I have at least one row of data, I will set visible
to true, otherwise, false.

How get I find out if I have at least one row of data? The code below is
from the CodeBehind of my asp form.

*****************************
OracleConnection conn = new OracleConnection(ConnectionString);
OracleCommand cmd = new OracleCommand("select * from customer where name =
'BOB', conn);
try
{
conn.Open();
IDataReader reader = cmd.ExecuteReader();
_grid.DataSource = reader;
_grid.DataBind();
}
finally
{
conn.Dispose();
}
*****************************

Thanks,
KevinI think your only option on a DataReader is to use the .Read property. It
returns false if it can't produce a record.

Otherwise, a dataset is necessary to get the actual record count.

--
Jerry Boone
Analytical Technologies, Inc.
http://www.antech.biz

"Kevin" <kevin@.spammers_die.com> wrote in message
news:OFV8NLZxDHA.2304@.TK2MSFTNGP12.phx.gbl...
> I am using the DataReader to populate an ASP table dynamically. I would
> like to set the tables "visible" property based on the the data from my
> query. For example, if I have at least one row of data, I will set
visible
> to true, otherwise, false.
> How get I find out if I have at least one row of data? The code below is
> from the CodeBehind of my asp form.
> *****************************
> OracleConnection conn = new OracleConnection(ConnectionString);
> OracleCommand cmd = new OracleCommand("select * from customer where name =
> 'BOB', conn);
> try
> {
> conn.Open();
> IDataReader reader = cmd.ExecuteReader();
> _grid.DataSource = reader;
> _grid.DataBind();
> }
> finally
> {
> conn.Dispose();
> }
> *****************************
> Thanks,
> Kevin
Thanks Jerry, that was exactly what I needed.

Kevin

"Jerry Boone" <jerry@.antech.biz.killspam> wrote in message
news:mkmEb.1432$C21.627@.newssvr22.news.prodigy.com ...
> I think your only option on a DataReader is to use the .Read property. It
> returns false if it can't produce a record.
> Otherwise, a dataset is necessary to get the actual record count.
> --
> Jerry Boone
> Analytical Technologies, Inc.
> http://www.antech.biz
>
> "Kevin" <kevin@.spammers_die.com> wrote in message
> news:OFV8NLZxDHA.2304@.TK2MSFTNGP12.phx.gbl...
> > I am using the DataReader to populate an ASP table dynamically. I would
> > like to set the tables "visible" property based on the the data from my
> > query. For example, if I have at least one row of data, I will set
> visible
> > to true, otherwise, false.
> > How get I find out if I have at least one row of data? The code below
is
> > from the CodeBehind of my asp form.
> > *****************************
> > OracleConnection conn = new OracleConnection(ConnectionString);
> > OracleCommand cmd = new OracleCommand("select * from customer where name
=
> > 'BOB', conn);
> > try
> > {
> > conn.Open();
> > IDataReader reader = cmd.ExecuteReader();
> > _grid.DataSource = reader;
> > _grid.DataBind();
> > }
> > finally
> > {
> > conn.Dispose();
> > }
> > *****************************
> > Thanks,
> > Kevin

Saturday, March 24, 2012

trouble with DataReader

Hi group,

I'm trying to use a DataReader but I get an error.

This is my code:

private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection oCon = new SqlConnection("server=server;uid=id;" +
"pwd=pwd;database=database");
SqlCommand oCmd = new SqlCommand("SELECT nieuwsid, datum, titel FROM news
WHERE datum <= { fn CURDATE() })ORDER BY datum DESC",oCon);
oCon.Open();
oCmd.Connection=oCon;
SqlDataReader dr = oCmd.ExecuteReader();
oCmd.Dispose ();
oCon.Close();
oCon.Dispose();
}

I have an error at runtime on the line SqlDataReader dr =
oCmd.ExecuteReader();
The error is: Incorrect syntax near ')'.

With I simply delete the line with the error then I don't get an error, so I
think the error is in the line SqlDataReader dr = oCmd.ExecuteReader();

Can someone say what I do wrong ?

grz

Bernie V

--
http://www.djberniev.beI think you query syntax is off. Try running it in query analyzer and fix
it.

"Bernie V" <bernd.vantyghem@.pandoradotbe> wrote in message
news:uXVuK8K8DHA.2924@.tk2msftngp13.phx.gbl...
> Hi group,
> I'm trying to use a DataReader but I get an error.
> This is my code:
> private void Page_Load(object sender, System.EventArgs e)
> {
> SqlConnection oCon = new SqlConnection("server=server;uid=id;" +
> "pwd=pwd;database=database");
> SqlCommand oCmd = new SqlCommand("SELECT nieuwsid, datum, titel FROM news
> WHERE datum <= { fn CURDATE() })ORDER BY datum DESC",oCon);
> oCon.Open();
> oCmd.Connection=oCon;
> SqlDataReader dr = oCmd.ExecuteReader();
> oCmd.Dispose ();
> oCon.Close();
> oCon.Dispose();
> }
> I have an error at runtime on the line SqlDataReader dr =
> oCmd.ExecuteReader();
> The error is: Incorrect syntax near ')'.
> With I simply delete the line with the error then I don't get an error, so
I
> think the error is in the line SqlDataReader dr = oCmd.ExecuteReader();
> Can someone say what I do wrong ?
> grz
> Bernie V
>
> --
> http://www.djberniev.be
"Marina" <someone@.nospam.com> schreef in bericht
news:umi3Y$K8DHA.2064@.TK2MSFTNGP11.phx.gbl...
> I think you query syntax is off. Try running it in query analyzer and fix
> it.

Hi Marina,

This was also my first opinion. But I changed the query to a simple select
query and I had the same problem.
The query like I use works in the query analyser.

grz

Bernie V

--
http://www.djberniev.be
The error message indicates a syntax problem with the query. I would suggest
either putting a breakpoint in, or recording the generated query somewhere
so that you can copy the generated query string and run it in Query Analyzer
to find out the problem with it.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Bernie V" <bernd.vantyghem@.pandoradotbe> wrote in message
news:#YfjfNL8DHA.1504@.TK2MSFTNGP12.phx.gbl...
> "Marina" <someone@.nospam.com> schreef in bericht
> news:umi3Y$K8DHA.2064@.TK2MSFTNGP11.phx.gbl...
> > I think you query syntax is off. Try running it in query analyzer and
fix
> > it.
> Hi Marina,
> This was also my first opinion. But I changed the query to a simple select
> query and I had the same problem.
> The query like I use works in the query analyser.
> grz
> Bernie V
> --
> http://www.djberniev.be
"Marina" <someone@.nospam.com> schreef in bericht
news:umi3Y$K8DHA.2064@.TK2MSFTNGP11.phx.gbl...
> I think you query syntax is off. Try running it in query analyzer and fix
> it.

Marina,

I double cheked and you 're right !
There where to many ')' .

Thx !

grz

Bernie V
Looks like you dont have a corresponding '('

Bernie V wrote:

> Hi group,
> I'm trying to use a DataReader but I get an error.
> This is my code:
> private void Page_Load(object sender, System.EventArgs e)
> {
> SqlConnection oCon = new SqlConnection("server=server;uid=id;" +
> "pwd=pwd;database=database");
> SqlCommand oCmd = new SqlCommand("SELECT nieuwsid, datum, titel FROM news
> WHERE datum <= { fn CURDATE() })ORDER BY datum DESC",oCon);
> oCon.Open();
> oCmd.Connection=oCon;
> SqlDataReader dr = oCmd.ExecuteReader();
> oCmd.Dispose ();
> oCon.Close();
> oCon.Dispose();
> }
> I have an error at runtime on the line SqlDataReader dr =
> oCmd.ExecuteReader();
> The error is: Incorrect syntax near ')'.
> With I simply delete the line with the error then I don't get an error, so I
> think the error is in the line SqlDataReader dr = oCmd.ExecuteReader();
> Can someone say what I do wrong ?
> grz
> Bernie V

--
Regards,
Dilip Krishnan
dilipkrish at msn. com