Hi Indradeo,
Please refer below sample.
HTML
<asp:GridView runat="server" ID="gridView1">
</asp:GridView>
<br />
<asp:Button Text="Save" runat="server" OnClick="btn_insert_Click" />
NameSpaces
C#
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Parameters"),
new DataColumn("CO2"),
new DataColumn("NO"),
new DataColumn("SO2"),
new DataColumn("CO"),
new DataColumn("PM"),
new DataColumn("Flow")
});
dt.Rows.Add("UoM", "%", "ppm", "ppm", "ppm", "mg/Nm", "m/s");
string stack1 = File.ReadAllText(Server.MapPath("~/Files/STACK1UNIT1.txt"));
string stack2 = File.ReadAllText(Server.MapPath("~/Files/STACK1UNIT2.txt"));
string[] splitParams = new[] { "CO2", "NO", "SO2", "CO", "PM", "FLOW" };
string[] values = stack1.Split(splitParams, StringSplitOptions.RemoveEmptyEntries);
dt.Rows.Add("UNIT#1", values[0], values[1], values[2], values[3], values[4], values[5]);
values = stack2.Split(splitParams, StringSplitOptions.RemoveEmptyEntries);
dt.Rows.Add("UNIT#2", values[0], values[1], values[2], values[3], values[4], values[5]);
gridView1.DataSource = dt;
gridView1.DataBind();
}
protected void btn_insert_Click(object sender, System.EventArgs e)
{
GridViewRow g1 = gridView1.Rows[1];
string connStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
SqlCommand com = new SqlCommand("insert into unit_one(CO2,NO,SO2,CO,PM,FLOW) values ('" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
g1 = gridView1.Rows[2];
con = new SqlConnection(connStr);
com = new SqlCommand("insert into unit_two(CO2,NO,SO2,CO,PM,FLOW) values ('" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
VB.Net
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Parameters"),
New DataColumn("CO2"),
New DataColumn("NO"),
New DataColumn("SO2"),
New DataColumn("CO"),
New DataColumn("PM"),
New DataColumn("Flow")})
dt.Rows.Add("UoM", "%", "ppm", "ppm", "ppm", "mg/Nm", "m/s")
Dim stack1 As String = File.ReadAllText(Server.MapPath("~/Files/STACK1UNIT1.txt"))
Dim stack2 As String = File.ReadAllText(Server.MapPath("~/Files/STACK1UNIT2.txt"))
Dim splitParams As String() = {"CO2", "NO", "SO2", "CO", "PM", "FLOW"}
Dim values As String() = stack1.Split(splitParams, StringSplitOptions.RemoveEmptyEntries)
dt.Rows.Add("UNIT#1", values(0), values(1), values(2), values(3), values(4), values(5))
values = stack2.Split(splitParams, StringSplitOptions.RemoveEmptyEntries)
dt.Rows.Add("UNIT#2", values(0), values(1), values(2), values(3), values(4), values(5))
gridView1.DataSource = dt
gridView1.DataBind()
End Sub
Protected Sub btn_insert_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim g1 As GridViewRow = gridView1.Rows(1)
Dim connStr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(connStr)
Dim com As SqlCommand = New SqlCommand("insert into unit_one(CO2,NO,SO2,CO,PM,FLOW) values ('" & g1.Cells(1).Text & "','" + g1.Cells(2).Text & "','" + g1.Cells(3).Text & "','" + g1.Cells(4).Text & "','" + g1.Cells(5).Text & "','" + g1.Cells(6).Text & "')", con)
con.Open()
com.ExecuteNonQuery()
con.Close()
g1 = gridView1.Rows(2)
con = New SqlConnection(connStr)
com = New SqlCommand("insert into unit_two(CO2,NO,SO2,CO,PM,FLOW) values ('" & g1.Cells(1).Text & "','" + g1.Cells(2).Text & "','" + g1.Cells(3).Text & "','" + g1.Cells(4).Text & "','" + g1.Cells(5).Text & "','" + g1.Cells(6).Text & "')", con)
con.Open()
com.ExecuteNonQuery()
con.Close()
End Sub