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

kxradhiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kxradhiAuthor Commented:
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?
0
Bob LearnedCommented:
It would help to know what line you are getting that exception with?
0
kxradhiAuthor Commented:
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;
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Bob LearnedCommented:
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.
0
prairiedogCommented:
Hi kxradhi,
You are on the right track.
In Edit mode, all editable fields become TextBoxes, so first you need to find those text boxes before you can retrieve their values.
string t = (string)row.Cells[1].Text; is not the right way to retrieve data. Change it to this:
TextBox txt1 = (TextBox)row.Cells[1].Controls[0];
TextBox txt2 = (TextBox)row.Cells[0].Controls[0];
string t = txt1.Text;
string t1 = txt2.Text;
Then continue with your update process.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kxradhiAuthor Commented:
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'.
0
kxradhiAuthor Commented:
Do i have to create text boxes and bind it?
0
prairiedogCommented:
You will need to change the index in Cells[i] to match the index where the textbox is located.
0
Bob LearnedCommented:
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

0
kxradhiAuthor Commented:
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?
0
Bob LearnedCommented:
Can you show us what you came up with, please?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.