2

Hi guys I really need your help I have a textbox inside a datalist the problem is, it does not get initialized with the new value I enter except the value from the database. I don't know if anyone have had such experience before, I will gladly appreciate your help.

Here is the html:

 <asp:DataList ID="SubList" runat="server" DataKeyField="ProductID" OnUpdateCommand="SubList_UpdateCommand">
                    <ItemTemplate>
                        PRICE: <%# Eval("Price", "{0:c}") %>
                       Quantity:<asp:Label ID="EditQuantitylbl" Text='<%# Eval("Quantity").ToString() %>' runat="server"></asp:Label>
                        COLOR:<asp:Label runat="server" ID="ColorLbl" Text='<%# Eval("ColorName").ToString() %>' ></asp:Label>
                        Size:<asp:Label runat="server" ID="Sizelbl" Text='<%# Eval("SizeName").ToString() %>' ></asp:Label>
                        PRODUCT NAME:<asp:Label runat="server" ID="productNamelbl" Text='<%# Eval("ProductName").ToString() %>'></asp:Label> 
                        <br />
                        <asp:LinkButton runat="server" ID="editBtn" CommandName="edit">Edit</asp:LinkButton><br />
                    </ItemTemplate>
                </asp:DataList>

Here is the code behind file:

     protected void SubList_UpdateCommand(object source, DataListCommandEventArgs e)
    {

        string Pid = SubList.DataKeys[e.Item.ItemIndex].ToString();
        string colorlbl = ((Label)e.Item.FindControl("Coloredit")).Text;
        string sizelbl = ((Label)e.Item.FindControl("Sizeedit")).Text;
        string quantityBox = ((TextBox)e.Item.FindControl("EditQuantityBox")).Text;
        bool success = true;

        string colorID = ShoppingCartAccess.GetColorID(colorlbl);
        string sizeID = ShoppingCartAccess.GetSizeID(sizelbl);
        int quantity;
        if (Int32.TryParse(quantityBox, out quantity)) {
             success = success && ShoppingCartAccess.UpdateItem(Pid, quantity, colorID, sizeID);
        }

        else
        {
            success = false;
        }
        statusLabel.Text = success ?
            "Your shopping cart was successfully updated!" :
            "Some quantity updates failed! Please verify your cart!";
        SubList.EditItemIndex = -1;
        SubList.DataBind();
    }

Here is the code that update:

       public static bool UpdateItem(string productId, int quantity, string colorId, string sizeId)
    {
        // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "ShoppingCartUpdateItem";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@CartID";
        param.Value = shoppingCartId;
        param.DbType = DbType.String;
        param.Size = 36;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@ProductID";
        param.Value = productId;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@Quantity";
        param.Value = quantity;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@ColorID";
        param.Value = colorId;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@SizeID";
        param.Value = sizeId;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);

        // returns true in case of success and false in case of an error
        try
        {
            // execute the stored procedure and return true if it executes
            // successfully, and false otherwise
            return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
        }
        catch
        {
            // prevent the exception from propagating, but return false to
            // signal the error
            return false;
        }
    }

where did I go wrong?

banji
  • 197
  • 1
  • 1
  • 4
  • If the DataList works like the ListView, you should set the DataSource before calling `DataBind` in the `UpdateCommand` event handler (see: http://stackoverflow.com/questions/36827111/asp-net-gridview-how-to-edit-and-delete-data-records/36828018#36828018). Note: I don't see the edit controls in your markup code. – ConnorsFan Jun 24 '16 at 16:54

0 Answers0