Carry Data Table value one place to another place, in windows Application - c#

I have text box control & a button, After putting value in text box & Click on button data should be added in data table.
The problem which i face is, when i added new record, previous record are gone,& new record are come in place of old record.
I come to know, what i use in Windows Application to carry data to one place to another. [viewstate] which we use in WebApplication, But what to do for window application
In this code, it add only one record in the table, when i try to add 2nd record it make replace the old one.
My code are:
public void Getdatatable(int srno, string Name, string address, int contactno,double amount,string Emailid)
{
try
{
dt = new DataTable();
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("SrNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("address", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("ContactNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("amount", typeof(double)));
dt.Columns.Add(new System.Data.DataColumn("Emailid", typeof(string)));
dr = dt.NewRow();
dr[0] = srno;
dr[1] = Name;
dr[2] = address;
dr[3] = contactno;
dr[4] = amount;
dr[5] = Emailid;
dt.Rows.Add(dr);
}
catch(Exception ex)
{}
}
private void btnadd_Click(object sender, EventArgs e)
{
srno =Convert.ToInt32(txtsrno.Text);
Name = txtname.Text;
address = txtaddress.Text;
contactno =Convert.ToInt32(txtcontact.Text);
amount =Convert.ToDouble(txtamount.Text);
Emailid = txtemail.Text;
Getdatatable(srno, Name, address, contactno, amount, Emailid);
//dt = ds.Tables[0].Rows[0][0];
foreach (DataRow dr in dt.Rows)
{
dr[0] = txtsrno.Text;
dr[1] = txtname.Text;
dr[2] = txtaddress.Text;
dr[3] = txtcontact.Text;
dr[4] = txtamount.Text;
dr[5] = txtemail.Text;
}
dt.AcceptChanges();
grd.DataSource = dt;
txtsrno.Text = "";
txtname.Text = "";
txtaddress.Text = "";
txtcontact.Text = "";
txtamount.Text = "";
txtemail.Text = "";
}

Well... Your Getdatatable method is creating a new table.
Since you are calling this method in your Click callback, the table is created anew each time. Therefore you only get the last element added.
You should create your table once and remove the Getdatatable(srno, Name, address, contactno, amount, Emailid); call from your button callback.

You could declare DataTable dt as global variable and add columns to it once when form is loaded.
private DataTable dt = null;
private void CreateTable()
{
dt = new DataTable();
dt.Columns.Add(new System.Data.DataColumn("SrNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("address", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("ContactNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("amount", typeof(double)));
dt.Columns.Add(new System.Data.DataColumn("Emailid", typeof(string)));
}
private void Form1_Load(object sender, EventArgs e)
{
CreateTable();
}
After the CreateTable method was called, you will have data table with columns you want and you don't need to call this method again when you try to add data row to the data table.
Here is the button click event handler
private void ClearTextBox()
{
txtsrno.Text = "";
txtname.Text = "";
txtaddress.Text = "";
txtcontact.Text = "";
txtamount.Text = "";
txtemail.Text = "";
}
private void btnadd_Click(object sender, EventArgs e)
{
try
{
srno = Convert.ToInt32(txtsrno.Text);
Name = txtname.Text;
address = txtaddress.Text;
contactno = Convert.ToInt32(txtcontact.Text);
amount = Convert.ToDouble(txtamount.Text);
Emailid = txtemail.Text;
DataRow dr = dt.NewRow();
dr[0] = srno;
dr[1] = Name;
dr[2] = address;
dr[3] = contactno;
dr[4] = amount;
dr[5] = Emailid;
dt.Rows.Add(dr);
dt.AcceptChanges();
grd.DataSource = dt;
ClearTextBox();
}
catch (Exception ex) { /*Handle Exception*/ }
}
Otherwise, you don't need to have those TextBoxes and button for add data to data table. You just code like this, and enter data in DataGridView cell directly (like when you edit excel file).
private DataTable dt = null;
private void CreateTable()
{
dt = new DataTable();
dt.Columns.Add(new System.Data.DataColumn("SrNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("address", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("ContactNo", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("amount", typeof(double)));
dt.Columns.Add(new System.Data.DataColumn("Emailid", typeof(string)));
grd.DataSource = dt;
}
private void Form1_Load(object sender, EventArgs e)
{
CreateTable();
}

Related

Set More than one Initial row in datatable

The below function set only one Initial row.
How can i make 10 initial rows for example.
Any Suggestions?
Function
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Sr.No", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dr = dt.NewRow();
dr["Sr.No"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
griditem.DataSource = dt;
griditem.DataBind();
}
try this..
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Sr.No", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
for (int i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr["Sr.No"] = (i +1);
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
}
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
griditem.DataSource = dt;
griditem.DataBind();
}
this may be?
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["Sr.No"] = i + 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
}
You could create your new row in a for loop. Something along the lines of the following:
for (var i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr["Sr.No"] = i + 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
}
This will then give you 10 rows in your dt object, and assign the Sr.No value to the index + 1 of the loop, i.e. 1,2...10
this could also be the option
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn { AutoIncrement=true,AutoIncrementSeed=1,AutoIncrementStep=1,ColumnName="SrNo",DataType=typeof(int)});
dt.Columns.Add(new DataColumn { ColumnName = "Column1", DataType = typeof(string) });
dt.Columns.Add(new DataColumn { ColumnName = "Column2", DataType = typeof(string) });
dt.Columns.Add(new DataColumn { ColumnName = "Column3", DataType = typeof(string) });
dt.Columns.Add(new DataColumn { ColumnName = "Column4", DataType = typeof(string) });
for(int i=0;i<10;i++)
dt.Rows.Add(dt.NewRow());

Bind Rows From GridView to DataTable and Display It In Another GridView RSS

I am going to find a value from a column in a gridview. If there are rows that have this value in that column, I will add those rows to a DataTable. Afterwards I will bind it to another gridview to display those rows out. However, when I tried to do this, columns that are in INT cannot be displayed out in INT but can only be displayed out in TEXT. Also, only 1 row is being binded to the datatable and displayed out in another gridview, I want many rows if many rows have the particular value in that column, not just 1 row.
This is the code I used:
public void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("DATE"));
dt.Columns.Add(new DataColumn("CODE"));
dt.Columns.Add(new DataColumn("PERSON_NAME"));
dt.Columns.Add(new DataColumn("STATUS"));
dt.Columns.Add(new DataColumn("HOBBIES"));
dt.Columns.Add(new DataColumn("SCORE"));
dt.Columns.Add(new DataColumn("ITEM"));
dt.Columns.Add(new DataColumn("QUANTITY"));
dt.Columns.Add(new DataColumn("TYPE"));
dt.Columns.Add(new DataColumn("RATING"));
dt.Columns.Add(new DataColumn("PRICE"));
foreach (GridViewRow gvr in GridView1.Rows)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "STATUS")) == "Regular")
{
dr = dt.NewRow();
dr["DATE"] = e.Row.Cells[0].Text;
dr["CODE"] = e.Row.Cells[1].Text;
dr["PERSON_NAME"] = e.Row.Cells[2].Text;
dr["STATUS"] = e.Row.Cells[3].Text;
dr["HOBBIES"] = e.Row.Cells[4].Text;
dr["SCORE"] = e.Row.Cells[5].Text;
dr["ITEM"] = e.Row.Cells[6].Text;
dr["QUANTITY"] = e.Row.Cells[7].Text;
dr["TYPE"] = e.Row.Cells[8].Text;
dr["RATING"] = e.Row.Cells[9].Text;
dr["PRICE"] = e.Row.Cells[10].Text;
dt.Rows.Add(dr);
GridView2.DataSource = dt;
GridView2.DataBind();
}
Can someone please help me on this? Thanks a lot!!
You should use GridView OnDataBound Event instead of RowDataBound. RowDataBound will be triggered after each row is bound to the Gridview where as OnDataBound will be called after all the rows are bound to the Gridview. Try the following
public void GridView1_OnDataBound(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("DATE"));
dt.Columns.Add(new DataColumn("CODE"));
dt.Columns.Add(new DataColumn("PERSON_NAME"));
dt.Columns.Add(new DataColumn("STATUS"));
dt.Columns.Add(new DataColumn("HOBBIES"));
dt.Columns.Add(new DataColumn("SCORE"));
dt.Columns.Add(new DataColumn("ITEM"));
dt.Columns.Add(new DataColumn("QUANTITY"));
dt.Columns.Add(new DataColumn("TYPE"));
dt.Columns.Add(new DataColumn("RATING"));
dt.Columns.Add(new DataColumn("PRICE"));
foreach (GridViewRow gvr in GridView1.Rows)
{
if (gvr.Cells[3].Text == "Regular")
{
dr = dt.NewRow();
dr["DATE"] = gvr.Cells[0].Text;
dr["CODE"] = gvr.Cells[1].Text;
dr["PERSON_NAME"] = gvr.Cells[2].Text;
dr["STATUS"] = gvr.Cells[3].Text;
dr["HOBBIES"] = gvr.Cells[4].Text;
dr["SCORE"] = gvr.Cells[5].Text;
dr["ITEM"] = gvr.Cells[6].Text;
dr["QUANTITY"] = gvr.Cells[7].Text;
dr["TYPE"] = gvr.Cells[8].Text;
dr["RATING"] = gvr.Cells[9].Text;
dr["PRICE"] = gvr.Cells[10].Text;
dt.Rows.Add(dr);
}
}
GridView2.DataSource = dt;
GridView2.DataBind();
}

Deleting dynamic gridview

I have a page that dynamically creates gridviews on button click which that works fine. And along with it creating a grid, on the top of the grid there is a linkbutton to delete it. Now, what i am trying to accomplish is that when i delete one of the grids it will delete the grid from the page and resync the numbers. Meaning, lets say i have the following:
Grid1
Grid2
Grid3
Grid4
If i decide to delete Grid3, what actually happens is this:
Grid1
Grid2
Grid4
And then if i click something on the page for it to reload or something, then it will resync the numbers correctly:
Grid1
Grid2
Grid3
I have no idea why its doing that and why it doesn't do it this way in the first place. Can someone please help me with this?
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
string ctrlname = this.Page.Request.Params.Get("__EVENTTARGET");
if (Session["Tables"] == null)
{
Session.Add("Tables", 1);
DataTable dt = GetTable();
Session.Add(Session["Tables"].ToString(), dt);
GridView gv = new GridView();
gv.ID = "Grid-" + Session["Tables"].ToString();
gv.DataSource = dt;
gv.DataBind();
LinkButton lb = new LinkButton();
lb.ID = "Delete-Grid-" + Session["Tables"].ToString();
lb.Text = "Delete Grid " + Session["Tables"].ToString();
lb.Click += new EventHandler(DeleteGrid);
PlaceHolder1.Controls.Add(lb);
PlaceHolder1.Controls.Add(gv);
}
if (IsPostBack)
{
PlaceHolder1.Controls.Clear();
int next = (int)Session["Tables"];
for (int i = 1; i <= (int)Session["Tables"]; i++)
{
GridView gv = new GridView();
gv.ID = "Grid-" + i.ToString();
gv.DataSource = (DataTable)Session[i];
gv.DataBind();
LinkButton lb = new LinkButton();
lb.ID = "Delete-Grid-" + i.ToString();
lb.Text = "Delete Grid " + i.ToString();
lb.Click += new EventHandler(DeleteGrid);
PlaceHolder1.Controls.Add(lb);
PlaceHolder1.Controls.Add(gv);
}
if (ctrlname == "Button1")
{
next = next + 1;
Session["Tables"] = next;
DataTable dt = GetTable();
Session.Add(Session["Tables"].ToString(), dt);
GridView gv = new GridView();
gv.ID = "Grid-" + next.ToString();
gv.DataSource = (DataTable)Session[next];
gv.DataBind();
LinkButton lb = new LinkButton();
lb.ID = "Delete-Grid-" + next.ToString();
lb.Text = "Delete Grid " + next.ToString();
lb.Click += new EventHandler(DeleteGrid);
PlaceHolder1.Controls.Add(lb);
PlaceHolder1.Controls.Add(gv);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void DeleteGrid(object sender, EventArgs e)
{
LinkButton gridLink = (LinkButton)sender;
String gridNum = gridLink.ID.ToString().Split('-').Last();
GridView grid = (GridView)this.Page.FindControl("Grid-" + gridNum);
LinkButton lbd = (LinkButton)this.Page.FindControl("Delete-Grid-" + gridNum);
PlaceHolder1.Controls.Remove(grid);
PlaceHolder1.Controls.Remove(lbd);
int next = (int)Session["Tables"];
next = next - 1;
Session.Add("Tables", next);
Session.Remove(gridNum);
}
public DataTable GetTable()
{
//
// Here we create a DataTable with a few columns.
//
// Create Datatable to store all colums
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Size", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
dt.Columns.Add(new DataColumn("Unit", typeof(string)));
dt.Columns.Add(new DataColumn("Duration", typeof(string)));
dt.Columns.Add(new DataColumn("DurationType", typeof(string)));
dt.Columns.Add(new DataColumn("Amount", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Size"] = string.Empty;
dr["Description"] = string.Empty;
dr["Quantity"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Duration"] = string.Empty;
dr["DurationType"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 2;
dr["Size"] = string.Empty;
dr["Description"] = string.Empty;
dr["Quantity"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Duration"] = string.Empty;
dr["DurationType"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 3;
dr["Size"] = string.Empty;
dr["Description"] = string.Empty;
dr["Quantity"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Duration"] = string.Empty;
dr["DurationType"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 4;
dr["Size"] = string.Empty;
dr["Description"] = string.Empty;
dr["Quantity"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Duration"] = string.Empty;
dr["DurationType"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["RowNumber"] = 5;
dr["Size"] = string.Empty;
dr["Description"] = string.Empty;
dr["Quantity"] = string.Empty;
dr["Unit"] = string.Empty;
dr["Duration"] = string.Empty;
dr["DurationType"] = string.Empty;
dr["Amount"] = string.Empty;
dt.Rows.Add(dr);
return dt;
}
}
you need to know the life cycle of an asp.net page first.
please refer to this article...
in your case, the delete even is firing after page load event.
therefore the sync is happening before the row is deleted.
may be you need to put the sync code in another method and call it after deletion...

dynamic gridview column autocalculation

I have a Gridview, with textboxes in templatefields, where i dynamically add columns to it.
protected void grid()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("SerialNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Material", typeof(string)));
dt.Columns.Add(new DataColumn("Bags", typeof(string)));
dt.Columns.Add(new DataColumn("GrossWt", typeof(string)));
dt.Columns.Add(new DataColumn("TareWt", typeof(string)));
dt.Columns.Add(new DataColumn("NetWt", typeof(string)));
dt.Columns.Add(new DataColumn("BillWt", typeof(string)));
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
dr["SerialNumber"] = 1;
GridView1.DataSource = dt;
GridView1.DataBind();
}
I need to have the NetWt column to have autocalulated values using the GrossWt and TareWt columns. The formula should be (grosswt-tarewt)/1000. But i dont know how to go about it. Any ideas??
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[5] = (e.Row.Cells[3].Text - e.Row.Cells[4].Text)/1000
}
}
Try this
Assuming the user edits the values, you can add your own handler to the CellValidated event and then do whatever you like with the values:
myDataGridView.CellValidated += new System.Windows.Forms.DataGridViewCellEventHandler(myDataGridView_CellValidated);
[...]
private void myGridView_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == myGridView.Columns["GrossWt"].Index){
myGridView.Rows[e.Index].Cells["NewWT"].Value = (
myGridView.Rows[e.Index].Cells["GrossWT"].Value -
myGridView.Rows[e.Index].Cells["TareWT"].Value ) /1000;
}
}

How to add gridview rows to a datatable?

I have a gridview which will contain some 'n' number of rows.... Now i want to add all rows of the gridview to a datatable which will be used for bulkcopy operation...
I have found this http://www.codeproject.com/KB/aspnet/GridView_To_DataTable.aspx
But i want all columns of my gridview to be added to the datarow of the datatable
Grid http://img85.imageshack.us/img85/4044/gridp.jpg
I want to convert gridview to datatable on submit.... Any suggestion...
EDIT:
Answer below works and i have found an answer too...
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("EmpId", typeof(Int64)));
dt.Columns.Add(new DataColumn("FromDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("ToDate", typeof(DateTime)));
dt.Columns.Add(new DataColumn("DaysPresent", typeof(double)));
dt.Columns.Add(new DataColumn("OpeningAdvance", typeof(double)));
dt.Columns.Add(new DataColumn("AdvanceDeducted", typeof(double)));
dt.Columns.Add(new DataColumn("RemainingAdvance", typeof(double)));
dt.Columns.Add(new DataColumn("SalaryGiven", typeof(double)));
dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
foreach (GridViewRow row in gridEmployee.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
DataRow dr = dt.NewRow();
dr["EmpId"] = Convert.ToInt64(((HiddenField)row.Cells[0].FindControl("HiddenId")).Value);
dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
dr["DaysPresent"] = Convert.ToDouble(((TextBox)row.Cells[3].FindControl("TxtDaysPresent")).Text);
dr["OpeningAdvance"] = Convert.ToDouble(((TextBox)row.Cells[4].FindControl("txtOpeningAdv")).Text);
dr["AdvanceDeducted"] = Convert.ToDouble(((TextBox)row.Cells[5].FindControl("TxtAdvanceDeducted")).Text);
dr["RemainingAdvance"] = Convert.ToDouble(((TextBox)row.Cells[6].FindControl("TxtClosingAdvance")).Text);
dr["SalaryGiven"] = Convert.ToDouble(((TextBox)row.Cells[7].FindControl("TxtSalary")).Text);
dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
dt.Rows.Add(dr);
}
}
SqlBulkCopy sbc = new SqlBulkCopy(connectionString);
sbc.DestinationTableName = "SalaryDetails";
sbc.ColumnMappings.Add("EmpId", "EmpId");
sbc.ColumnMappings.Add("FromDate", "FromDate");
sbc.ColumnMappings.Add("ToDate", "ToDate");
sbc.ColumnMappings.Add("DaysPresent", "DaysPresent");
sbc.ColumnMappings.Add("OpeningAdvance", "OpeningAdvance");
sbc.ColumnMappings.Add("AdvanceDeducted", "AdvanceDeducted");
sbc.ColumnMappings.Add("RemainingAdvance", "RemainingAdvance");
sbc.ColumnMappings.Add("SalaryGiven", "SalaryGiven");
sbc.ColumnMappings.Add("CreatedDate", "CreatedDate");
sbc.WriteToServer(dt);
sbc.Close();
you can traverse datagrid row by row and make a comma separated file. then use Bulk insert or bcp for inserting data to db.
Another Solution
DataTable dt = new DataTable();
for (int j = 0; j < grdList.Rows.Count; j++)
{
DataRow dr;
GridViewRow row = grdList.Rows[j];
dr = dt.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text;
}
dt.Rows.Add(dr);
}
SqlBulkCopy sbc = new SqlBulkCopy(targetConnStr);
sbc.DestinationTableName = "yourDestinationTable";
sbc.WriteToServer(dt);
sbc.Close();

Categories