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.
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.IsPostBackSo now my
Page_Load()functions sets up the GridView and then instead of just binding the data it goes: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…