DetailsView Updating With Old Values

asp.net , db Bulgaria
  • 10 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">

    <Fields>

    <asp:TemplateField HeaderText="Product Name">

    <EditItemTemplate>

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

    </EditItemTemplate>

     

    <ItemTemplate>

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

    </ItemTemplate>

    </asp:TemplateField>

     

    <asp:TemplateField HeaderText="Description">

    <EditItemTemplate>

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

    </EditItemTemplate>

     

    <ItemTemplate>

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

    </ItemTemplate>

    </asp:TemplateField>

     

    <asp:TemplateField HeaderText="Price">

    <EditItemTemplate>

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

    </EditItemTemplate>

     

    <ItemTemplate>

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

    </ItemTemplate>

    </asp:TemplateField>

     

    <asp:TemplateField HeaderText="Quantity">

    <EditItemTemplate>

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

    </EditItemTemplate>

     

    <ItemTemplate>

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

    </ItemTemplate>

    </asp:TemplateField>

    <asp:CommandField ShowEditButton="True"/>

    </Fields>

    <HeaderTemplate>

    <%#Eval ("itemname") %>

    </HeaderTemplate>

    </asp:DetailsView>

    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

    Try

    conn.Open()

    cmd.ExecuteNonQuery()

    Catch

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

    Finally

    conn.Close()

    End Try

    DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)

    GetProductDetails()

    End Sub

     

    I tried adding a label to my asp.net 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? 

Post a reply

No one has replied yet! Why not be the first?

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

Contribute

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.

“Every language has an optimization operator. In C++ that operator is ‘//’”