Click here to Skip to main content
15,913,194 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
Select * from student


Rollno      Studname   Age    DOB            Address   Active    Status
   1          Ajay      6     12.10.2010      Chennai   A          Male
   2          Suresh    5     5.7.2011        Chennai   A          Male
   3          Priya     6     10.6.2010       Chennai   A         FeMale
   4          Revathy   5     8.9.2011        Chennai   A         FeMale



from the above table i am validating as follows

C#
using (SqlConnection newCon = new SqlConnection(db.GetConnectionString))
            {
                SqlCommand newCmd = new SqlCommand(Select * from student)", newCon);
                newCmd.Parameters.Add()
                newCon.Open();
                SqlDataReader rdr = newCmd.ExecuteReader();
                rdr.Read();

                if(reader[6].ToString() == "Male");
                    {
                          //In that i am displaying Male Image
                    }
                 if(reader[6].Tostring() == "FeMale");
                    
                     {
                          //In that i am displaying FeMale Image
                     }
                   
           }



When i exeute the above code shows error as follows

Invalid attempt to read when no data is present.

The above error shows in below line as follows

C#
if(reader[6].ToString() == "Male")


What is the mistake in my above code. please help me

i tried lot but shows error.
Posted
Updated 14-Aug-16 23:08pm
v2
Comments
Patrice T 14-Aug-16 4:13am    
Which word you don't understand in the error message ?
Maciej Los 15-Aug-16 4:52am    
Do not repost!
http://www.codeproject.com/Questions/1118317/Using-foreach-loop-get-data-and-save-into-excel-fi

You read from the DataReader, but you don't check that any rows are returned: so when they aren't you get an error which explicitly tells you just that!
Start by checking the return value:
C#
if (rdr.Read();
   {
   if(reader[6].ToString() == "Male");
      {
   ...
And the immediate error will go away.

But...the code you show isn't the code you are running, because this won't compile:
C#
SqlCommand newCmd = new SqlCommand(Select * from student)", newCon);

And I suspect that your "real" code is using a WHERE clause to select which student to return info for. And that WHERE clause doesn't work...so no rows are returned.
 
Share this answer
 
I'd suggest to create Dictionary[^] object for such of functionality:

C#
Dictionary <string, string> images = new Dictionary<string, string>();
images.Add("Male", "C:\Pictures\Male.jpg");
images.Add("FeMale", "C:\Pictures\FeMale.jpg");


Then you'll be able to simply assign path to image depending on student's gender.
C#
PictureBox1.Image = Image.FromFile(images[reader[6]]);


For further details, please see:
Setting Pictures at Run Time (Windows Forms)[^]
Image.FromFile Method (String) (System.Drawing)[^]
 
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