GridView not Updating

asp.net , db Bulgaria
  • 11 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?

  • 11 years ago

    Hey katy,

    Looks like the "Try" will be eliminating the possibility of an error message. If you have a "Catch" to go with it.... Try will hide an error that occurs within it from the user.

    Take a look here for some a useful example that I think may help:

    http://aspalliance.com/147_Try__Catch__Finally_in_ASPNET

    John

Post a reply

Enter your message below

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.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski