Click here to Skip to main content
15,909,498 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Error:- Invalid attempt to read when no data is present.

my code is:-
C#
string jobcategory = "Select Job_Name from Job_Category where Industry_ID=@b;";
        SqlCommand cmd4 = new SqlCommand(jobcategory, cn);
        cmd4.Parameters.AddWithValue("b", b);
        SqlDataReader reader = cmd4.ExecuteReader();
        Label6.Text = Convert.ToString(reader[0]);//here error this.
        Label8.Text = Convert.ToString(reader[1]);

how to solve this error plz help me friends....
Posted
Updated 12-Jun-12 7:05am
v2
Comments
Abhi KA 12-Jun-12 8:34am    
if data is present ?
RDBurmon 13-Jun-12 9:25am    
Thanks Everyone who replied to this thread , So Member********, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.

Try

C#
SqlDataReader reader = cmd4.ExecuteReader();
if(reader.HasRows)
{
reader.Read();
Label6.Text = Convert.ToString(reader[0]);
Label8.Text = Convert.ToString(reader[1]);
}
 
Share this answer
 
v2
Comments
Member 9027483 12-Jun-12 8:42am    
after trying above changes same error occure....give me another suggestion..
Sharma Richa 13-Jun-12 5:46am    
Please see the another answer provided by me
The way you are using DataReader looks incorrect.

Look at these:
Retrieving Data Using a DataReader (ADO.NET)[^]
DataReader Class[^]
 
Share this answer
 
Comments
Maciej Los 12-Jun-12 13:10pm    
Good links, my 5!
Sandeep Mewara 13-Jun-12 1:39am    
Thanks.
Vani Kulkarni 13-Jun-12 23:38pm    
5!
Sandeep Mewara 15-Jun-12 2:58am    
Thanks.
C#
while (dr.read())
{
    //your logic here
Label6.Text = Convert.ToString(reader[0]);
Label8.Text = Convert.ToString(reader[1]);

}
 
Share this answer
 
Comments
Sharma Richa 12-Jun-12 8:37am    
But if query return multiple records, then it will replace values of Labels again and again.
Abhi KA 12-Jun-12 8:51am    
how many records get?
Hi
Your query is
Select Job_Name from Job_Category where Industry_ID=@b
You are fetching only Job_Name, but when assigning values to Labels You are using
C#
Label8.Text = Convert.ToString(reader[1]);

But your reader has only one value i.e. Job_Name
You can't use reader[1] because it is not present.
 
Share this answer
 
Hi,

Try this:

C#
string jobcategory = "Select Job_Name from Job_Category where Industry_ID=@b;";
SqlCommand cmd4 = new SqlCommand(jobcategory, cn);
cmd4.Parameters.AddWithValue("b", b);
SqlDataReader reader = cmd4.ExecuteReader();
while(reader.Read()){
   Label6.Text = Convert.ToString(reader[0]);
   Label8.Text = Convert.ToString(reader[1]);
}


All the best.
--AK
 
Share this answer
 
v2
Try this:

string jobcategory = "Select Job_Name from Job_Category where Industry_ID=@b;";
SqlCommand cmd4 = new SqlCommand(jobcategory, cn);
SqlDataReader reader = cmd4.ExecuteReader();
while(reader.Read())
{
Label6.Text = reader[0].ToString();
Label8.Text = reader[1].ToString();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900