Click here to Skip to main content
15,910,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I am using asp.net c# , i want to check that gridview cell value is equal to textbox value i am writing this code by it is giving error ..
"
SQL
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

"

C#
if (Gridview1.Rows[0].Cells[5].Text.ToString() == textbox1.Text)
        {
            Loaddata();
        }
        else
        {
            lblerror.text = "Error";
            textbox2.Focus();
        }


Please Help.............
Posted

C#
if (Gridview1.Rows[0].Cells[5].Text.ToString() == textbox1.Text)
        {//Why are you checking cell 5 text with textbox ???
            Loaddata();
   //Note: After this line gridview will populate. If you are loading gridview here
        }
        else
        {
            lblerror.text = "Error";
            textbox2.Focus();
        }


See at the earlier point you are checking gridview row index 0. Where there is no row binded to gridview it's before. So how could this "if (Gridview1.Rows[0].Cells[5].Text.ToString() == textbox1.Text)" will work.

YOur condition is wrong that's why it's giving error to you
 
Share this answer
 
v2
Comments
Member 11014751 14-Oct-14 1:04am    
"if (Gridview1.Rows[0].Cells[5].Text.ToString() == textbox1.Text)"
this is also not working
[no name] 14-Oct-14 1:05am    
yes I told that after loaddata if you will check this line then it will work
Member 11014751 14-Oct-14 1:06am    
Ohhhh Yes i am making mistake ..... Thank You So much...
[no name] 14-Oct-14 1:07am    
welcome :)
Like you message is saying, you are trying to access an array outside is range. The error could be generated by two reasons:
1.Your grid view has only 5 columns (with indexes from 0 to 4) and you are trying to access the 6th column that does not exist.
2.You are trying to access the grid view 1st row, but you grid view does not contain any row at that time.

PS: You could find details and examples about the using of the ASPX grid view in my next article: Advanced ASPX GridView Pagination and Data Entities[^]
 
Share this answer
 
check how many columns are there in gridview. index start from 0 onwards, if there are six columns in your gridview, then it won't thrown an error.
 
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