Hi, please help me i want to insert data in the gridview however the data is not inserted into database and the gridview.
Here is my .aspx:
<asp:GridView ID="gvTetapanJenisKenderaan" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" Width="100%"
CssClass="gridView table table-striped table-bordered" CellPadding="3" Font-Size="Small"
GridLines="Vertical" ShowFooter="True" ShowHeaderWhenEmpty="True" AllowPaging="True" PageSize="4" OnPageIndexChanging="gvTetapanJenisKenderaan_PageIndexChanging"
OnRowCommand="gvTetapanJenisKenderaan_RowCommand"
OnRowEditing="gvTetapanJenisKenderaan_RowEditing"
OnRowCancelingEdit="gvTetapanJenisKenderaan_RowCancelingEdit"
OnRowUpdating="gvTetapanJenisKenderaan_RowUpdating"
OnRowDeleting="gvTetapanJenisKenderaan_RowDeleting" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkCollapse" runat="server" ToolTip="Collapse" ForeColor="Black" Font-Size="14pt"
CommandArgument='<%# Eval("JenamaKenderaanID") %>' OnClick="lnkCollapse_Click"><i class="fa fa-plus"></i>
</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Jenis Buatan / Jenama" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="60%">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="60%"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblJenamaKenderaan" runat="server" Text='<%# Eval("cVehicleMake") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJenamaKenderaan" runat="server" Text='<%# Eval("cVehicleMake") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtJenamaKenderaan_Footer" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tindakan" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkKemaskini" runat="server" ToolTip="Kemaskini" ForeColor="Orange" Font-Size="14pt"
CommandArgument='<%# Eval("JenamaKenderaanID") %>' OnClick="lnkKemaskini_Click"><i class="fas fa-marker"></i>
</asp:LinkButton>
<asp:LinkButton ID="lnkHapus" runat="server" ToolTip="Hapus" ForeColor="Red" Font-Size="14pt"
CommandArgument='<%# Eval("JenamaKenderaanID") %>' OnClick="lnkHapus_Click"><i class="fas fa-trash"></i>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkSimpan" runat="server" ToolTip="Simpan" ForeColor="Green" Font-Size="14pt"
CommandArgument='<%# Eval("JenamaKenderaanID") %>' OnClick="lnkSimpan_Click"><i class="fa fa-save"></i>
</asp:LinkButton>
<asp:LinkButton ID="lnkBatal" runat="server" ToolTip="Batal" ForeColor="Red" Font-Size="14pt"
CommandArgument='<%# Eval("JenamaKenderaanID") %>' OnClick="lnkBatal_Click"><i class="fa fa-close"></i>
</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkTambah" runat="server" ToolTip="Tambah" ForeColor="Blue" Font-Size="14pt"
CommandArgument='<%# Eval("cVehicleMake") %>' OnClick="lnkTambah_Click"><i class="fa fa-plus-square-o"></i>
</asp:LinkButton>
</FooterTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle HorizontalAlign="Center" BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerSettings FirstPageText="First" LastPageText="Last" PageButtonCount="4" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
and behind code :
protected void lnkTambah_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gvTetapanJenisKenderaan.Rows)
{
TextBox txtJenamaKenderaan = (TextBox)row.FindControl("txtJenamaKenderaan");
if (txtJenamaKenderaan != null && !string.IsNullOrEmpty(txtJenamaKenderaan.Text))
{
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO eLoanVehicleMake (cVehicleMake) VALUES (@cVehicleMake)", con);
cmd.Parameters.AddWithValue("@cVehicleMake", txtJenamaKenderaan.Text);
cmd.ExecuteNonQuery();
}
}
}
GVbind();
}