HangTenDesign
asked on
Can NOT update from GridView
I am totally stuck! I have a gridview where the select, and delete commands are working just fine. However, the updates are not working. I don't get any errors but the data never changes. Here is my GridView and Code Behind.
<asp:GridView ID="ExpenseView" runat="server" DataSourceID="ExpenseSqlDa taSource" BorderColor="White" BorderWidth="1px" CellPadding="5" GridLines="None" ShowFooter="True" AutoGenerateColumns="False " DataKeyNames="cost_detail_ id,cost_de tail_amnt" Width="100%" OnDataBinding="GetExpenseT otal">
<Columns>
<asp:BoundField DataField="expense_type_de sc" HeaderText="Expense Type" SortExpression="expense_ty pe_desc" >
<HeaderStyle CssClass="frm_tbl_head" />
<FooterStyle CssClass="total_area" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="asset_type_desc " HeaderText="Asset Type" SortExpression="asset_type _desc" >
<HeaderStyle CssClass="frm_tbl_head" />
<FooterStyle CssClass="total_area" HorizontalAlign="Right" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="vendor_name" HeaderText="Vendor Name" SortExpression="vendor_nam e" FooterText="<span style='line-height:20px;'> Capital Total:<br>Operating Total:<br>Bid Total:</span>" >
<HeaderStyle CssClass="frm_tbl_head" />
<FooterStyle CssClass="total_area" HorizontalAlign="Right" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:TemplateField HeaderText="Amount" SortExpression="cost_detai l_amnt">
<HeaderStyle CssClass="frm_tbl_head" />
<FooterStyle CssClass="total_area" />
<ItemStyle Wrap="False" />
<ItemTemplate>
<%# String.Format("{0:c}", Eval("cost_detail_amnt")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("cost_detail_amnt") %>' />
</EditItemTemplate>
<FooterTemplate>
<span style="line-height:20px;">
<%# String.Format("{0:c}", Decimal.Parse(request_capi tal_tot.Va lue)) %><br />
<%# String.Format("{0:c}", Decimal.Parse(request_oper ating_tot. Value)) %><br />
<%# String.Format("{0:c}", Decimal.Parse(bid_tot.Valu e)) %>
</span>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" DeleteText="clear" ShowEditButton="true" EditText="edit" UpdateText="update" CancelText="cancel">
<FooterStyle CssClass="total_area" />
</asp:CommandField>
</Columns>
<HeaderStyle BackColor="White" Wrap="False" HorizontalAlign="Left" />
</asp:GridView>
<asp:SqlDataSource ID="ExpenseSqlDataSource" runat="server" />
--------------------- CODE BEHIND -------------------------- ------
ExpenseSqlDataSource.Conne ctionStrin g = cnnStr;
sql = "SELECT e.expense_type_cd, e.expense_type_desc, a.asset_type_desc, " +
"c.vendor_name, c.cost_detail_amnt, c.cost_detail_id " +
"FROM cost_detail c " +
"INNER JOIN asset_type a " +
"ON c.asset_type_id = a.asset_type_id " +
"INNER JOIN expense_type e " +
"ON a.expense_type_cd = e.expense_type_cd " +
"WHERE c.project_id = @project_id " +
"ORDER BY c.cost_detail_id";
ExpenseSqlDataSource.Selec tCommand = sql;
ExpenseSqlDataSource.Selec tParameter s.Clear();
ExpenseSqlDataSource.Selec tParameter s.Add("pro ject_id", project_id.Value);
ExpenseSqlDataSource.Delet eCommand = "DELETE FROM cost_detail WHERE cost_detail_id = @cost_detail_id";
ExpenseSqlDataSource.Updat eCommand = "UPDATE cost_detail SET cost_detail_amnt = @cost_detail_amnt WHERE cost_detail_id = @cost_detail_id";
ExpenseView.DataBind();
<asp:GridView ID="ExpenseView" runat="server" DataSourceID="ExpenseSqlDa
<Columns>
<asp:BoundField DataField="expense_type_de
<HeaderStyle CssClass="frm_tbl_head" />
<FooterStyle CssClass="total_area" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="asset_type_desc
<HeaderStyle CssClass="frm_tbl_head" />
<FooterStyle CssClass="total_area" HorizontalAlign="Right" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="vendor_name" HeaderText="Vendor Name" SortExpression="vendor_nam
<HeaderStyle CssClass="frm_tbl_head" />
<FooterStyle CssClass="total_area" HorizontalAlign="Right" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:TemplateField HeaderText="Amount" SortExpression="cost_detai
<HeaderStyle CssClass="frm_tbl_head" />
<FooterStyle CssClass="total_area" />
<ItemStyle Wrap="False" />
<ItemTemplate>
<%# String.Format("{0:c}", Eval("cost_detail_amnt")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("cost_detail_amnt") %>' />
</EditItemTemplate>
<FooterTemplate>
<span style="line-height:20px;">
<%# String.Format("{0:c}", Decimal.Parse(request_capi
<%# String.Format("{0:c}", Decimal.Parse(request_oper
<%# String.Format("{0:c}", Decimal.Parse(bid_tot.Valu
</span>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" DeleteText="clear" ShowEditButton="true" EditText="edit" UpdateText="update" CancelText="cancel">
<FooterStyle CssClass="total_area" />
</asp:CommandField>
</Columns>
<HeaderStyle BackColor="White" Wrap="False" HorizontalAlign="Left" />
</asp:GridView>
<asp:SqlDataSource ID="ExpenseSqlDataSource" runat="server" />
--------------------- CODE BEHIND --------------------------
ExpenseSqlDataSource.Conne
sql = "SELECT e.expense_type_cd, e.expense_type_desc, a.asset_type_desc, " +
"c.vendor_name, c.cost_detail_amnt, c.cost_detail_id " +
"FROM cost_detail c " +
"INNER JOIN asset_type a " +
"ON c.asset_type_id = a.asset_type_id " +
"INNER JOIN expense_type e " +
"ON a.expense_type_cd = e.expense_type_cd " +
"WHERE c.project_id = @project_id " +
"ORDER BY c.cost_detail_id";
ExpenseSqlDataSource.Selec
ExpenseSqlDataSource.Selec
ExpenseSqlDataSource.Selec
ExpenseSqlDataSource.Delet
ExpenseSqlDataSource.Updat
ExpenseView.DataBind();
ASKER
Nope, that didn't do it.
Where does the code you posted above execute?
John
John
ASKER
In the page_load. But I replaced with you code and it still did not work. I even started a new browser to be sure.
Show me the full Page_Load event...
John
John
ASKER
rotected void Page_Load(object sender, EventArgs e)
{
PageTitle.Text = "New Project Approval";
network_id = (string)HttpContext.Curren t.Session[ "network_i d"];
Security _Security = new Security(network_id);
if (!IsPostBack)
{
project_id.Value = Request.QueryString["pid"] ;
GetDropdownItems();
if (project_id.Value == "")
{
GetUserInfo();
session_id.Value = GetUniqueId(15);
project_id.Value = "0";
SetSessionId();
}
else
{
GetProjectInfo();
}
ShowHideSteps(1);
budget_type_cd_BU.Attribut es.Add("On Click", "ShowProjectNumber();");
budget_type_cd_UB.Attribut es.Add("On Click", "ShowProjectNumber();");
budget_type_cd_EM.Attribut es.Add("On Click", "ShowProjectNumber();");
}
string sql;
MpcSqlDataSource.Connectio nString = cnnStr;
sql = "SELECT pm.project_mpc_id, m.assetid, m.assettype, m.total_cost " +
"FROM mpc m " +
"INNER JOIN project_mpc pm " +
"ON m.mpc_id = pm.mpc_id " +
"WHERE pm.project_id = @project_id " +
"ORDER BY pm.project_mpc_id";
MpcSqlDataSource.SelectCom mand = sql;
MpcSqlDataSource.SelectPar ameters.Cl ear();
MpcSqlDataSource.SelectPar ameters.Ad d("project _id", project_id.Value);
MpcSqlDataSource.DeleteCom mand = "DELETE FROM project_mpc WHERE project_mpc_id = @project_mpc_id";
MpcSqlDataSource.DataBind( );
ExpenseSqlDataSource.Conne ctionStrin g = cnnStr;
sql = "SELECT e.expense_type_cd, e.expense_type_desc, a.asset_type_desc, " +
"c.vendor_name, c.cost_detail_amnt, c.cost_detail_id " +
"FROM cost_detail c " +
"INNER JOIN asset_type a " +
"ON c.asset_type_id = a.asset_type_id " +
"INNER JOIN expense_type e " +
"ON a.expense_type_cd = e.expense_type_cd " +
"WHERE c.project_id = @project_id " +
"ORDER BY c.cost_detail_id";
ExpenseSqlDataSource.Selec tCommand = sql;
ExpenseSqlDataSource.Selec tParameter s.Clear();
ExpenseSqlDataSource.Selec tParameter s.Add("pro ject_id", project_id.Value);
ExpenseSqlDataSource.Delet eCommand = "DELETE FROM cost_detail WHERE cost_detail_id = @cost_detail_id";
ExpenseSqlDataSource.Updat eCommand = "UPDATE cost_detail SET cost_detail_amnt = @cost_detail_amnt WHERE cost_detail_id = @cost_detail_id";
ExpenseView.DataBind();
if (iStep.Value == "3")
{
Page.SmartNavigation = true;
}
}
{
PageTitle.Text = "New Project Approval";
network_id = (string)HttpContext.Curren
Security _Security = new Security(network_id);
if (!IsPostBack)
{
project_id.Value = Request.QueryString["pid"]
GetDropdownItems();
if (project_id.Value == "")
{
GetUserInfo();
session_id.Value = GetUniqueId(15);
project_id.Value = "0";
SetSessionId();
}
else
{
GetProjectInfo();
}
ShowHideSteps(1);
budget_type_cd_BU.Attribut
budget_type_cd_UB.Attribut
budget_type_cd_EM.Attribut
}
string sql;
MpcSqlDataSource.Connectio
sql = "SELECT pm.project_mpc_id, m.assetid, m.assettype, m.total_cost " +
"FROM mpc m " +
"INNER JOIN project_mpc pm " +
"ON m.mpc_id = pm.mpc_id " +
"WHERE pm.project_id = @project_id " +
"ORDER BY pm.project_mpc_id";
MpcSqlDataSource.SelectCom
MpcSqlDataSource.SelectPar
MpcSqlDataSource.SelectPar
MpcSqlDataSource.DeleteCom
MpcSqlDataSource.DataBind(
ExpenseSqlDataSource.Conne
sql = "SELECT e.expense_type_cd, e.expense_type_desc, a.asset_type_desc, " +
"c.vendor_name, c.cost_detail_amnt, c.cost_detail_id " +
"FROM cost_detail c " +
"INNER JOIN asset_type a " +
"ON c.asset_type_id = a.asset_type_id " +
"INNER JOIN expense_type e " +
"ON a.expense_type_cd = e.expense_type_cd " +
"WHERE c.project_id = @project_id " +
"ORDER BY c.cost_detail_id";
ExpenseSqlDataSource.Selec
ExpenseSqlDataSource.Selec
ExpenseSqlDataSource.Selec
ExpenseSqlDataSource.Delet
ExpenseSqlDataSource.Updat
ExpenseView.DataBind();
if (iStep.Value == "3")
{
Page.SmartNavigation = true;
}
}
You are building your expense data source and calling DataBind() outside the if (!IsPostBack) {} code block. What is happening thus is every time you change your data and click an Update command link/button you are requerying your database and rebinding the grid with the data in the database before the UpdateCommand routine locates the data to do an update. You need to build and execute your command object outside the if block, but call DataBind() on the grid inside the if block so that the grid does not get refreshed before your UpdateCommand routine fires. This way the grid will still have the user's changes when your UpdateCommand routine fires.
John
John
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For example: (inside of Page_Load)
// code to load data here...
// check postback.
if (!this.IsPostBack)
{
// bind grid.
grdView.DataBind();
}
The reason you want to get your data outside of the if block above, and bind inside the block is when you click your Update link, Page_Load will fire first, then your UpdateCommand routine. If you are calling DataBind() outside of the if block (meaning it gets called on a postback) the DataBind() call replaces whatever changes your user makes with the original value, which is likely why you do not see any update.
John