DetailsView Updating With Old Values , db Bulgaria
  • 11 years ago
    Ok, Now I'm sure this was working the other day and I don't think I've changed anything but now it's not working and I can't see anything wrong with the code. Basically I have a details view with product information in it, when the user clicks edit it goes into edit mode when I change any of the fields and click update it updates but with the old values. Here is my detailsview:

    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" Height="50px" Width="50%" CellPadding="4" ForeColor="Black" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellSpacing="2">


    <asp:TemplateField HeaderText="Product Name">


    <asp:TextBox ID="ProductNameTextBox" runat="server" Text='<%# Bind("itemname") %>'></asp:TextBox>




    <asp:Label ID="ProductName" runat="server" Text='<%# Bind("itemname") %>'></asp:Label>




    <asp:TemplateField HeaderText="Description">


    <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("description") %>'></asp:TextBox>




    <asp:Label ID="Description" runat="server" Text='<%# Bind("description") %>'></asp:Label>




    <asp:TemplateField HeaderText="Price">


    <asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("price", "{0:c}") %>'></asp:TextBox>




    <asp:Label ID="Price" runat="server" Text='<%# Bind("price", "{0:c}") %>'></asp:Label>




    <asp:TemplateField HeaderText="Quantity">


    <asp:TextBox ID="QuantityTextBox" runat="server" Text='<%# Bind("stock") %>'></asp:TextBox>




    <asp:Label ID="Quantity" runat="server" Text='<%# Bind("stock") %>'></asp:Label>



    <asp:CommandField ShowEditButton="True"/>



    <%#Eval ("itemname") %>



    And here is my VB code with the item updating function that is called when the edit button is clicked. 

    Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating

    Dim name As TextBox = DetailsView1.FindControl("ProductNameTextBox")

    Dim description As TextBox = DetailsView1.FindControl("DescriptionTextBox")

    Dim price As TextBox = DetailsView1.FindControl("PriceTextBox")

    Dim quantity As TextBox = DetailsView1.FindControl("QuantityTextBox")

    Dim ItemId As Integer = DetailsView1.DataKey.Value

    Dim updatedName As String = name.Text

    Dim updatedDescription As String = description.Text

    Dim updatedPrice As Decimal = price.Text

    Dim updatedQuantity As Integer = quantity.Text

    conn = New SqlConnection(strConn)cmd = New SqlCommand("UpdateProductDetails", conn)

    cmd.CommandType = Data.CommandType.StoredProcedure

    cmd.Parameters.Add("@ItemId", Data.SqlDbType.Int)

    cmd.Parameters("@ItemId").Value = ItemId

    cmd.Parameters.Add("@Name", Data.SqlDbType.NVarChar, 50)

    cmd.Parameters("@Name").Value = updatedName

    cmd.Parameters.Add("@Description", Data.SqlDbType.NVarChar, 255)

    cmd.Parameters("@Description").Value = updatedDescription

    cmd.Parameters.Add("@Price", Data.SqlDbType.SmallMoney)

    cmd.Parameters("@Price").Value = updatedPrice

    cmd.Parameters.Add("@Quantity", Data.SqlDbType.Int)

    cmd.Parameters("@Quantity").Value = updatedQuantity





    Label1.Text = "Error updating product in database!"



    End Try



    End Sub


    I tried adding a label to my page and then once updatedName had been assigned the value of the textbox I tried assigning that value to the label and it printed out the old product name not the one in the textbox. I can't see anything wrong with the code and it's driving me mad. Anyone got any ideas? 


  • 11 years ago

    hi, i think you miss something, try to see the table where you save the value of the fields you entered if it change anything, if it change anything, then you have put some code on your SP that retrieves data after the UPDATE code that you have.

  • 11 years ago
    The product table is not updating with the new values. I think the problem is to do with the new values from the textbox not being retrieved. As when I assign updatedName to Label1 it displays the old product name value and not the one in the textbox. But I can't see why it wouldn't be bringing back the new textbox values.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie