im using reader.read() in many times in reading data from database from my web application.
some where i get error as ""Invalid attempt to read when no data is present""
but this error is removed when i use rdr.read() after executing reader in command.
why i get his error. is it better to call reader.read() function as some where im getting data iwthout using rdr.read() function just by rdr=cmd.executereadeR() function.
Mark My Post As An Answer If It Worked !
Please Visit : www.classifiedspak.com
shan000
Member
491 Points
446 Posts
when to use reader.read() and when to not
May 09, 2012 05:52 AM|LINK
Hi ,
im using reader.read() in many times in reading data from database from my web application.
some where i get error as ""Invalid attempt to read when no data is present""
but this error is removed when i use rdr.read() after executing reader in command.
why i get his error. is it better to call reader.read() function as some where im getting data iwthout using rdr.read() function just by rdr=cmd.executereadeR() function.
Please Visit : www.classifiedspak.com
karthicks
All-Star
31382 Points
5424 Posts
Re: when to use reader.read() and when to not
May 09, 2012 06:08 AM|LINK
hi, here is perfect example , before calling dataReader.Read() check using if (dataReader.HasRows)
generally you will get this error when you try read data from datareader before calling dataReader.Read()
SqlConnection connection = new SqlConnection("server=2.1.6.8;Initial Catalog=data;Integrated Security=true;"); SqlCommand command = new SqlCommand("select top 5 * from Countries", connection); connection.Open(); SqlDataReader dataReader = command.ExecuteReader(); if (dataReader.HasRows) { while (dataReader.Read()) { //your code } } connection.Close();Refer :http://www.dotnetperls.com/sqldatareader
http://www.csharp-station.com/Tutorial/AdoDotNet/lesson04
Karthick S
amit.jain
Star
11225 Points
1815 Posts
Re: when to use reader.read() and when to not
May 09, 2012 06:13 AM|LINK
refer http://csharpdotnetfreak.blogspot.com/2012/05/executereader-example-in-aspnet-c-vbnet.html
http://csharpdotnetfreak.blogspot.com/2012/05/executenonquery-executescalar-reader.html
amiT jaiN
ASP.NET C# VB Articles And Code Examples