Click here to Skip to main content
12,396,461 members (33,936 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET .NET C#4.0 .NET4
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.

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


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 6-Feb-13 18:38pm
Updated 6-Feb-13 19:55pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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;
 
   }
  Permalink  
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.........
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

hi try the following code

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
  Permalink  
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.......
CShubham 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2016
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