Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3425682
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:37:24+00:00 2026-05-18T06:37:24+00:00

I am posting this question after reading other similar questions about my problem but

  • 0

I am posting this question after reading other similar questions about my problem but not really understanding how I can use the information.

I have been mainly writing this code to see how I can use SqlDataAdapters to update a database from a GridView.

I am writing my GridView in my aspx page as follows:

<asp:GridView ID="Clients" runat="server">
    <Columns>
        <asp:TemplateField HeaderText="Name" SortExpression="Name">
            <ItemTemplate>
                <asp:Label ID="labelName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="textboxName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField EditText="Edit" ShowEditButton="true" />
    </Columns>
</asp:GridView>

Then in my code behind file I am writing the following code (Database is just a class to connect to my database…):

Database database = new Database();
database.open_connection();

SqlCommand command = new SqlCommand(query, database.dbConnection);
SqlDataAdapter adapter = new SqlDataAdapter(command);

DataTable dataTable = new DataTable();
adapter.Fill(dataTable);

SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

Clients.AutoGenerateColumns = false;
Clients.PageIndexChanging += new GridViewPageEventHandler(this.grid_view_page_index_changing);
Clients.Sorting += new GridViewSortEventHandler(this.grid_view_sorting);
Clients.RowEditing += new GridViewEditEventHandler(this.row_editing);
Clients.RowUpdating += new GridViewUpdateEventHandler(this.row_updating);
Clients.RowCancelingEdit += new GridViewCancelEditEventHandler(this.row_canceling_edit);
Clients.AllowPaging = true;
Clients.PageSize = 25;
Clients.AllowSorting = true;
Clients.DataSource = dataTable;
Clients.DataBind();

database.close_connection();

This all works fine so far; The GridView Sorting, Editing, RowCancellingEdit, PageIndexChanging functions etc work as they should do.

My problem is when I call the RowUpdating function.

What I want to do is use the adapter.Update() function to update the database.

It doesn’t throw any errors with my current code, but it doesn’t update the database either.

As soon as I click update, the edit text box from my GridView disappears and I am left with the original value before trying to edit it.

This is my row_updating() function:

public void row_updating(object sender, GridViewUpdateEventArgs e) {
    GridViewRow gvr = gridView.Rows[Clients.EditIndex];

    TextBox txt = (TextBox)gvr.Cells[0].FindControl("textboxName");
    e.NewValues["Name"] = txt.Text;

    adapter.Update((DataTable)Clients.DataSource);

    Clients.EditIndex = -1;
    Clients.DataBind();
}

I can’t figure out why it won’t update the database (Probably because I am doing it completely wrong)

I have seen stuff around the internet that mentions a EndEdit() function, but I’m not sure if that applies here.

If anyone could tell me what I am doing wrong and why my database won’t update it would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:37:25+00:00Added an answer on May 18, 2026 at 6:37 am

    I changed my row_updating() functions code which isn’t of much relevence here, but the thing that fixed it for me was making sure I used !Page.IsPostBack

    So now my Page_Load() functions sets up the GridView and then instead of just binding the data it goes:

    if(!Page.IsPostBack) {
        Clients.DataBind();
    }
    

    Although it isn’t working perfectly, at least it is now updating my database (Which is what this question was about, so I will post a new questions if I can’t find a solution to my new problem)

    Sometimes it’s just the simplest things that cause the most frustrating problems…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might not be a programming question but I am posting it after long
This question is already answered. I discovered what the problem is. But, I'm posting
I noticed prior to posting this question that there have been similar questions posted
After posting this question and reading that one I realized that it is very
After I posting this question I tried to reproduce the problem of accidental rvalue
I searched StackOverflow before posting this question but I wasn't able to find the
It might be a long shot posting this question here but we will see.
Before you all get pissy about me posting this question again, let me explain
I am again posting the same question. Please help me in this problem. Here
I'm posting this question (and answer) so if anybody else has this problem in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.