Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On click update command not taking new values

Status
Not open for further replies.

f0z

Programmer
Jan 10, 2003
48
US
Hi all,

I'm using a datagrid that connects to a sql server database. The datagrid has paging enabled and has an edit column aswell.

The problem i have is this, when I click on the edit link the text box appear and i'm able to change the information but when I click on the update link the value reverts to what it was prior to the change.

I've seen other posts that suggest putting the DataGrid1.DataBind() into an if(!Page.IsPostBack) but that doesn't seem to work. If anything this seems to make things worse. When i click on either the Paging links or the edit links the datagrid disappears.


Any help is greatly appreciated - i have a feeling its something really simple.

----------------- ASPX Page -----------------

<%@ Page language=&quot;c#&quot; Codebehind=&quot;view.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;cms.hr.payscales.view&quot; %>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
<title>View all pay scales</title>
<meta content=&quot;Microsoft Visual Studio 7.0&quot; name=&quot;GENERATOR&quot;>
<meta content=&quot;C#&quot; name=&quot;CODE_LANGUAGE&quot;>
<meta content=&quot;JavaScript&quot; name=&quot;vs_defaultClientScript&quot;>
<meta content=&quot; name=&quot;vs_targetSchema&quot;>
<link href=&quot; type=&quot;text/css&quot; rel=&quot;stylesheet&quot;>
</HEAD>
<body MS_POSITIONING=&quot;FlowLayout&quot;>
<form id=&quot;view&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:datagrid OnPageIndexChanged=&quot;DataGrid1_PageIndexChanged&quot; OnCancelCommand=&quot;DataGrid1_CancelCommand&quot; OnEditCommand=&quot;DataGrid1_EditCommand&quot; OnUpdateCommand=&quot;DataGrid1_UpdateCommand&quot; AllowPaging=&quot;true&quot; PageSize=&quot;7&quot; PagerStyle-Mode=&quot;NumericPages&quot; PagerStyle-PageButtonCount=&quot;3&quot; id=&quot;DataGrid1&quot; runat=&quot;server&quot; Width=&quot;60%&quot; AutoGenerateColumns=&quot;False&quot; BorderColor=&quot;#999999&quot; BorderStyle=&quot;Solid&quot; BorderWidth=&quot;1px&quot; BackColor=&quot;White&quot; CellPadding=&quot;3&quot; GridLines=&quot;Vertical&quot; ForeColor=&quot;Black&quot;>
<AlternatingItemStyle BorderStyle=&quot;None&quot; BackColor=&quot;#CCCCCC&quot;></AlternatingItemStyle>
<HeaderStyle Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; BackColor=&quot;Black&quot;></HeaderStyle>
<FooterStyle BackColor=&quot;#CCCCCC&quot;></FooterStyle>
<Columns>
<asp:BoundColumn DataField=&quot;Title&quot; HeaderText=&quot;Grade Title&quot;></asp:BoundColumn>
<asp:TemplateColumn HeaderText=&quot;Union&quot;>
<ItemTemplate>
<asp:Label ID=&quot;Union&quot; Runat=&quot;server&quot; Text='<%# DataBinder.Eval(Container.DataItem, &quot;Union&quot;) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType=&quot;LinkButton&quot; UpdateText=&quot;Update&quot; HeaderText=&quot;Edit&quot; CancelText=&quot;Cancel&quot; EditText=&quot;Edit&quot;></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign=&quot;Left&quot; ForeColor=&quot;Black&quot; BackColor=&quot;#999999&quot; PageButtonCount=&quot;4&quot; Mode=&quot;NumericPages&quot;></PagerStyle>
</asp:datagrid></form>
</body>
</HTML>

------------------- ASPX.CS -------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace cms.hr.payscales
{
/// <summary>
/// Summary description for view.
/// </summary>
public class view : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConn;
protected System.Data.SqlClient.SqlDataAdapter sqlDA;
protected System.Data.DataSet ds1;

private void Page_Load(object sender, System.EventArgs e)
{
BindGrid();
}
public void BindGrid(){
this.sqlConn = new SqlConnection(&quot;data source=localhost;initial catalog=hr payscales; integrated security=SSPI&quot;);
sqlConn.Open();

sqlDA = new SqlDataAdapter(&quot;select distinct Title, [Union] from Payscales&quot;, sqlConn);

ds1 = new DataSet();
sqlDA.Fill(this.ds1, &quot;Payscales&quot;);
DataGrid1.DataSource = ds1;
DataGrid1.DataBind();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}

public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// do sql update
TextBox tbTemp = (TextBox)e.Item.Cells[0].Controls[0];
this.sqlConn = new SqlConnection(&quot;data source=localhost;initial catalog=hr payscales; integrated security=SSPI&quot;);

string strCommand = &quot;update Payscales set Title = '&quot; + tbTemp.Text + &quot;' where Title = 'test'&quot;;

SqlCommand update = new SqlCommand(strCommand, sqlConn);

this.sqlConn.Open();
update.ExecuteNonQuery();
this.sqlConn.Close();

DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}

public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
}
}
 
Ok, I think I see your problem. In your update function, you databind without having a datasource:

DataGrid1.EditItemIndex = -1;
//DataGrid1.DataSource = some source
DataGrid1.DataBind();

As to databinding in (!Page.IsPostBack), generally it's a good idea. Databinding should be related to when a change happens in the database, or the first time a user visits the web page. Otherwise you end up having excess trips to the server.
 
nope still no luck - it's still not taking the new value.

thanks for the suggestion.
 
is the where clause of you SqlCommand's commandtext value actually correct?

i.e.,
&quot;update Payscales set Title = '&quot; + tbTemp.Text + &quot;' where Title = 'test'&quot;;



Rhys

Be careful that the light at the end of the tunnel isn't a train coming the other way.
 
yeah the update sql is working fine - i've just set up a few &quot;test&quot; entries.
 
Here's an abstract and a link.

Notice the Page_Load Event Handler!
Notice that the Page_Load event handler for the editable DataGrid only calls the BindData() subroutine when the page is first visited. That is, it is not called on subsequent postbacks. This is vital! If you change the Page_Load event handler so that it calls BindData() every time, your edited values will not be saved in the database. For a thorough explanation as to why this is the case, be sure to read the FAQ: Why Your DataGrid's Updates Don't Show Up.

 
Thanks for that link - it proved very helpful.

I had a feeling it would have been something really simple, I hadn't called BindGrid() in the Cancel, Edit, Update functions.

Thanks all for the help - much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top