Hi maideen,
Please refer below sample,
HTML
<asp:GridView ID="gvMODetails_TYPE" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="docno" HeaderText="docno" />
<asp:BoundField DataField="docdate" HeaderText="docdate" />
<asp:BoundField DataField="PubDate" HeaderText="PubDate" />
<asp:BoundField DataField="mainsection" HeaderText="mainsection" />
<asp:BoundField DataField="subsection" HeaderText="subsection" />
<asp:BoundField DataField="position" HeaderText="position" />
<asp:BoundField DataField="pagenumber" HeaderText="pagenumber" />
<asp:BoundField DataField="SizeCode" HeaderText="SizeCode" />
<asp:BoundField DataField="Remarks" HeaderText="Remarks" />
<asp:BoundField DataField="CreateOn" HeaderText="CreateOn" />
</Columns>
</asp:GridView>
<asp:Button ID="btnInsert" runat="server" Text="Add" OnClick="OnInsert" />
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
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[10] {new DataColumn("docno", typeof(string)),
new DataColumn("docdate", typeof(DateTime)),
new DataColumn("PubDate", typeof(DateTime)),
new DataColumn("mainsection", typeof(string)),
new DataColumn("subsection", typeof(string)),
new DataColumn("position", typeof(string)),
new DataColumn("pagenumber", typeof(decimal)),
new DataColumn("SizeCode", typeof(string)),
new DataColumn("Remarks", typeof(string)),
new DataColumn("CreateOn", typeof(DateTime))});
dt.Rows.Add("abc", "2016-07-02 19:23:19.000", "2016-07-02 19:23:19.000", "asd", "fss", "dff", "0.5", "jf", "GG", "2016-07-02 19:23:19.000");
dt.Rows.Add("abc", "2016-07-02 19:23:19.000", "2016-07-02 19:23:19.000", "asd", "fss", "jf", "0.5", "gfsd", "GG", "2016-07-02 19:23:19.000");
dt.Rows.Add("shj", "2016-07-02 19:23:19.000", "2016-07-02 19:23:19.000", "asd", "fss", "dff", "0.5", "gfsd", "GG", "2016-07-02 19:23:19.000");
gvMODetails_TYPE.DataSource = dt;
gvMODetails_TYPE.DataBind();
}
}
protected void OnInsert(object sender, EventArgs e)
{
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[10] {new DataColumn("docno", typeof(string)),
new DataColumn("docdate", typeof(DateTime)),
new DataColumn("PubDate", typeof(DateTime)),
new DataColumn("mainsection", typeof(string)),
new DataColumn("subsection", typeof(string)),
new DataColumn("position", typeof(string)),
new DataColumn("pagenumber", typeof(decimal)),
new DataColumn("SizeCode", typeof(string)),
new DataColumn("Remarks", typeof(string)),
new DataColumn("CreateOn", typeof(DateTime))});
foreach (GridViewRow row in gvMODetails_TYPE.Rows)
{
if ((row.FindControl("CheckBox1") as CheckBox).Checked)
{
string docno = row.Cells[1].Text;
DateTime docdate = DateTime.Parse(row.Cells[2].Text);
DateTime PubDate = DateTime.Parse(row.Cells[3].Text);
string mainsection = row.Cells[4].Text;
string subsection = row.Cells[5].Text;
string position = row.Cells[5].Text;
decimal pagenumber = decimal.Parse(row.Cells[7].Text);
string SizeCode = row.Cells[8].Text;
string Remarks = row.Cells[9].Text;
DateTime CreateOn = DateTime.Parse(row.Cells[10].Text);
dt2.Rows.Add(docno, docdate, PubDate, mainsection, subsection, position, pagenumber, SizeCode, Remarks, CreateOn);
}
}
if (dt2.Rows.Count > 0)
{
string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlCommand cmd = new SqlCommand("Insert_MODetails", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@tblMODetails", dt2);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
VB.Net
Class SurroundingClass
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(9) {New DataColumn("docno", GetType(String)), New DataColumn("docdate", GetType(DateTime)), New DataColumn("PubDate", GetType(DateTime)), New DataColumn("mainsection", GetType(String)), New DataColumn("subsection", GetType(String)), New DataColumn("position", GetType(String)), New DataColumn("pagenumber", GetType(Decimal)), New DataColumn("SizeCode", GetType(String)), New DataColumn("Remarks", GetType(String)), New DataColumn("CreateOn", GetType(DateTime))})
dt.Rows.Add("abc", "2016-07-02 19:23:19.000", "2016-07-02 19:23:19.000", "asd", "fss", "dff", "0.5", "jf", "GG", "2016-07-02 19:23:19.000")
dt.Rows.Add("abc", "2016-07-02 19:23:19.000", "2016-07-02 19:23:19.000", "asd", "fss", "jf", "0.5", "gfsd", "GG", "2016-07-02 19:23:19.000")
dt.Rows.Add("shj", "2016-07-02 19:23:19.000", "2016-07-02 19:23:19.000", "asd", "fss", "dff", "0.5", "gfsd", "GG", "2016-07-02 19:23:19.000")
gvMODetails_TYPE.DataSource = dt
gvMODetails_TYPE.DataBind()
End If
End Sub
Protected Sub OnInsert(ByVal sender As Object, ByVal e As EventArgs)
Dim dt2 As DataTable = New DataTable()
dt2.Columns.AddRange(New DataColumn(9) {New DataColumn("docno", GetType(String)), New DataColumn("docdate", GetType(DateTime)), New DataColumn("PubDate", GetType(DateTime)), New DataColumn("mainsection", GetType(String)), New DataColumn("subsection", GetType(String)), New DataColumn("position", GetType(String)), New DataColumn("pagenumber", GetType(Decimal)), New DataColumn("SizeCode", GetType(String)), New DataColumn("Remarks", GetType(String)), New DataColumn("CreateOn", GetType(DateTime))})
For Each row As GridViewRow In gvMODetails_TYPE.Rows
If (TryCast(row.FindControl("CheckBox1"), CheckBox)).Checked Then
Dim docno As String = row.Cells(1).Text
Dim docdate As DateTime = DateTime.Parse(row.Cells(2).Text)
Dim PubDate As DateTime = DateTime.Parse(row.Cells(3).Text)
Dim mainsection As String = row.Cells(4).Text
Dim subsection As String = row.Cells(5).Text
Dim position As String = row.Cells(5).Text
Dim pagenumber As Decimal = Decimal.Parse(row.Cells(7).Text)
Dim SizeCode As String = row.Cells(8).Text
Dim Remarks As String = row.Cells(9).Text
Dim CreateOn As DateTime = DateTime.Parse(row.Cells(10).Text)
dt2.Rows.Add(docno, docdate, PubDate, mainsection, subsection, position, pagenumber, SizeCode, Remarks, CreateOn)
End If
Next
If dt2.Rows.Count > 0 Then
Dim consString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(consString)
Using cmd As SqlCommand = New SqlCommand("Insert_MODetails", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@tblMODetails", dt2)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End If
End Sub
End Class
Inserted Record
DocNo |
DocDate |
PubDate |
MainSection |
SubSection |
Position |
PageNumber |
SizeCode |
Remarks |
CreateOn |
shj |
23:19.0 |
23:19.0 |
asd |
fss |
fss |
1 |
gfsd |
GG |
23:19.0 |