Monday, 18 March 2013

Multiple data Insert from gridview using datatable in Asp.Net


C# Code
  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                  CreateDataTable();
            }
        }

     private void CreateDataTable()
        {
            try
            {
                dt = new DataTable();
                dt.Columns.Add("Barcode", typeof(string));
                dt.Columns.Add("Quantity", typeof(int));
                dt.Columns.Add("Category_id", typeof(int));
                dt.Columns.Add("Item_id", typeof(int));
               
                ViewState["tabledata"] = dt;
            }
            catch (Exception ex)
            {
            }

        }
//Grid re Add Heba
 protected void BtnAdd_Click(object sender, EventArgs e)
        {
            if (Checkwhenaddgrid() == true)
            {
                dt = new DataTable();
                dt = (DataTable)ViewState["tabledata"];
                DataRow dr;
                if (flag == 0)
                {
                    dr = dt.NewRow();
                    dr[0] = TxtBarcode.Text;
                    dr[1] = TxtQuantity.Text;
                    dr[2] = ViewState["cataid"];
                    dr[3] = ViewState["ItemID"];
                    dt.Rows.Add(dr);
                }
                else if (flag == 1)
                {
                     dt.Rows[rowno][0] = TxtBarcode.Text;
                     dt.Rows[rowno][1] = TxtQuantity.Text;
                    dt.Rows[rowno][2] = ViewState["cataid"];
                    dt.Rows[rowno][3] = ViewState["ItemID"];
                }
                ViewState["tabledata"] = dt;
                TxtBarcode.Text = "";
                TxtQuantity.Text = "";
                FillGrid();
                foreach (GridViewRow dgv in GrdViewKit.Rows)
                {
                    int a = GrdViewKit.Rows.Count;
                    TxttotalNoOfIems.Text = Convert.ToString(a);
                }
            }
        }
 protected void BtnSave_Click(object sender, EventArgs e)
        {
            save_single();
        }
  private void save_single()
        {
            if (checkData() == true)
            {
                CreateDataTable();
                CreateDataTable_Insert();
                TAGGING_IssueToPackagingAndKitting objIssueQC = new TAGGING_IssueToPackagingAndKitting();
                objIssueQC.Issue_To = int.Parse(DDlIssueTo.SelectedValue.ToString());
                objIssueQC.Expected_Receive_Date = Convert.ToDateTime(TxtIssueDate.Text);
                objIssueQC.Issue_Date_Packing = Convert.ToDateTime(TxtExpectedReceiveDate.Text);
                objIssueQC.Toatl_Qty_of_Items = int.Parse(TxttotalNoOfIems.Text);
                objIssueQC.Toatl_Gross_Weight = 0;
                objIssueQC.Toatl_Stone_Weight = 0;
                objIssueQC.Toatl_Net_Weight = 0;
                objIssueQC.Created_By = 1;
                objIssueQC.ItemDetailsPacking = dt;
                issueNo = ERPManagement.GetInstance.InsertPackingAndKitting(objIssueQC);
                Message("Data saved successfully." + Environment.NewLine + "Issue to pack single no. :" + issueNo);//, "KenCloud", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
 private void CreateDataTable_Insert()
        {
            try
            {
                foreach (GridViewRow gvr in GrdViewKit.Rows)
                {
                    if (GrdViewKit.Rows.Count > 0)
                    {
                        DataRow dr = dt.NewRow();
                        dr["Barcode"] = gvr.Cells[0].Text;
                        dr["Quantity"] = gvr.Cells[1].Text;
                        int Category_id = Convert.ToInt32(GrdViewKit.DataKeys[gvr.RowIndex].Values[0]);
                        int Item_ID = Convert.ToInt32(GrdViewKit.DataKeys[gvr.RowIndex].Values[1]);
                        dr["Category_id"] = Category_id;
                        dr["Item_ID"] = Item_ID;                      
                        dt.Rows.Add(dr);
                        dt.AcceptChanges();
                    }
                }

            }
            catch (Exception ex)
            {
            }

        }

No comments:

Post a Comment