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);
}
}