makumbi says:
cmd.Parameters.AddWithValue(
"@CustomerId"
, row.Cells(11).Controls.OfType(Of TextBox)().FirstOrDefault().Text)
Change with below. TextBox should be LinkButton.
cmd.Parameters.AddWithValue("@CustomerId", row.Cells(11).Controls.OfType(Of LinkButton)().FirstOrDefault().Text)
Please refer below sample.
Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.
HTML
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="Gvgridclient" runat="server" AutoGenerateColumns="False" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="ItemCode">
<ItemTemplate>
<asp:TextBox ID="txtItemCode" runat="server" Text='<%# Bind("ItemCode") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ItemDescription" HeaderText="ItemDescription" />
<asp:BoundField DataField="ItemSize" HeaderText="ItemSize" />
<asp:TemplateField HeaderText="Quantity">
<EditItemTemplate>
<asp:TextBox ID="Quantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="Fqty" runat="server" Width="46px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" AutoPostBack="true" Text='<%# Eval("Quantity") %>' Width="46px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="OrderPrice" HeaderText="OrderPrice" />
<asp:TemplateField HeaderText="SalePrice">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("SalePrice") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("SalePrice") %>' Visible="False"></asp:Label>
<asp:TextBox ID="txtSalePrice" runat="server" Text='<%# Eval("SalePrice") %>' Width="85px" Height="16px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SubTotal">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFsubtotal" runat="server" Width="46px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblsubtotal" runat="server" Text='<%# Bind("SubTotal") %>'></asp:Label>
<asp:TextBox ID="txtsubtotal" runat="server" Height="16px" Text='<%# Eval("SubTotal") %>' Width="75px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Prno" HeaderText="Prno" />
<asp:BoundField DataField="Names" HeaderText="Names" />
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:TemplateField HeaderText="DelSingle">
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Key">
<ItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" OnClick="Update"></asp:LinkButton>
<%-- <asp:TextBox ID="Classcode" runat="server" Height="16px" Visible="True" Text='<%# Bind("Keyfield") %>' Width="54px"></asp:TextBox>--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[12] {
new DataColumn("ItemCode"),
new DataColumn("ItemDescription"),
new DataColumn("ItemSize"),
new DataColumn("Quantity"),
new DataColumn("OrderPrice"),
new DataColumn("SalePrice"),
new DataColumn("SubTotal"),
new DataColumn("Prno"),
new DataColumn("Names"),
new DataColumn("Date"),
new DataColumn("DelSingle"),
new DataColumn("Key")});
dt.Rows.Add("012", "Item 1", "5cm", "40", "10", "450", "4510", "04215", "Excel", "10/04/2022", "");
dt.Rows.Add("013", "Item 2", "6cm", "70", "80", "820", "5361", "0258", "Word", "04/05/2022", "");
dt.Rows.Add("014", "Item 3", "4cm", "210", "59", "890", "8468", "85642", "Text", "18/06/2022", "");
dt.Rows.Add("015", "Item 4", "9cm", "440", "70", "40", "8120", "47258", "Items", "20/07/2022", "");
dt.Rows.Add("016", "Item 5", "7cm", "450", "80", "50", "9812", "69140", "Excel", "31/08/2022", "");
Gvgridclient.DataSource = dt;
Gvgridclient.DataBind();
}
}
protected void Update(object sender, EventArgs e)
{
foreach (GridViewRow row in Gvgridclient.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
if (isChecked)
{
SqlCommand cmd = new SqlCommand("UPDATE [order] SET qty = @qty, Subtotal = @subtotal, saleprice=@saleprice, itemcode=@svalue WHERE keycode = @CustomerId");
cmd.Parameters.AddWithValue("@qty", row.Cells[4].Controls.OfType<TextBox>().FirstOrDefault().Text);
cmd.Parameters.AddWithValue("@subtotal", row.Cells[6].Controls.OfType<TextBox>().FirstOrDefault().Text);
cmd.Parameters.AddWithValue("@saleprice", row.Cells[5].Controls.OfType<TextBox>().FirstOrDefault().Text);
cmd.Parameters.AddWithValue("@svalue", row.Cells[1].Controls.OfType<TextBox>().FirstOrDefault().Text);
string kk = row.Cells[11].Controls.OfType<LinkButton>().FirstOrDefault().Text;
cmd.Parameters.AddWithValue("@CustomerId", row.Cells[11].Controls.OfType<LinkButton>().FirstOrDefault().Text);
cmd.ExecuteNonQuery();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(11) {New DataColumn("ItemCode"), New DataColumn("ItemDescription"), New DataColumn("ItemSize"), New DataColumn("Quantity"), New DataColumn("OrderPrice"), New DataColumn("SalePrice"), New DataColumn("SubTotal"), New DataColumn("Prno"), New DataColumn("Names"), New DataColumn("Date"), New DataColumn("DelSingle"), New DataColumn("Key")})
dt.Rows.Add("012", "Item 1", "5cm", "40", "10", "450", "4510", "04215", "Excel", "10/04/2022", "")
dt.Rows.Add("013", "Item 2", "6cm", "70", "80", "820", "5361", "0258", "Word", "04/05/2022", "")
dt.Rows.Add("014", "Item 3", "4cm", "210", "59", "890", "8468", "85642", "Text", "18/06/2022", "")
dt.Rows.Add("015", "Item 4", "9cm", "440", "70", "40", "8120", "47258", "Items", "20/07/2022", "")
dt.Rows.Add("016", "Item 5", "7cm", "450", "80", "50", "9812", "69140", "Excel", "31/08/2022", "")
Gvgridclient.DataSource = dt
Gvgridclient.DataBind()
End If
End Sub
Protected Sub Update(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In Gvgridclient.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
If isChecked Then
Dim cmd As SqlCommand = New SqlCommand("UPDATE [order] SET qty = @qty, Subtotal = @subtotal, saleprice=@saleprice, itemcode=@svalue WHERE keycode = @CustomerId")
cmd.Parameters.AddWithValue("@qty", row.Cells(4).Controls.OfType(Of TextBox)().FirstOrDefault().Text)
cmd.Parameters.AddWithValue("@subtotal", row.Cells(6).Controls.OfType(Of TextBox)().FirstOrDefault().Text)
cmd.Parameters.AddWithValue("@saleprice", row.Cells(5).Controls.OfType(Of TextBox)().FirstOrDefault().Text)
cmd.Parameters.AddWithValue("@svalue", row.Cells(1).Controls.OfType(Of TextBox)().FirstOrDefault().Text)
Dim kk As String = row.Cells(11).Controls.OfType(Of LinkButton)().FirstOrDefault().Text
cmd.Parameters.AddWithValue("@CustomerId", row.Cells(11).Controls.OfType(Of LinkButton)().FirstOrDefault().Text)
cmd.ExecuteNonQuery()
End If
End If
Next
End Sub
Screenshot