Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
hi i have been stuck on that problem for last 2 days. i want to get the data from textboxes and on button click i want that data to be inserted in gridview. on every click there shud be a new row created and old row must not be removed. here is my code. whenever i enter the new data and click on button my old row is deleted and new row is stored in place of it. Kindly help me with my that problem thanx.
 

        private void gridVIEWData()
       {
 
           dt1.Columns.Add("pName", typeof(string));
           dt1.Columns.Add("pCategory", typeof(string));
           dt1.Columns.Add("price", typeof(string));
           dt1.Columns.Add("pQuantity", typeof(string));
           dt1.Columns.Add("totalPrice", typeof(string));
       }
       protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
       {
 
       }
 
       protected void TextBox4_TextChanged(object sender, EventArgs e)
       {
 
       }
 
       protected void txt_productCode_TextChanged(object sender, EventArgs e)
       {
 
       }
 
       protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
       {
 
       }
 
protected void Button3_Click(object sender, EventArgs e)
        {
            if (!flag)
            {
                gridVIEWData();
                flag = true;
                Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
                DataRow dr = dt1.NewRow();
                dr["pName"] = DropDownList2.SelectedItem;
                dr["pCategory"] = DropDownList1.SelectedItem;
                dr["price"] = txt_price.Text;
                dr["pQuantity"] = txt_quantity.Text;
                dr["totalPrice"] = total;
                dt1.Rows.Add(dr);
 
                GridView1.DataSource = dt1;
                GridView1.DataBind();
            }
            else if (!IsPostBack)
            {
                Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
                DataRow dr = dt1.NewRow();
                dr["pName"] = DropDownList2.SelectedItem;
                dr["pCategory"] = DropDownList1.SelectedItem;
                dr["price"] = txt_price.Text;
                dr["pQuantity"] = txt_quantity.Text;
                dr["totalPrice"] = total;
                dt1.Rows.Add(dr);
 
                GridView1.DataSource = dt1;
                GridView1.DataBind();
            }
            
            //else if (!IsPostBack)
            //{
            //    Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
            //    DataRow dr = dt1.NewRow();
            //    dr["pName"] = DropDownList2.SelectedItem;
            //    dr["pCategory"] = DropDownList1.SelectedItem;
            //    dr["price"] = txt_price.Text;
            //    dr["pQuantity"] = txt_quantity.Text;
            //    dr["totalPrice"] = total;
            //    dt1.Rows.Add(dr);

            //   GridView1.DataSource = dt1;
            //   GridView1.DataBind();
            //}
            
        }
    } 
Posted 13-Oct-12 5:41am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why this problem happens is Asp.Net cannot persist the changes in DataTable you created between different requested. You need to write code to persist these changes. One solution is to store the data table in a Session variable after adding the new row. Also make sure to retrieve the same data table from this session before adding the next row.
 
The code will look like this;
DataTable dt1=new DataTable();
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               Session["a"] = null;
           }
       }

protected void Button3_Click(object sender, EventArgs e)
{
if  Session["a"]==null)
            {
                gridVIEWData();
                //flag= true;
                Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
                DataRow dr = dt1.NewRow();
                dr["pName"] = DropDownList2.SelectedItem;
                dr["pCategory"] = DropDownList1.SelectedItem;
                dr["price"] = txt_price.Text;
                dr["pQuantity"] = txt_quantity.Text;
                dr["totalPrice"] = total;
                dt1.Rows.Add(dr);
 Session["datas"]=dt1;
                GridView1.DataSource = dt1;
                GridView1.DataBind();
            }
            else             {
                Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
DataTable dtcached = (DataTable)Session["a"];
                DataRow dr = dtcached.NewRow();
                dr["pName"] = DropDownList2.SelectedItem;
                dr["pCategory"] = DropDownList1.SelectedItem;
                dr["price"] = txt_price.Text;
                dr["pQuantity"] = txt_quantity.Text;
                dr["totalPrice"] = total;
DataTable dtCached=(DataTable)Session["datas"];
                dtCached.Rows.Add(dr);
 
                GridView1.DataSource = dtCached;                GridView1.DataBind();
            }
}
Changed code I have shown it in BOLD, Hope this helps.. It is not good to use lot of session variable as it adds more loads to server, but it gives you a hint
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

thanks alot buddy. i have solved it myself. now i m getting problem in deleting a row from it.can anyone help.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 402
1 Nirav Prabtani 258
2 Richard Deeming 250
3 _Amy 195
4 CHill60 175
0 Nirav Prabtani 265
1 OriginalGriff 177
2 _Amy 170
3 Mehdi Gholam 105
4 Maciej Los 90


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100