Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have created one form with 3 textboxes product name,qty,price and one button add.
when i click on add button i have grid and 3 textboxes values should be inserted in grid and it is getting inserted.
now i want to add different values which i again insert in textboxes but now it shows me current values in grid and previous values are not retaining...
i have tried following code.
pls tell where to add sessions so that i can retain previous values too.

C#
public partial class invoice : System.Web.UI.Page
{
  
    public SqlDataReader reader;
    Query.product objprdt = new product(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());
    Query.customer objcus = new customer(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());

    Query.cal objcal = new cal(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());
    DataTable dt1 = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        
        DataTable dt = new DataTable();
        dt = objprdt.getproducts();
        ddlprdcts.DataSource = dt;
        ddlprdcts.DataValueField = "Product_Id";
        ddlprdcts.DataTextField = "Product_Name";
        ddlprdcts.DataBind();
        ddlprdcts.Items.Add(new ListItem("- Please Select -", "-1"));
        ddlprdcts.SelectedValue = "-1";



     
        DataColumn dc1 = new DataColumn("Product Name");
        DataColumn dc2 = new DataColumn("Qty");
         DataColumn dc3 = new DataColumn("Product Price");
        DataColumn dc4= new DataColumn("Amount");
        
        dt1.Columns.Add(dc1);
        dt1.Columns.Add(dc2);
         dt1.Columns.Add(dc3);
        dt1.Columns.Add(dc4);
       
        DataRow dr1 = dt.NewRow();
        GridView1.DataSource = dt1;
        GridView1.DataBind();

    }



C#
protected void Button2_Click(object sender, EventArgs e)
   {
       DataRow dr1 = dt1.NewRow();
       dr1[0] = TextBox2.Text;
       dr1[1] = TextBox4.Text;
       dr1[2] =TextBox3.Text;
       dr1[3] =TextBox5.Text;
       dt1.Rows.Add(dr1);
       GridView1.DataSource = dt1;
       GridView1.DataBind();


   }

where to use session so that i can add as many values grid should retain all
NOTE: I dnt want to save into database
pls tell
regards
Posted
Updated 6-Feb-13 19:55pm
v2

Try the following code

protected void Button2_Click(object sender, EventArgs e)
   {
       if(Session["dt1"] != null)
       {
          dt1 = (DataTable)Session["dt1"];
       }

       DataRow dr1 = dt1.NewRow();
       dr1[0] = TextBox2.Text;
       dr1[1] = TextBox4.Text;
       dr1[2] =TextBox3.Text;
       dr1[3] =TextBox5.Text;
       dt1.Rows.Add(dr1);
       GridView1.DataSource = dt1;
       GridView1.DataBind();

       Session["dt1"] = dt1;

   }
 
Share this answer
 
Comments
shivani 2013 7-Feb-13 2:13am    
it is showing only current values in place of previous .....first row is only getting affected.....
pls tell
thanks
vishal.shimpi 7-Feb-13 4:55am    
hi shivani,add the given code it will surely solve your problem

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//write your page load code here
Session["dt1"] = dt1;
}
}
shivani 2013 12-Feb-13 6:00am    
thanks.i got it.........
hi try the following code

C#
Button_Click()
{
    DataTable dt = new DataTable();
    DataRow dr;
    dt.Columns.Add("Name");
    dt.Columns.Add("Address");
    dt.Columns.Add("Number");
    //First fill all the date present in the grid
    for (int intCnt = 0; intCnt < grd.Rows.Count; intCnt ++)
    {
        if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
        {
        dr = dt.NewRow();
        dr["Name"] = grd.Rows[intCnt].Cells[0].Value;
        dr["Address"] = grd.Rows[intCnt].Cells[1].Value;
        dr["Number"] = grd.Rows[intCnt].Cells[2].Value;
        dt.Rows.Add(dr);
        }
    }
    dr = dt.NewRow();
    dr["Name"] = txt1.Text;
    dr["Address"] = txt2.Text;
    dr["Number"] = txt3.Text;
    dt.Rows.Add(dr);
    grd.DataSource = dt;
    grd.DataBind();
}

Please make corrections if I made any syntactic mistake because I wrote this code directly in the notepad
 
Share this answer
 
v2
Comments
shivani 2013 7-Feb-13 2:19am    
in grid no data is filled..i need to fill the data through textboxes then how can i tale loop till grd.Rows.Count???????????
shivani 2013 7-Feb-13 2:53am    
thanks i got it.......
[no name] 8-Feb-13 1:29am    
your welcome

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