Solved

Can NOT update from GridView

Posted on 2006-10-27
8
201 Views
Last Modified: 2010-08-05
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="ExpenseSqlDataSource" BorderColor="White" BorderWidth="1px" CellPadding="5" GridLines="None" ShowFooter="True" AutoGenerateColumns="False" DataKeyNames="cost_detail_id,cost_detail_amnt" Width="100%" OnDataBinding="GetExpenseTotal">
                                    <Columns>
                                        <asp:BoundField DataField="expense_type_desc" HeaderText="Expense Type" SortExpression="expense_type_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_name" 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_detail_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_capital_tot.Value)) %><br />
                                                <%# String.Format("{0:c}", Decimal.Parse(request_operating_tot.Value)) %><br />
                                                <%# String.Format("{0:c}", Decimal.Parse(bid_tot.Value)) %>
                                                </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.ConnectionString = 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.SelectCommand = sql;
        ExpenseSqlDataSource.SelectParameters.Clear();
        ExpenseSqlDataSource.SelectParameters.Add("project_id", project_id.Value);
        ExpenseSqlDataSource.DeleteCommand = "DELETE FROM cost_detail WHERE cost_detail_id = @cost_detail_id";
        ExpenseSqlDataSource.UpdateCommand = "UPDATE cost_detail SET cost_detail_amnt = @cost_detail_amnt WHERE cost_detail_id = @cost_detail_id";
        ExpenseView.DataBind();

0
Comment
Question by:HangTenDesign
  • 5
  • 3
8 Comments
 
LVL 10

Expert Comment

by:jnhorst
ID: 17820271
When you load the data in Page_Load (I am assuming), are you calling DataBind() on the grid within or without an if block checink for a postback?

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
0
 

Author Comment

by:HangTenDesign
ID: 17820782
Nope, that didn't do it.
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 17820941
Where does the code you posted above execute?

John
0
 

Author Comment

by:HangTenDesign
ID: 17821080
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:jnhorst
ID: 17821270
Show me the full Page_Load event...

John
0
 

Author Comment

by:HangTenDesign
ID: 17821692
rotected void Page_Load(object sender, EventArgs e)
    {
        PageTitle.Text = "New Project Approval";

        network_id = (string)HttpContext.Current.Session["network_id"];

        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.Attributes.Add("OnClick", "ShowProjectNumber();");
            budget_type_cd_UB.Attributes.Add("OnClick", "ShowProjectNumber();");
            budget_type_cd_EM.Attributes.Add("OnClick", "ShowProjectNumber();");
        }

        string sql;
        MpcSqlDataSource.ConnectionString = 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.SelectCommand = sql;
        MpcSqlDataSource.SelectParameters.Clear();
        MpcSqlDataSource.SelectParameters.Add("project_id", project_id.Value);
        MpcSqlDataSource.DeleteCommand = "DELETE FROM project_mpc WHERE project_mpc_id = @project_mpc_id";
        MpcSqlDataSource.DataBind();

        ExpenseSqlDataSource.ConnectionString = 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.SelectCommand = sql;
        ExpenseSqlDataSource.SelectParameters.Clear();
        ExpenseSqlDataSource.SelectParameters.Add("project_id", project_id.Value);
        ExpenseSqlDataSource.DeleteCommand = "DELETE FROM cost_detail WHERE cost_detail_id = @cost_detail_id";
        ExpenseSqlDataSource.UpdateCommand = "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;
        }
    }
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 17821739
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
0
 
LVL 10

Accepted Solution

by:
jnhorst earned 500 total points
ID: 17821801
Try this:

// get project id
project_id.Value = Request.QueryString["pid"];

// build your data source's and their command objects...

// check postback.
if (!IsPostBack)
{
     // get dropdown items... you can do this inside the block because you do not update dropdown lists.

     // bind the controls.  you MUST do this inside the block if you want to get user changes to data in the grids.
     MpcSqlDataSource.DataBind();
     ExpenseView.DataBind();

     // do other things...
}

John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Different Delete Messages 7 37
asp.net mvc return json 2 56
Trying to use colormap to replace color in png, with a gradient to transparent 7 31
Turn on intranet settings 1 40
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now