Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
Hi All,
I am trying to update gridview row,but i am getting the error.
i am using the below code

C#
DataTable dt = (DataTable)Session["table"];


               GridViewRow row = Admin_Grid.Rows[e.RowIndex];
               dt.Rows[row.DataItemIndex]["APCUserID"] = ((Label)(row.Cells[1].Controls[0]));
              dt.Rows[row.DataItemIndex]["FirstName"] = ((TextBox)(row.Cells[2].Controls[0]));
               dt.Rows[row.DataItemIndex]["LastName"] = ((TextBox)(row.Cells[3].Controls[0]));
               dt.Rows[row.DataItemIndex]["Organization"] = ((TextBox)(row.Cells[4].Controls[0]));
               dt.Rows[row.DataItemIndex]["PhoneNumber"] = ((TextBox)(row.Cells[5].Controls[0]));
               dt.Rows[row.DataItemIndex]["EmailAddress"] = ((TextBox)(row.Cells[6].Controls[0]));
               //dt.Rows[row.DataItemIndex]["AIPServed"] = ((TextBox)(row.Cells[0].Controls[1]));
               dt.Rows[row.DataItemIndex]["DropDownUserStatus"] = ((DropDownList)(row.Cells[7].Controls[0]));
               // dt.Rows[row.DataItemIndex]["UserID"] = ((TextBox)(row.Cells[0].Controls[1]));
               // dt.Rows[row.DataItemIndex]["Password"] = (TextBox)Admin_Grid.Rows[e.RowIndex].FindControl("TextBoxEmailAddress");
             //  dt.Rows[row.DataItemIndex]["DropDownUserStatus"] = (DropDownList)Admin_Grid.Rows[e.RowIndex].FindControl("DropDownUserStatus");
               //Reset the edit index.
               Admin_Grid.EditIndex = -1;
              // ((TextBox)(row.Cells[1].Controls[0])).Text;
               //Bind data to the GridView control.
               LoadAdminData();


i am getting the error the bolded one

Any one can u please share solution to me.

What I have tried:

I tried all the possible ways,i am getting row value but it's coming down to next line it's throwing error is There is no row position.
Posted
Updated 9-Aug-16 8:43am
Comments
The Praveen Singh 9-Aug-16 8:30am    
first check in your data table there should be all column name which you used on RowIndex. For eg-
dt.Rows[row.DataItemIndex]["APCUserID"] = ((Label)(row.Cells[1].Controls[0])); in dt there shuld be "APCUserID",this the only cause when error occurs as you mentioned.
Richard Deeming 9-Aug-16 9:44am    
If you get an error, then you need to tell us what the error is. We don't have access to your computer, so we can't run your code to find out.

Click "Improve question" and update your question with the full details of the error you're getting.
Ram349 9-Aug-16 12:40pm    
Hi Praveen,
Thanks for responding my post.can i declare all the column names predefined.can u please please tel me how should declare .

1 solution

A few notes for you:

(1) Always check for nulls when referencing a Session object.
C#
if(Session["table"] != null){
     //your code here
}

(2)Always check for the Rows count before accessing the grid’s row collection.
C#
if(Admin_Grid.Rows.Count > 0){
    //your code here
}

(3) If you want to get the text vale of a Label, then you should use the .Text after casting the object to a Label.
C#
string someVariableName = ((Label)(row.Cells[1].Controls[0])).Text;

(4)Verify that you your DataTable contains the name of the field that you are trying to access. In your case, make sure that the APCUserID field name exist in your DataTable otherwise it will throws an error. If you are unsure about the field names, then you can alternatively reference them using indeces. For example:
C#
dt.Rows[row.DataItemIndex][0] = ((Label)(row.Cells[1].Controls[0])).Text;


The downside of using index in referencing a column is that if the sequence of column changes, then your data/logic will be messed up.
 
Share this answer
 
v2

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