Hi ashishk,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView runat="server" AutoGenerateColumns="false" ID="GridView1" DataKeyNames="Id">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ID="txtEmail" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Save" runat="server" OnClick="Save" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Populate GridView from Database.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
int id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
string name = row.Cells[0].Text.Trim();
string country = row.Cells[1].Text.Trim();
string email = (row.FindControl("txtEmail") as TextBox).Text.Trim();
Insert(id, name, country, email);
}
}
private void Insert(int id, string name, string country, string email)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
string query = "INSERT INTO Students (Id,Name,Country,Email) VALUES(@Id,@Name,@Country,@Email)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Parameters.AddWithValue("@Email", email);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
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() {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
Dim id As Integer = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value)
Dim name As String = row.Cells(0).Text.Trim()
Dim country As String = row.Cells(1).Text.Trim()
Dim email As String = (TryCast(row.FindControl("txtEmail"), TextBox)).Text.Trim()
Insert(id, name, country, email)
Next
End Sub
Private Sub Insert(ByVal id As Integer, ByVal name As String, ByVal country As String, ByVal email As String)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Dim query As String = "INSERT INTO Students (Id,Name,Country,Email) VALUES(@Id,@Name,@Country,@Email)"
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Id", id)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", country)
cmd.Parameters.AddWithValue("@Email", email)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Screenshot