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.TextDim updatedPrice As Decimal = price.Text
Dim updatedQuantity As Integer = quantity.Textconn = 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 = ItemIdcmd.Parameters.Add("@ItemName", Data.SqlDbType.NVarChar, 255)
cmd.Parameters(
"@ItemName").Value = updatedItemNamecmd.Parameters.Add("@Price", Data.SqlDbType.SmallMoney)
cmd.Parameters(
"@Price").Value = updatedPricecmd.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?
Enter your message below
Sign in or Join us (it's free).