Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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;
            //DropDownList ddl = (DropDownList)row.FindControl("DropDownList1");
            // int catid = Int32.Parse(row.Cells[2].Text);
            //DateTime date = DateTime.Parse(row.Cells[3].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);
            //int dcno =Int32.Parse( row.Cells[4].Text);
            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);
            //DateTime exp = DateTime.Parse(row.Cells[9].Text);
            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))
            {
                //Set the database table name
                sqlBulkCopy.DestinationTableName = "dbo.Tbl_Catheterusagestock";

                //[OPTIONAL]: Map the DataTable columns with that of the database table
                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();
            }
        }
Posted
Updated 4-Feb-15 17:30pm
v3
Comments
Tomas Takac 4-Feb-15 16:45pm    
If I understand you correctly, then rows are inserted with default values. Are you sure the mapping is correct?
Member 11377180 4-Feb-15 22:17pm    
ya database columns are matching
Member 11377180 4-Feb-15 23:30pm    
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 '' ''.

please help with coding..
Tomas Takac 5-Feb-15 1:43am    
You are using TryParse() and don't check the result. Maybe the parse is failing. What about row.Cells[x].Text? Ire the correct values in the grid rows?
Member 11377180 5-Feb-15 8:44am    
row.Cells[x]values also empty.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900