Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error:-Index was outside the bounds of the array
my code is:-
C#
SqlDataReader reader = cmd4.ExecuteReader();

  while (reader.Read())
  {
      Label6.Text = Convert.ToString(reader[0]);
      Label8.Text = Convert.ToString(reader[1]); //it gives error on this line.
      Label10.Text = Convert.ToString(reader[2]);
  }

so plz try to solve it...
Posted
Updated 12-Jun-12 19:44pm
v4
Comments
RDBurmon 13-Jun-12 9:30am    
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.

As explained here http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.90).aspx[^], "You use the Read method of the DataReader object to obtain a row from the results of the query. You can access each column of the returned row by passing the name or ordinal reference of the column to the DataReader. However, for best performance, the DataReader provides a series of methods that allow you to access column values in their native data types (GetDateTime, GetDouble, GetGuid, GetInt32, and so on). For a list of typed accessor methods for data provider-specific DataReaders, see OleDbDataReader and SqlDataReader. Using the typed accessor methods, assuming the underlying data type is known, reduces the amount of type conversion required when retrieving the column value."

One possibility could be that the Row returned by the Reader may be having only one column.

Please but a bread point at Label6.Text and check the contents of the reader to see whether it has required number of columns. Otherwise, please check the cmd4 for ensuring to return the required number of columns. Further instead of using Convert.ToString the GetString method explained here http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstring(v=vs.90)[^] can be used as suggested in the above reference.
 
Share this answer
 
Comments
Manas Bhardwaj 13-Jun-12 4:09am    
Correct +5!
VJ Reddy 13-Jun-12 4:31am    
Thank you, Manas :)
The way you use DataReader is not correct.

Change to:
C#
SqlDataReader reader = command.ExecuteReader();
        int i=0;
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine(reader[i].ToString());
                ++i;
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();

Look at these:
Retrieving Data Using a DataReader (ADO.NET)[^]
DataReader Class[^]
 
Share this answer
 
Comments
tanweer 13-Jun-12 1:53am    
good answer, my 5!
Member 9027483 13-Jun-12 2:25am    
after trying above code same error occure ..
Manas Bhardwaj 13-Jun-12 4:09am    
Correct +5!
Hi,

Check if your command have how many column returned. it may happen only if required resource is not available.

Also post your command code to identify accurately.

thanks
-Amit
 
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