Gridview not updating

asp.net , db Bulgaria
  • 12 years ago

    I'm trying to update fields in the GridView it goes into Edit mode and I update the field but when I click Update the fields or the database don't update and I don't get any error message. My Asp.Net code is:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" ForeColor="Black" GridLines="Vertical" Width="100%" PageSize="2" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px">

    <Columns>

    <asp:TemplateField HeaderText="Item">

    <EditItemTemplate>

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

    </EditItemTemplate>

    <ItemTemplate>

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

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Price">

    <EditItemTemplate>

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

    </EditItemTemplate>

    <ItemTemplate>

    <asp:Label ID="Price" runat="server" Text='<%# Bind("price") %>'></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:ButtonField CommandName="Select" Text="Select" />

    <asp:CommandField ShowEditButton="True" />

    <asp:CommandField ShowDeleteButton="True" />

    </Columns>

    </asp:GridView>

     

    Then in my VB code I have this method to update the selected product:

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

    Dim itemname As TextBox = GridView1.Rows(e.RowIndex).FindControl("ItemNameTextBox")

    Dim price As TextBox = GridView1.Rows(e.RowIndex).FindControl("PriceTextBox")

    Dim quantity As TextBox = GridView1.Rows(e.RowIndex).FindControl("QuantityTextBox")

    Dim ItemId As Integer = GridView1.DataKeys(e.RowIndex).Item(0)

    Dim updatedItemName As String = itemname.Text

    Dim updatedPrice As Decimal = price.Text

    Dim updatedQuantity As Integer = quantity.Text

    conn = New SqlConnection(strConn)

    cmd = New SqlCommand("Update tblitem set itemname = @ItemName, price = @Price, stock= @Quantity where itemid = @ItemId", conn)

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

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

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

    cmd.Parameters("@ItemName").Value = updatedItemName

    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()

    Finally

    conn.Close()

    End Try

    GridView1.EditIndex = -1

    GetProducts()

    End Sub

    Any ideas where I'm going wrong?

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.

“The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!' but 'That's funny...'” - Isaac Asimov