Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

what is the reason for occurring this problem whenever execute the C# Code also retrieving the records from SQL server?

pls comments...
Posted

It's error related when you are retriving index record which is not exist.

See if there's 5 elements in array, But when you are trying to retrieve record at index 6 then it will generate Exception of kind IndexOutOfRangeException.

See HERE[^] for more details.

Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
Abhijit Jana 3-Nov-10 2:00am    
didn,t find any reason why some one voted it 1. Though it was correct answer. I voted 5 ! Thanks
Hiren solanki 3-Nov-10 2:04am    
Thanks abhijit.
Abhijit Jana 3-Nov-10 2:07am    
Don't know why someone voted it 1, though it was correct answer :( ! I voted 5.
IndexOutOfRange exception is thrown when you attempt to access (Read/ Write) an element of an array with an index which is outside the bounds of the array

As per your problem, I guess you are trying to read or write some array elements which are outside of your array boundary.
 
Share this answer
 
Comments
Hiren solanki 3-Nov-10 2:02am    
Yes, that is the issue i wanted to post in GIT but I thought it will be considered as bagging of point request. another one is also as accepted answer but it's also having downvote, See http://www.codeproject.com/Answers/123821/How-can-we-Give-Spaces-in-EmptyDataText-of-gridvie.aspx#answer2
Hiren solanki 3-Nov-10 2:02am    
Below answer is a reply to your comment in my answer.
C#
int ScriptId = 0;
               string sql = "Select Max(ScriptId) from tbl_Script";
               SqlCommand cmd = new SqlCommand(sql, connection);
               using (var reader = cmd.ExecuteReader())
               {
                   //while (reader.Read())
                   //{

                       if (reader["ScriptId"] == System.DBNull.Value)
                       {
                           ScriptId += 1;
                       }
                       else
                       {
                           ScriptId = ScriptId++;
                       }
                   //}
               }


this is my code i am face same error (IndexOutOfRangeException was unhandled) this is an issue pls let me know ans
 
Share this answer
 
your getting only one value here so executescalar() method instead executereader()
 
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