Wrox Home  
Search P2P Archive for: Go

  Return to Index  

ado_dotnet thread: SqlDataReader


Message #1 by =?iso-8859-1?Q?Fausto_Lu=EDs?= <fausto.luis@a...> on Mon, 28 Jan 2002 13:08:46 -0000
Greetings.



I need to bind the results of a datareader to a repeater control; how 

can I

detect if the datareader returned any records ?. If I use 'If dr.read 

...',

and there are records returned, the record pointer advances to the next

record, and the first record is never displayed; how can I simulate a

'movefirst' method ?



  Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

        Dim dr As SqlClient.SqlDataReader =3D comm.ExecuteReader()

        Dim schemaTable As DataTable =3D dr.GetSchemaTable()

        '      If not dr.read Then

        '       	eof =3D True

        '      End If





        Repeater1.DataSource =3D dr

        If eof Then

            Result.Text =3D "No records found" ' ASP:Label

            Repeater1.Visible =3D False

        Else

            Result.Text =3D ""

            Repeater1.DataBind()

        End If

        dr.Close()

        cnn.Close()





Thanks in advance.



Fausto Lu=EDs

Message #2 by "Douglas Rohm" <dlr@m...> on Mon, 28 Jan 2002 09:59:15 -0500
You are correct in that you should use 'if (dr.Read())', but you are

incorrect in that it will skip the first record.  By default, when a

DataReader is returned it is pointing before the first record.  So when

you call dr.Read() it then points to the first record, not the second.



Doug



-----Original Message-----

From: Fausto Lu=EDs [mailto:fausto.luis@a...]

Sent: Monday, January 28, 2002 8:09 AM

To: ADO.NET

Subject: [ado_dotnet] SqlDataReader





Greetings.



I need to bind the results of a datareader to a repeater control; how

can I detect if the datareader returned any records ?. If I use 'If

dr.read ...', and there are records returned, the record pointer

advances to the next record, and the first record is never displayed;

how can I simulate a 'movefirst' method ?



  Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

        Dim dr As SqlClient.SqlDataReader =3D comm.ExecuteReader()

        Dim schemaTable As DataTable =3D dr.GetSchemaTable()

        '      If not dr.read Then

        '       	eof =3D True

        '      End If





        Repeater1.DataSource =3D dr

        If eof Then

            Result.Text =3D "No records found" ' ASP:Label

            Repeater1.Visible =3D False

        Else

            Result.Text =3D ""

            Repeater1.DataBind()

        End If

        dr.Close()

        cnn.Close()





Thanks in advance.



Fausto Lu=EDs






$subst('Email.Unsub').



Message #3 by =?iso-8859-1?Q?Fausto_Lu=EDs?= <fausto.luis@a...> on Mon, 28 Jan 2002 15:01:58 -0000
The problem is that, when I have 1 record returned, and I don't apply 

the

test referred, the repeater display the data for that record, 

otherwise, it

doesn't; that is why I inferred that the test made, moved the record

pointer.



Thanks the same.



FL



	-----Original Message-----

	From:	Douglas Rohm [SMTP:dlr@m...]

	Sent:	segunda-feira, 28 de Janeiro de 2002 14:59

	To:	ADO.NET

	Subject:	[ado_dotnet] RE: SqlDataReader



	You are correct in that you should use 'if (dr.Read())', but you are

	incorrect in that it will skip the first record.  By default, when a

	DataReader is returned it is pointing before the first record.  So

when

	you call dr.Read() it then points to the first record, not the

second.



	Doug



	-----Original Message-----

	From: Fausto Lu=EDs [mailto:fausto.luis@a...]

	Sent: Monday, January 28, 2002 8:09 AM

	To: ADO.NET

	Subject: [ado_dotnet] SqlDataReader





	Greetings.



	I need to bind the results of a datareader to a repeater control;

how

	can I detect if the datareader returned any records ?. If I use 'If

	dr.read ...', and there are records returned, the record pointer

	advances to the next record, and the first record is never

displayed;

	how can I simulate a 'movefirst' method ?



	  Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

	        Dim dr As SqlClient.SqlDataReader =3D comm.ExecuteReader()

	        Dim schemaTable As DataTable =3D dr.GetSchemaTable()

	        '      If not dr.read Then

	        '       	eof =3D True

	        '      End If





	        Repeater1.DataSource =3D dr

	        If eof Then

	            Result.Text =3D "No records found" ' ASP:Label

	            Repeater1.Visible =3D False

	        Else

	            Result.Text =3D ""

	            Repeater1.DataBind()

	        End If

	        dr.Close()

	        cnn.Close()





	Thanks in advance.



	Fausto Lu=EDs






	$subst('Email.Unsub').








$subst('Email.Unsub').

Message #4 by "Douglas Rohm" <dlr@m...> on Mon, 28 Jan 2002 11:30:42 -0500
The DataReader is a forward-only stream and I can only guess, because of

the way you are doing your checking that since you call dr.Read() to

determine if any records were returned and then call DataBind() it's

picking up after the first Read().  I put together a sample here and

confirmed it.  Try using this instead:



------------------------------------------------------------------------

-------

Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

Dim dr As SqlClient.SqlDataReader =3D comm.ExecuteReader()

Dim schemaTable As DataTable =3D dr.GetSchemaTable()

Repeater1.DataSource =3D dr



If dr.FieldCount > 0 Then

  Result.Text =3D ""

  Repeater1.DataBind()

Else

  Result.Text =3D "No records found"

  Repeater1.Visible =3D False

End If



dr.Close()

cnn.Close()

------------------------------------------------------------------------

-------

The code I tested this with was in c#:



string strSQL =3D "SELECT * FROM authors WHERE au_id=3D'172-32-1176'";

sqlCommand1.CommandText =3D strSQL;

sqlCommand1.Connection =3D sqlConnection1;

sqlConnection1.Open();

SqlDataReader dr =3D sqlCommand1.ExecuteReader();

Repeater1.DataSource =3D dr;

if (dr.FieldCount > 0)

{

	Repeater1.DataBind();

}

dr.Close();

sqlConnection1.Close();

------------------------------------------------------------------------

-------



Hope this helps,



Doug



-----Original Message-----

From: Fausto Lu=EDs [mailto:fausto.luis@a...]

Sent: Monday, January 28, 2002 10:02 AM

To: ADO.NET

Subject: [ado_dotnet] RE: SqlDataReader





The problem is that, when I have 1 record returned, and I don't apply

the test referred, the repeater display the data for that record,

otherwise, it doesn't; that is why I inferred that the test made, moved

the record pointer.



Thanks the same.



FL



	-----Original Message-----

	From:	Douglas Rohm [SMTP:dlr@m...]

	Sent:	segunda-feira, 28 de Janeiro de 2002 14:59

	To:	ADO.NET

	Subject:	[ado_dotnet] RE: SqlDataReader



	You are correct in that you should use 'if (dr.Read())', but you

are

	incorrect in that it will skip the first record.  By default,

when a

	DataReader is returned it is pointing before the first record.

So when

	you call dr.Read() it then points to the first record, not the

second.



	Doug



	-----Original Message-----

	From: Fausto Lu=EDs [mailto:fausto.luis@a...]

	Sent: Monday, January 28, 2002 8:09 AM

	To: ADO.NET

	Subject: [ado_dotnet] SqlDataReader





	Greetings.



	I need to bind the results of a datareader to a repeater

control; how

	can I detect if the datareader returned any records ?. If I use

'If

	dr.read ...', and there are records returned, the record pointer

	advances to the next record, and the first record is never

displayed;

	how can I simulate a 'movefirst' method ?



	  Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

	        Dim dr As SqlClient.SqlDataReader =3D comm.ExecuteReader()

	        Dim schemaTable As DataTable =3D dr.GetSchemaTable()

	        '      If not dr.read Then

	        '       	eof =3D True

	        '      End If





	        Repeater1.DataSource =3D dr

	        If eof Then

	            Result.Text =3D "No records found" ' ASP:Label

	            Repeater1.Visible =3D False

	        Else

	            Result.Text =3D ""

	            Repeater1.DataBind()

	        End If

	        dr.Close()

	        cnn.Close()





	Thanks in advance.



	Fausto Lu=EDs






	$subst('Email.Unsub').








$subst('Email.Unsub').






$subst('Email.Unsub').



Message #5 by =?iso-8859-1?Q?Fausto_Lu=EDs?= <fausto.luis@a...> on Mon, 28 Jan 2002 17:01:56 -0000
It doesn't work either ...



Well, it's my first .Net project anyway; so if you think I'm giving you 

too

much work, please forgive me; I think I'm gone try using the dataset 

...



Now for the last one :-) ...







Dim comm2 As New SqlClient.SqlCommand("sp_queryMedicall", cnn)



 With comm2

.CommandType =3D CommandType.StoredProcedure

.Parameters.Add("@Local", SqlDbType.VarChar, 255).Value =3D 

parmDPostal

.Parameters.Add("@Especialidade", SqlDbType.VarChar, 255).Value =3D 

parmEspec

 End With

 comm2.ExecuteReader()





When I run the preceding code, I  receive the following error :



Procedure 'sp_QueryMedicall' expects parameter '@L...', which was not

supplied.





CREATE PROCEDURE sp_QueryMedicall(@Local as varchar(255), 

@Especialidade as

varchar(255))



as

if @Local is null

begin

	select * from Directorio where Especialidade =3D @Especialidade order

by nome

end

else

if @Especialidade is null

begin

	select * from Directorio where DPostal =3D @Local order by Cpostal,

Nome

end

else

begin

	select * from Directorio where DPostal =3D @Local and Especialidade 

=3D

@Especialidade order by nome

end





Am I doing something wrong ?



Thanks...



Fausto



	-----Original Message-----

	From:	Douglas Rohm [SMTP:dlr@m...]

	Sent:	segunda-feira, 28 de Janeiro de 2002 16:31

	To:	ADO.NET

	Subject:	[ado_dotnet] RE: SqlDataReader



	The DataReader is a forward-only stream and I can only guess,

because of

	the way you are doing your checking that since you call dr.Read() to

	determine if any records were returned and then call DataBind() it's

	picking up after the first Read().  I put together a sample here and

	confirmed it.  Try using this instead:



=09

------------------------------------------------------------------------



	-------

	Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

	Dim dr As SqlClient.SqlDataReader =3D comm.ExecuteReader()

	Dim schemaTable As DataTable =3D dr.GetSchemaTable()

	Repeater1.DataSource =3D dr



	If dr.FieldCount > 0 Then

	  Result.Text =3D ""

	  Repeater1.DataBind()

	Else

	  Result.Text =3D "No records found"

	  Repeater1.Visible =3D False

	End If



	dr.Close()

	cnn.Close()

=09

------------------------------------------------------------------------



	-------

	The code I tested this with was in c#:



	string strSQL =3D "SELECT * FROM authors WHERE au_id=3D'172-32-1176'";

	sqlCommand1.CommandText =3D strSQL;

	sqlCommand1.Connection =3D sqlConnection1;

	sqlConnection1.Open();

	SqlDataReader dr =3D sqlCommand1.ExecuteReader();

	Repeater1.DataSource =3D dr;

	if (dr.FieldCount > 0)

	{

		Repeater1.DataBind();

	}

	dr.Close();

	sqlConnection1.Close();

=09

------------------------------------------------------------------------



	-------



	Hope this helps,



	Doug



	-----Original Message-----

	From: Fausto Lu=EDs [mailto:fausto.luis@a...]

	Sent: Monday, January 28, 2002 10:02 AM

	To: ADO.NET

	Subject: [ado_dotnet] RE: SqlDataReader





	The problem is that, when I have 1 record returned, and I don't

apply

	the test referred, the repeater display the data for that record,

	otherwise, it doesn't; that is why I inferred that the test made,

moved

	the record pointer.



	Thanks the same.



	FL



		-----Original Message-----

		From:	Douglas Rohm [SMTP:dlr@m...]

		Sent:	segunda-feira, 28 de Janeiro de 2002 14:59

		To:	ADO.NET

		Subject:	[ado_dotnet] RE: SqlDataReader



		You are correct in that you should use 'if (dr.Read())', but

you

	are

		incorrect in that it will skip the first record.  By

default,

	when a

		DataReader is returned it is pointing before the first

record.

	So when

		you call dr.Read() it then points to the first record, not

the

	second.



		Doug



		-----Original Message-----

		From: Fausto Lu=EDs [mailto:fausto.luis@a...]

		Sent: Monday, January 28, 2002 8:09 AM

		To: ADO.NET

		Subject: [ado_dotnet] SqlDataReader





		Greetings.



		I need to bind the results of a datareader to a repeater

	control; how

		can I detect if the datareader returned any records ?. If I

use

	'If

		dr.read ...', and there are records returned, the record

pointer

		advances to the next record, and the first record is never

	displayed;

		how can I simulate a 'movefirst' method ?



		  Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

		        Dim dr As SqlClient.SqlDataReader =3D

comm.ExecuteReader()

		        Dim schemaTable As DataTable =3D dr.GetSchemaTable()

		        '      If not dr.read Then

		        '       	eof =3D True

		        '      End If





		        Repeater1.DataSource =3D dr

		        If eof Then

		            Result.Text =3D "No records found" ' ASP:Label

		            Repeater1.Visible =3D False

		        Else

		            Result.Text =3D ""

		            Repeater1.DataBind()

		        End If

		        dr.Close()

		        cnn.Close()





		Thanks in advance.



		Fausto Lu=EDs



		---

		Change your mail options at http://p2p.wrox.com/manager.asp

or


		$subst('Email.Unsub').





		---

		Change your mail options at http://p2p.wrox.com/manager.asp

or


	$subst('Email.Unsub').






	$subst('Email.Unsub').








$subst('Email.Unsub').

Message #6 by "Douglas Rohm" <dlr@m...> on Mon, 28 Jan 2002 12:21:27 -0500
Can you show me your SQL statement?  The testing I did here locally

returned 1 record and displayed it correctly.  Maybe your SQL isn't

returning any records?



Doug



-----Original Message-----

From: Fausto Lu=EDs [mailto:fausto.luis@a...]

Sent: Monday, January 28, 2002 12:02 PM

To: ADO.NET

Subject: [ado_dotnet] RE: SqlDataReader





It doesn't work either ...



Well, it's my first .Net project anyway; so if you think I'm giving you

too much work, please forgive me; I think I'm gone try using the dataset

...



Now for the last one :-) ...







Dim comm2 As New SqlClient.SqlCommand("sp_queryMedicall", cnn)



 With comm2

.CommandType =3D CommandType.StoredProcedure .Parameters.Add("@Local",

SqlDbType.VarChar, 255).Value =3D parmDPostal

.Parameters.Add("@Especialidade", SqlDbType.VarChar, 255).Value =3D

parmEspec  End With

 comm2.ExecuteReader()





When I run the preceding code, I  receive the following error :



Procedure 'sp_QueryMedicall' expects parameter '@L...', which was not

supplied.





CREATE PROCEDURE sp_QueryMedicall(@Local as varchar(255), @Especialidade

as

varchar(255))



as

if @Local is null

begin

	select * from Directorio where Especialidade =3D @Especialidade

order by nome end else if @Especialidade is null begin

	select * from Directorio where DPostal =3D @Local order by

Cpostal, Nome end else begin

	select * from Directorio where DPostal =3D @Local and

Especialidade =3D @Especialidade order by nome end





Am I doing something wrong ?



Thanks...



Fausto



	-----Original Message-----

	From:	Douglas Rohm [SMTP:dlr@m...]

	Sent:	segunda-feira, 28 de Janeiro de 2002 16:31

	To:	ADO.NET

	Subject:	[ado_dotnet] RE: SqlDataReader



	The DataReader is a forward-only stream and I can only guess,

because of

	the way you are doing your checking that since you call

dr.Read() to

	determine if any records were returned and then call DataBind()

it's

	picking up after the first Read().  I put together a sample here

and

	confirmed it.  Try using this instead:



=09

------------------------------------------------------------------------

	-------

	Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

	Dim dr As SqlClient.SqlDataReader =3D comm.ExecuteReader()

	Dim schemaTable As DataTable =3D dr.GetSchemaTable()

	Repeater1.DataSource =3D dr



	If dr.FieldCount > 0 Then

	  Result.Text =3D ""

	  Repeater1.DataBind()

	Else

	  Result.Text =3D "No records found"

	  Repeater1.Visible =3D False

	End If



	dr.Close()

	cnn.Close()

=09

------------------------------------------------------------------------

	-------

	The code I tested this with was in c#:



	string strSQL =3D "SELECT * FROM authors WHERE

au_id=3D'172-32-1176'";

	sqlCommand1.CommandText =3D strSQL;

	sqlCommand1.Connection =3D sqlConnection1;

	sqlConnection1.Open();

	SqlDataReader dr =3D sqlCommand1.ExecuteReader();

	Repeater1.DataSource =3D dr;

	if (dr.FieldCount > 0)

	{

		Repeater1.DataBind();

	}

	dr.Close();

	sqlConnection1.Close();

=09

------------------------------------------------------------------------

	-------



	Hope this helps,



	Doug



	-----Original Message-----

	From: Fausto Lu=EDs [mailto:fausto.luis@a...]

	Sent: Monday, January 28, 2002 10:02 AM

	To: ADO.NET

	Subject: [ado_dotnet] RE: SqlDataReader





	The problem is that, when I have 1 record returned, and I don't

apply

	the test referred, the repeater display the data for that

record,

	otherwise, it doesn't; that is why I inferred that the test

made, moved

	the record pointer.



	Thanks the same.



	FL



		-----Original Message-----

		From:	Douglas Rohm [SMTP:dlr@m...]

		Sent:	segunda-feira, 28 de Janeiro de 2002 14:59

		To:	ADO.NET

		Subject:	[ado_dotnet] RE: SqlDataReader



		You are correct in that you should use 'if (dr.Read())',

but you

	are

		incorrect in that it will skip the first record.  By

default,

	when a

		DataReader is returned it is pointing before the first

record.

	So when

		you call dr.Read() it then points to the first record,

not the

	second.



		Doug



		-----Original Message-----

		From: Fausto Lu=EDs [mailto:fausto.luis@a...]

		Sent: Monday, January 28, 2002 8:09 AM

		To: ADO.NET

		Subject: [ado_dotnet] SqlDataReader





		Greetings.



		I need to bind the results of a datareader to a repeater

	control; how

		can I detect if the datareader returned any records ?.

If I use

	'If

		dr.read ...', and there are records returned, the record

pointer

		advances to the next record, and the first record is

never

	displayed;

		how can I simulate a 'movefirst' method ?



		  Dim comm As New SqlClient.SqlCommand(selectOutput,

cnn)

		        Dim dr As SqlClient.SqlDataReader =3D

comm.ExecuteReader()

		        Dim schemaTable As DataTable =3D

dr.GetSchemaTable()

		        '      If not dr.read Then

		        '       	eof =3D True

		        '      End If





		        Repeater1.DataSource =3D dr

		        If eof Then

		            Result.Text =3D "No records found" ' ASP:Label

		            Repeater1.Visible =3D False

		        Else

		            Result.Text =3D ""

		            Repeater1.DataBind()

		        End If

		        dr.Close()

		        cnn.Close()





		Thanks in advance.



		Fausto Lu=EDs







		$subst('Email.Unsub').









	$subst('Email.Unsub').






	$subst('Email.Unsub').








$subst('Email.Unsub').






$subst('Email.Unsub').



Message #7 by =?iso-8859-1?Q?Fausto_Lu=EDs?= <fausto.luis@a...> on Mon, 28 Jan 2002 18:25:49 -0000
Well, you must have a lot of experience; since after reading your lines

immediately provide me the answer...



The parameters that I was passing to the stored procedure, come from 

two

dropdownlist that, initially, have no values selected; it was just a 

matter

of testing those values, and it worked like a charm ... now the app 

performs

the way I wanted to ...



You were most kind in supplying me all that help



Please forgive me, if my 'english' was not the better :-) ...



Bye.

Fausto.









	-----Original Message-----

	From:	Douglas Rohm [SMTP:dlr@m...]

	Sent:	segunda-feira, 28 de Janeiro de 2002 17:21

	To:	ADO.NET

	Subject:	[ado_dotnet] RE: SqlDataReader



	Can you show me your SQL statement?  The testing I did here locally

	returned 1 record and displayed it correctly.  Maybe your SQL isn't

	returning any records?



	Doug



	-----Original Message-----

	From: Fausto Lu=EDs [mailto:fausto.luis@a...]

	Sent: Monday, January 28, 2002 12:02 PM

	To: ADO.NET

	Subject: [ado_dotnet] RE: SqlDataReader





	It doesn't work either ...



	Well, it's my first .Net project anyway; so if you think I'm giving

you

	too much work, please forgive me; I think I'm gone try using the

dataset

	...



	Now for the last one :-) ...







	Dim comm2 As New SqlClient.SqlCommand("sp_queryMedicall", cnn)



	 With comm2

	.CommandType =3D CommandType.StoredProcedure .Parameters.Add("@Local",

	SqlDbType.VarChar, 255).Value =3D parmDPostal

	.Parameters.Add("@Especialidade", SqlDbType.VarChar, 255).Value =3D

	parmEspec  End With

	 comm2.ExecuteReader()





	When I run the preceding code, I  receive the following error :



	Procedure 'sp_QueryMedicall' expects parameter '@L...', which was

not

	supplied.





	CREATE PROCEDURE sp_QueryMedicall(@Local as varchar(255),

@Especialidade

	as

	varchar(255))



	as

	if @Local is null

	begin

		select * from Directorio where Especialidade =3D

@Especialidade

	order by nome end else if @Especialidade is null begin

		select * from Directorio where DPostal =3D @Local order by

	Cpostal, Nome end else begin

		select * from Directorio where DPostal =3D @Local and

	Especialidade =3D @Especialidade order by nome end





	Am I doing something wrong ?



	Thanks...



	Fausto



		-----Original Message-----

		From:	Douglas Rohm [SMTP:dlr@m...]

		Sent:	segunda-feira, 28 de Janeiro de 2002 16:31

		To:	ADO.NET

		Subject:	[ado_dotnet] RE: SqlDataReader



		The DataReader is a forward-only stream and I can only

guess,

	because of

		the way you are doing your checking that since you call

	dr.Read() to

		determine if any records were returned and then call

DataBind()

	it's

		picking up after the first Read().  I put together a sample

here

	and

		confirmed it.  Try using this instead:



	=09

=09

------------------------------------------------------------------------



		-------

		Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

		Dim dr As SqlClient.SqlDataReader =3D comm.ExecuteReader()

		Dim schemaTable As DataTable =3D dr.GetSchemaTable()

		Repeater1.DataSource =3D dr



		If dr.FieldCount > 0 Then

		  Result.Text =3D ""

		  Repeater1.DataBind()

		Else

		  Result.Text =3D "No records found"

		  Repeater1.Visible =3D False

		End If



		dr.Close()

		cnn.Close()

	=09

=09

------------------------------------------------------------------------



		-------

		The code I tested this with was in c#:



		string strSQL =3D "SELECT * FROM authors WHERE

	au_id=3D'172-32-1176'";

		sqlCommand1.CommandText =3D strSQL;

		sqlCommand1.Connection =3D sqlConnection1;

		sqlConnection1.Open();

		SqlDataReader dr =3D sqlCommand1.ExecuteReader();

		Repeater1.DataSource =3D dr;

		if (dr.FieldCount > 0)

		{

			Repeater1.DataBind();

		}

		dr.Close();

		sqlConnection1.Close();

	=09

=09

------------------------------------------------------------------------



		-------



		Hope this helps,



		Doug



		-----Original Message-----

		From: Fausto Lu=EDs [mailto:fausto.luis@a...]

		Sent: Monday, January 28, 2002 10:02 AM

		To: ADO.NET

		Subject: [ado_dotnet] RE: SqlDataReader





		The problem is that, when I have 1 record returned, and I

don't

	apply

		the test referred, the repeater display the data for that

	record,

		otherwise, it doesn't; that is why I inferred that the test

	made, moved

		the record pointer.



		Thanks the same.



		FL



			-----Original Message-----

			From:	Douglas Rohm [SMTP:dlr@m...]

			Sent:	segunda-feira, 28 de Janeiro de 2002 14:59

			To:	ADO.NET

			Subject:	[ado_dotnet] RE: SqlDataReader



			You are correct in that you should use 'if

(dr.Read())',

	but you

		are

			incorrect in that it will skip the first record.  By

	default,

		when a

			DataReader is returned it is pointing before the

first

	record.

		So when

			you call dr.Read() it then points to the first

record,

	not the

		second.



			Doug



			-----Original Message-----

			From: Fausto Lu=EDs [mailto:fausto.luis@a...]

			Sent: Monday, January 28, 2002 8:09 AM

			To: ADO.NET

			Subject: [ado_dotnet] SqlDataReader





			Greetings.



			I need to bind the results of a datareader to a

repeater

		control; how

			can I detect if the datareader returned any records

?.

	If I use

		'If

			dr.read ...', and there are records returned, the

record

	pointer

			advances to the next record, and the first record is

	never

		displayed;

			how can I simulate a 'movefirst' method ?



			  Dim comm As New SqlClient.SqlCommand(selectOutput,

	cnn)

			        Dim dr As SqlClient.SqlDataReader =3D

	comm.ExecuteReader()

			        Dim schemaTable As DataTable =3D

	dr.GetSchemaTable()

			        '      If not dr.read Then

			        '       	eof =3D True

			        '      End If





			        Repeater1.DataSource =3D dr

			        If eof Then

			            Result.Text =3D "No records found" '

ASP:Label

			            Repeater1.Visible =3D False

			        Else

			            Result.Text =3D ""

			            Repeater1.DataBind()

			        End If

			        dr.Close()

			        cnn.Close()





			Thanks in advance.



			Fausto Lu=EDs







			$subst('Email.Unsub').









		$subst('Email.Unsub').



		---

		Change your mail options at http://p2p.wrox.com/manager.asp

or


		$subst('Email.Unsub').





		---

		Change your mail options at http://p2p.wrox.com/manager.asp

or


	$subst('Email.Unsub').






	$subst('Email.Unsub').








$subst('Email.Unsub').

Message #8 by =?iso-8859-1?Q?Fausto_Lu=EDs?= <fausto.luis@a...> on Tue, 29 Jan 2002 12:52:09 -0000
Hello again.



I've found the solution for the datareader / repeater problem (it was a 

long

night...). All I have to do is check for the number of items in the

repeater, after binding the datareader.



Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

Dim dr As SqlClient.SqlDataReader =3D comm.ExecuteReader()

Dim schemaTable As DataTable =3D dr.GetSchemaTable()

Repeater1.DataSource =3D dr

Repeater1.DataBind()

If Repeater.Items.Count =3D 0 then

Result.Text =3D "No records found"

Repeater1.Visible =3D False

 Else

Result.Text =3D ""

End If



dr.Close()

cnn.Close()



Please tell me it that helped someone else.(just to feed me ego...)



Once again, thanks for your help.



Fausto





	-----Original Message-----

	From:	Douglas Rohm [SMTP:dlr@m...]

	Sent:	segunda-feira, 28 de Janeiro de 2002 16:31

	To:	ADO.NET

	Subject:	[ado_dotnet] RE: SqlDataReader



	The DataReader is a forward-only stream and I can only guess,

because of

	the way you are doing your checking that since you call dr.Read() to

	determine if any records were returned and then call DataBind() it's

	picking up after the first Read().  I put together a sample here and

	confirmed it.  Try using this instead:



=09

------------------------------------------------------------------------



	-------

	Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

	Dim dr As SqlClient.SqlDataReader =3D comm.ExecuteReader()

	Dim schemaTable As DataTable =3D dr.GetSchemaTable()

	Repeater1.DataSource =3D dr



	If dr.FieldCount > 0 Then

	  Result.Text =3D ""

	  Repeater1.DataBind()

	Else

	  Result.Text =3D "No records found"

	  Repeater1.Visible =3D False

	End If



	dr.Close()

	cnn.Close()

=09

------------------------------------------------------------------------



	-------

	The code I tested this with was in c#:



	string strSQL =3D "SELECT * FROM authors WHERE au_id=3D'172-32-1176'";

	sqlCommand1.CommandText =3D strSQL;

	sqlCommand1.Connection =3D sqlConnection1;

	sqlConnection1.Open();

	SqlDataReader dr =3D sqlCommand1.ExecuteReader();

	Repeater1.DataSource =3D dr;

	if (dr.FieldCount > 0)

	{

		Repeater1.DataBind();

	}

	dr.Close();

	sqlConnection1.Close();

=09

------------------------------------------------------------------------



	-------



	Hope this helps,



	Doug



	-----Original Message-----

	From: Fausto Lu=EDs [mailto:fausto.luis@a...]

	Sent: Monday, January 28, 2002 10:02 AM

	To: ADO.NET

	Subject: [ado_dotnet] RE: SqlDataReader





	The problem is that, when I have 1 record returned, and I don't

apply

	the test referred, the repeater display the data for that record,

	otherwise, it doesn't; that is why I inferred that the test made,

moved

	the record pointer.



	Thanks the same.



	FL



		-----Original Message-----

		From:	Douglas Rohm [SMTP:dlr@m...]

		Sent:	segunda-feira, 28 de Janeiro de 2002 14:59

		To:	ADO.NET

		Subject:	[ado_dotnet] RE: SqlDataReader



		You are correct in that you should use 'if (dr.Read())', but

you

	are

		incorrect in that it will skip the first record.  By

default,

	when a

		DataReader is returned it is pointing before the first

record.

	So when

		you call dr.Read() it then points to the first record, not

the

	second.



		Doug



		-----Original Message-----

		From: Fausto Lu=EDs [mailto:fausto.luis@a...]

		Sent: Monday, January 28, 2002 8:09 AM

		To: ADO.NET

		Subject: [ado_dotnet] SqlDataReader





		Greetings.



		I need to bind the results of a datareader to a repeater

	control; how

		can I detect if the datareader returned any records ?. If I

use

	'If

		dr.read ...', and there are records returned, the record

pointer

		advances to the next record, and the first record is never

	displayed;

		how can I simulate a 'movefirst' method ?



		  Dim comm As New SqlClient.SqlCommand(selectOutput, cnn)

		        Dim dr As SqlClient.SqlDataReader =3D

comm.ExecuteReader()

		        Dim schemaTable As DataTable =3D dr.GetSchemaTable()

		        '      If not dr.read Then

		        '       	eof =3D True

		        '      End If





		        Repeater1.DataSource =3D dr

		        If eof Then

		            Result.Text =3D "No records found" ' ASP:Label

		            Repeater1.Visible =3D False

		        Else

		            Result.Text =3D ""

		            Repeater1.DataBind()

		        End If

		        dr.Close()

		        cnn.Close()





		Thanks in advance.



		Fausto Lu=EDs



		---

		Change your mail options at http://p2p.wrox.com/manager.asp

or


		$subst('Email.Unsub').





		---

		Change your mail options at http://p2p.wrox.com/manager.asp

or


	$subst('Email.Unsub').






	$subst('Email.Unsub').








$subst('Email.Unsub').


  Return to Index