Hi smile,
Please refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="CustomerId">
<HeaderTemplate>
Id
<br />
<asp:TextBox runat="server" Text="" ID="txtId" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%#Eval("CustomerId") %>' ID="lblId" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<HeaderTemplate>
Name
<br />
<asp:TextBox runat="server" Text="" ID="txtName" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%#Eval("Name") %>' ID="lblName" runat="server" />s
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<HeaderTemplate>
Country
<br />
<asp:TextBox runat="server" Text="" ID="txtCountry" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%#Eval("Country") %>' ID="lblCountry" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button Text="Insert" runat="server" OnClick="Insert" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT * FROM TestDemo", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
protected void Insert(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string customerid = (gvCustomers.HeaderRow.FindControl("txtId") as TextBox).Text;
string name = (gvCustomers.HeaderRow.FindControl("txtName") as TextBox).Text;
string country = (gvCustomers.HeaderRow.FindControl("txtCountry") as TextBox).Text;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("INSERT INTO TestDemo VALUES(@customerid, @name, @country)", con);
cmd.Parameters.AddWithValue("@customerid", customerid);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@country", country);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM TestDemo", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim customerid As String = (TryCast(gvCustomers.HeaderRow.FindControl("txtId"), TextBox)).Text
Dim name As String = (TryCast(gvCustomers.HeaderRow.FindControl("txtName"), TextBox)).Text
Dim country As String = (TryCast(gvCustomers.HeaderRow.FindControl("txtCountry"), TextBox)).Text
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("INSERT INTO TestDemo VALUES(@customerid, @name, @country)", con)
cmd.Parameters.AddWithValue("@customerid", customerid)
cmd.Parameters.AddWithValue("@name", name)
cmd.Parameters.AddWithValue("@country", country)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
BindGrid()
End Sub
Screenshot