im selecting rows from gridview and on clicking a button it must copy data to datatable. in database rows are getting incremented
i used breakpoints and checked the values..they are same as getting copied to database. int fields store 0, datetime fields store 01-01-0001 value, and string fields null values. and row.cells for all fields is '' ''.
.pls suggest changes in code ..thanku
protected void btnGetUsedStock_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[9] { new DataColumn("CatheterCat", typeof(string)),
new DataColumn("CatID", typeof(int)),
new DataColumn("Date",typeof(DateTime)),
new DataColumn("DC No", typeof(int)),
new DataColumn("Company", typeof(string)),
new DataColumn("Name", typeof(string)),
new DataColumn("Size", typeof(string)),
new DataColumn("Batch", typeof(string)),
new DataColumn("Expiry",typeof(DateTime)),});
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("chkSelect") as CheckBox).Checked)
{
string dd1 = row.Cells[1].Text;
int catid;
int.TryParse(row.Cells[2].Text, out catid);
DateTime date;
DateTime.TryParse(row.Cells[3].Text, out date);
int dcno;
int.TryParse(row.Cells[4].Text, out dcno);
string company = row.Cells[5].Text;
string name = row.Cells[6].Text;
string size = row.Cells[7].Text;
string batch = row.Cells[8].Text;
DateTime exp;
DateTime.TryParse(row.Cells[9].Text, out exp);
dt.Rows.Add(dd1, catid, date.Date,dcno, company, name, size, batch, exp.Date);
}
}
if (dt.Rows.Count > 0)
{
string consString = WebConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
sqlBulkCopy.DestinationTableName = "dbo.Tbl_Catheterusagestock";
sqlBulkCopy.ColumnMappings.Add("CatheterCat", "CathCat");
sqlBulkCopy.ColumnMappings.Add("CatID", "CathID");
sqlBulkCopy.ColumnMappings.Add("Date", "Date");
sqlBulkCopy.ColumnMappings.Add("DC No", "DCNo");
sqlBulkCopy.ColumnMappings.Add("Company", "Company");
sqlBulkCopy.ColumnMappings.Add("Name", "Name");
sqlBulkCopy.ColumnMappings.Add("Size", "Size");
sqlBulkCopy.ColumnMappings.Add("Batch", "Batch");
sqlBulkCopy.ColumnMappings.Add("Expiry", "Expiry");
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}