Live Chat Icon For mobile
Live Chat Icon

How to check EOF with SqlDataReader

Platform: ASP.NET| Category: ADO.NET

If you are using the Framework 1.1 , use HasRows

For Framework < 1.1


Dim myconnection As SqlConnection
Dim mycmd As SqlCommand
Dim strSql As String
Dim myReader As SqlDataReader

myconnection = New SqlConnection('Server=localhost;uid=sa;password=;database=northwind;')
strSql = 'Select count(*) from employees;Select * from employees'
mycmd = New SqlCommand(strSql, myconnection)
Dim count As Integer = CInt(mycmd.ExecuteScalar())
myReader = mycmd.ExecuteReader(CommandBehavior.CloseConnection)
If count = 0 Then
	Response.Write('No records found')
            	While myReader.Read()
                	Response.Write(myReader('Employeeid').ToString() + '<BR>')
            	End While
End If


SqlConnection myconnection ;
SqlCommand mycmd ; 
string strSql ;
SqlDataReader myReader ;

myconnection = new SqlConnection('Server=localhost;uid=sa;password=;database=northwind;');
strSql = 'Select count(*) from employees;Select * from employees';
mycmd = new SqlCommand(strSql, myconnection);
int count=(int) mycmd.ExecuteScalar() ;
myReader = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
if (count==0 )
	Response.Write('No records found');
	myReader.NextResult ();
	while(myReader.Read ())
		Response.Write(myReader['Employeeid'].ToString () + '<br>');

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.