0
votes

I have one gridview which has 14 columns in which one of them is for ProductUID and this is invisible and one is textbox which has AutoCompleteExtender.

I used this textbox to select product name. FillGridView function fill gridview of that selected product. But problem is that when I add new product then I first call AddNewRow() function. It added new row on gridview then call fillgridview() function.It filled all columns with selected product's details.

After That when I add another new product on next line then it will lost previous rows only first cell data other columns are remain stored.

What is the problem with my source code?

Please help me!

private void AddNewRow()
    {
        try
        {
            int rowIndex = 0;
            if (Session["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)Session["CurrentTable"];
                DataRow drCurrentRow = null;

                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        string boundfield0 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.ProductID].Text;
                        TextBox TextBoxItemName =
                          (TextBox)gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.ItemName].FindControl("item_txtBox");
                        string boundfield1 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Company].Text;
                        string boundfield2 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Packing].Text;
                        string boundfield3 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Unit].Text;
                        string boundfield4 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Expiry].Text;
                        string boundfield5 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.MRP].Text;
                        string boundfield6 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.ShelfNo].Text;
                        string boundfield7 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.BatchNo].Text;
                        string boundfield8 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Stock].Text;
                        TextBox TextBoxQuantity =
                          (TextBox)gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Quantity].FindControl("qty_txtBox");
                        string boundfield9 = gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Amount].Text;
                        Button ButtonRemove =
                          (Button)gvCounterSale.Rows[rowIndex].Cells[(int)CounterSaleColumnIndex.Button].FindControl("btnRemove");

                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;

                        dtCurrentTable.Rows[i - 1]["ProductID"] = boundfield0;
                        dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxItemName.Text;
                        dtCurrentTable.Rows[i - 1]["Company"] = boundfield1;
                        dtCurrentTable.Rows[i - 1]["Packing"] = boundfield2;
                        dtCurrentTable.Rows[i - 1]["Unit"] = boundfield3;
                        dtCurrentTable.Rows[i - 1]["Expiry"] = boundfield4;
                        dtCurrentTable.Rows[i - 1]["Pur_Rate"] = boundfield5;
                        dtCurrentTable.Rows[i - 1]["Shelf_No"] = boundfield6;
                        dtCurrentTable.Rows[i - 1]["Batch_No"] = boundfield7;
                        dtCurrentTable.Rows[i - 1]["Stock"] = boundfield8;
                        dtCurrentTable.Rows[i - 1]["Col10"] = TextBoxQuantity.Text;
                        dtCurrentTable.Rows[i - 1]["Amount"] = boundfield9;
                        dtCurrentTable.Rows[i - 1]["Col11"] = ButtonRemove;
                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    Session["CurrentTable"] = dtCurrentTable;
                    gvCounterSale.DataSource = dtCurrentTable;
                    gvCounterSale.DataBind();
                }
            }
            else
            {
                Response.Write("Session is null");
            }
            SetPreviousData();
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(Page, this.GetType(), "hi", "alert('Error Occured');", true);
        }
    }

 private void FillGridView()
    {
        try
        {
            Guid puid = new Guid();
            puid = System.Guid.ParseExact(hdnProductId.Value, "D");
            int rowIndex = gvCounterSale.Rows.Count - 1;
            DataTable dtCurrentTable = (DataTable)Session["CurrentTable"];

            string connStr = ConfigurationManager.ConnectionStrings["pharmacy_ConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(connStr);
            con.Open();
            SqlDataReader dr = null;

            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i < gvCounterSale.Rows.Count; i++)
                {
                    if (rowIndex != gvCounterSale.Rows.Count)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append(" SELECT STOCK.PRODUCT_ID, STOCK.BATCH_NO, STOCK.COMPANY_NAME, STOCK.EXPIRY, STOCK.PUR_RATE, STOCK.MRP, STOCK.SHELF_NO, ");
                        sb.Append(" STOCK.OPENING_STOCK, PRODUCT.PRODUCT_ID, PRODUCT.UNIT, PRODUCT.PACKING ");
                        sb.Append(" FROM STOCK INNER JOIN PRODUCT ");
                        sb.Append(" ON STOCK.PRODUCT_ID=PRODUCT.PRODUCT_ID WHERE PRODUCT.PRODUCT_ID = '" + puid + "'GROUP BY STOCK.PRODUCT_ID, STOCK.BATCH_NO, STOCK.COMPANY_NAME, ");
                        sb.Append("STOCK.EXPIRY, STOCK.PUR_RATE, STOCK.MRP, STOCK.SHELF_NO, STOCK.OPENING_STOCK, PRODUCT.PRODUCT_ID, PRODUCT.UNIT, PRODUCT.PACKING ");
                        SqlCommand cmdid = new SqlCommand(sb.ToString(), con);
                        dr = cmdid.ExecuteReader();
                        while (dr.Read())
                        {
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.ProductID].Text = puid.ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.Packing].Text = dr["PACKING"].ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.Unit].Text = dr["UNIT"].ToString();
                            DateTime dt = Convert.ToDateTime(dr["EXPIRY"].ToString());
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.Company].Text = dr["COMPANY_NAME"].ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.Expiry].Text = dt.ToShortDateString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.MRP].Text = dr["MRP"].ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.ShelfNo].Text = dr["SHELF_NO"].ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.BatchNo].Text = dr["BATCH_NO"].ToString();
                            gvCounterSale.Rows[rowIndex - 1].Cells[(int)CounterSaleColumnIndex.Stock].Text = dr["OPENING_STOCK"].ToString();
                        }
                        //rowIndex++;
                        dr.Close();
                    }
                }
                con.Close();
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(Page, this.GetType(), "hi", "alert('Error Occured');", true);
        }
    }
1
If you click anything in asp page it will get refresh so that your data table data might be lost,.. create a class and create a data table property and set your current table to that so when ever you are creating new grid append the old data table with new data table so that your data will remain foreverAravindan Srinivasan
Thanks Arvind. Please explain in detailAkshay Dake
I explained the whole thing Akshay ...Aravindan Srinivasan
Accept the answer ... if it works..Aravindan Srinivasan
What exactly does SetPreviousData() do in AddNewRow() ? Does commenting out that line affect your issue?TheSchmu

1 Answers

0
votes

Here you have to do something

class SessionHepler
         {
              public static DataTable OldTable
                 {
                  get;
                  set;
                  }
            }

// Write this in your code before ----> Session["CurrentTable"] = dtCurrentTable;

SeeionHelper.OldTable = dtCurrentTable;

now you will have the old table value ... so you can assign that to current table

like

if(SeeionHelper.OldTable ==null)
      {
         dtCurrentTable = new DataTable();
        }
 else
   {
      dtCurrentTable = new DataTable();
      dtCurrentTable = SeeionHelper.OldTable;
    }

so First time OldTable will be null it will create then from second time on-wards it will assign OldTable to dtCurrentTable ... try like this