I am trying to manually update a GridView row using TableAdapter.Update. I cannot use the object datasource.
I am able to fill the GridView, but when I call the update method, the error says that my datarow (id, etc) does not exist in the current context.
Below is my code. Does anyone know what am am missing or if my update statement is wrong. Thank you
I tried this method, but it throws a new error: CS0117: 'System.Web.UI.WebControls.GridViewEditEventArgs' does not contain a definition for 'OldValues'. When I take out that line, it tells me that no definition is contained for NewValues.
Any clues? I'm not sure how to go about defining these.
and
changed the update parameter slightly to eliminate the decsription not defined error:
this line: pAdapter.Update(name, desc, id);
I've also added a row cancelling event and the cancel event works, but it still throws the above error (Object reference not set to an instance of an object:
string name = e.NewValues["name"].ToString(); when I click on edit
I still seem to be missing something..do you have any ideas?
Do you have a column in the GridView bound to a field called "name"? If you don't, you have to change "name" to the name of the bound field holding the name value.
yes, I do. The bound columns are name and description..and id.
thanks. Thought maybe the gridview is empty when I click on edit or it is not finding the id..not really sure if I need a session object tied to the populated gridview..seems like I have tried everything..but no luck yet
Create an ObjectDataSource+TableAdapter to expose CRUD methods,and bind to the GridView by enabling its CRUD functions。
【Reasons】
If you create a new instance of TableAdapter from time to time,I think it cannot "remember" what it used to have,So there will be confliction when rendering the page……。So you need a "consistance" TableAdapter,with a table inside。So ObjectDataSource is better。
liza99
0 Points
8 Posts
TableAdapter Update GridView
Apr 09, 2012 09:41 AM|LINK
Hello,
I am trying to manually update a GridView row using TableAdapter.Update. I cannot use the object datasource.
I am able to fill the GridView, but when I call the update method, the error says that my datarow (id, etc) does not exist in the current context.
Below is my code. Does anyone know what am am missing or if my update statement is wrong. Thank you
partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e
{
productTableAdapter pAdapter = new productTableAdapter();
GridView1.DataSource = pAdapter.GetData();
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_RowUpdating(object sender, GridViewEditEventArgs e)
{
productTableAdapter pAdapter = new productTableAdapter();
GridView1.UpdateRow = pAdapter.Update(id = @original_id, name = @name, description = @description);
GridView1.DataBind();
}
}
<% @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title></head><body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
mm10
Contributor
6445 Points
1187 Posts
Re: TableAdapter Update GridView
Apr 09, 2012 12:13 PM|LINK
Get the updated values from the GridViewEventArgs.NewValues property:
protected void GridView1_RowUpdating(object sender, GridViewEditEventArgs e) { productTableAdapter pAdapter = new productTableAdapter(); int id = Convert.ToInt32(e.OldValues["id"]); string name = e.NewValues["name"].ToString(); string desc = e.NewValues["description"].ToString(); pAdapter.Update(id, name, description); GridView1.EditIndex = -1; GridView1.DataBind(); }liza99
0 Points
8 Posts
Re: TableAdapter Update GridView
Apr 09, 2012 02:46 PM|LINK
Hello mm10,
Thanks for repying.
I tried this method, but it throws a new error: CS0117: 'System.Web.UI.WebControls.GridViewEditEventArgs' does not contain a definition for 'OldValues'. When I take out that line, it tells me that no definition is contained for NewValues.
Any clues? I'm not sure how to go about defining these.
Thankyou
mm10
Contributor
6445 Points
1187 Posts
Re: TableAdapter Update GridView
Apr 09, 2012 05:36 PM|LINK
Oh, you should use the GridViewUpdateEventArgs:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
productTableAdapter pAdapter = new productTableAdapter();
int id = Convert.ToInt32(e.OldValues["id"]);
string name = e.NewValues["name"].ToString();
string desc = e.NewValues["description"].ToString();
pAdapter.Update(id, name, description);
GridView1.EditIndex = -1;
GridView1.DataBind();
}
liza99
0 Points
8 Posts
Re: TableAdapter Update GridView
Apr 09, 2012 10:40 PM|LINK
Thanks again, for replying
Now, however, I am getting this error: Object reference not set to an instance of an object: string name = e.NewValues["name"].ToString();
I've changed my HTML to this:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateEditButton="True"
OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowUpdating="GridView1_RowUpdating"></asp:GridView>
to eliminate a row editing not handled error,
and changed the update parameter slightly to eliminate the decsription not defined error:
this line: pAdapter.Update(name, desc, id);
I've also added a row cancelling event and the cancel event works, but it still throws the above error (Object reference not set to an instance of an object: string name = e.NewValues["name"].ToString(); when I click on edit
I still seem to be missing something..do you have any ideas?
Thanks for your replies
mm10
Contributor
6445 Points
1187 Posts
Re: TableAdapter Update GridView
Apr 10, 2012 07:31 AM|LINK
Do you have a column in the GridView bound to a field called "name"? If you don't, you have to change "name" to the name of the bound field holding the name value.
liza99
0 Points
8 Posts
Re: TableAdapter Update GridView
Apr 10, 2012 12:38 PM|LINK
yes, I do. The bound columns are name and description..and id.
thanks. Thought maybe the gridview is empty when I click on edit or it is not finding the id..not really sure if I need a session object tied to the populated gridview..seems like I have tried everything..but no luck yet
thanks
mm10
Contributor
6445 Points
1187 Posts
Re: TableAdapter Update GridView
Apr 10, 2012 12:48 PM|LINK
Set a breakpoint in the method and find out how many keys there are in e.NewValues.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: TableAdapter Update GridView
Apr 11, 2012 01:19 AM|LINK
Hello liza99:)
【Solutions】
Create an ObjectDataSource+TableAdapter to expose CRUD methods,and bind to the GridView by enabling its CRUD functions。
【Reasons】
If you create a new instance of TableAdapter from time to time,I think it cannot "remember" what it used to have,So there will be confliction when rendering the page……。So you need a "consistance" TableAdapter,with a table inside。So ObjectDataSource is better。
liza99
0 Points
8 Posts
Re: TableAdapter Update GridView
Apr 11, 2012 01:35 AM|LINK
Thanks mm10,
It appears that the values are empty, but I acnnot figure out why. Thanks for replying.