Link to home
Start Free TrialLog in
Avatar of kxradhi
kxradhi

asked on

GridView Update Rows

Hello,
I am trying to enable edit/update for a gridview(has 2 columns). I am able to edit it, but not able to update it.I have a procedure that takes 2 params a,b. I want to pass the values of the edited row in the gridview to the procedure to update.Any help please?

Thanks

Avatar of kxradhi
kxradhi

ASKER

I have a gridview for which the datasource is a dataset. In the  rowediting event, i have

        GridView2.EditIndex = e.NewEditIndex;

In the row update event , i have the following -
public void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Create_Role create_role = new Create_Role();

        GridViewRow row = GridView2.Rows[e.RowIndex];

        string t = (string)row.Cells[1].Text;
        string t1 = (string)row.Cells[0].Text;

        create_role.Update_Role_Page(t1,t);
}
and the definition for gridview -
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="true"
              BorderColor="#FFFF80" Font-Size="Small"  OnRowEditing="GridView2_RowEditing"
              onselectedindexchanged="GridView2_SelectedIndexChanged"
              OnUpdateCommand="GridView2_RowUpdating" OnCancelCommand="GridView2_RowCancelingEdit"  
              Width="264px" onrowupdating="GridView2_RowUpdating" >
              <Columns>
              <asp:CommandField
                       ShowEditButton="True"></asp:CommandField>
              </Columns>
          </asp:GridView>
I am getting an error - System.NullReferenceException: Object reference not set to an instance of an object.

Please help?
Avatar of Bob Learned
It would help to know what line you are getting that exception with?
Avatar of kxradhi

ASKER

Actually i was able to solve the issue of null reference, but i am not able to get the new values of the edited columns, The update procedure is working fine.

string t = (string)row.Cells[1].Text;
 string t1 = (string)row.Cells[0].Text;
What did you do to fix the exception, and what are you trying to achieve with the GridView that is doesn't do now?  I don't understand what those two lines are supposed to be doing for you.
ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kxradhi

ASKER

I changed the code to use the textboxes and I am getting this error
Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
Avatar of kxradhi

ASKER

Do i have to create text boxes and bind it?
You will need to change the index in Cells[i] to match the index where the textbox is located.
Yes, if you want to edit something, you need to create TemplateField columns, and have an ItemTemplate with Label controls, and an EditItemTemplate for TextBox controls.  Then, you can use row.FindControl with the ID for the control to get a reference.

Example shown here:

Using TemplateFields in the GridView Control
http://www.asp.net/learn/data-access/tutorial-12-cs.aspx



<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server"
            Text='<%# Bind("FirstName") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server"
            Text='<%# Bind("FirstName") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Open in new window

Avatar of kxradhi

ASKER

ok. I  have added the TemplateFields but I am still getting an error - DataBinding: 'System.Data.Common.DataRecordInternal' does not contain a property with the name 'Role_Id'. I am using a dataset to retrieve the rows into a gridview. Am i missing something?
Can you show us what you came up with, please?