Click here to Skip to main content
15,881,612 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello
sorry for ununderstandable title.
I work on asp.net website in this i need to create dynamic datatable. in this there one button and on button click event i have to add one row in datatabel.at every time button click event fired i have to append one more row in datatable.
For that i do this code.

C#
//Form load event datatable initialize.

    DataTable dt = new DataTable();
    DataRow dtrow;
    protected void Page_Load(object sender, EventArgs e)
    {
        
            dt.Columns.Add("Productid", typeof(string));
            dt.Columns.Add("ProductName", typeof(string));
            dt.Columns.Add("ProductsubName", typeof(string));
            dt.Columns.Add("UnitPrice", typeof(string));
            dt.Columns.Add("Qyt", typeof(string));
            dt.Columns.Add("TotalAmount", typeof(string));
             
        
    }
//now on button click event

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string id = ComboBox2.SelectedItem.Value.ToString();
        dataBind objdata = new dataBind();
        DataTable dtprod= objdata.viewdata("Select * from product_master where sno='"+id+"'");

        dtrow = dt.NewRow();
           // Create New Row
        dtrow["Productid"] = id;            //Bind Data to Columns
        dtrow["ProductName"] = dtprod.Rows[0]["prod_type"];
        dtrow["ProductsubName"] = dtprod.Rows[0]["prod_sub_type"];
        dtrow["UnitPrice"] = dtprod.Rows[0]["rate"];
        dtrow["Qyt"] = txtqty.Text.ToString();
        dtrow["TotalAmount"] = dtprod.Rows[0]["rate"];
        dt.Rows.Add(dtrow);

        GridView1.DataSource = dt; //Bind the gridview with datatable.
        GridView1.DataBind();

}


Now problem is for 1st time button click it's work fine but when i click button on second time datatable gonig to clear and only new data row is inserted. but actually i want to append row in datatable

please help me to solve out this.
Posted
Updated 29-Sep-13 23:37pm
v2

Use Static Keyword for DataTable and DataRow
C#
static DataTable dt = new DataTable();
static DataRow dtrow;


And also use...
C#
protected void Page_Load(object sender, EventArgs e)
{

     if(!page.IsPostBack)
     {
        dt.Columns.Add("Productid", typeof(string));
        dt.Columns.Add("ProductName", typeof(string));
        dt.Columns.Add("ProductsubName", typeof(string));
        dt.Columns.Add("UnitPrice", typeof(string));
        dt.Columns.Add("Qyt", typeof(string));
        dt.Columns.Add("TotalAmount", typeof(string));
     }

}


Hope This Help
 
Share this answer
 
Hi,

Declare the datatable globally and keep the datatable in viewstate.

while every time adding row,add row to datatable of viewstate not to local datatable. between postbacks you will loss the data.

Ex:

DataTable dt=new DataTable()

ViewStatt["OriginalData"]= dt

Did you got it?

Regards,
Kiran.
 
Share this answer
 
Comments
[no name] 30-Sep-13 5:50am    
no i can't get.
Hello there,

First:

Wrap your Page_Load code in
C#
if(!page.IsPostBack)
         {}

Second, you need to keep your DataTable in a Session or ViewState object as in the current scenario every button click will create a new DataTable dt object.
So go like this after this:
C#
DataTable dtprod= objdata.viewdata("Select * from product_master where sno='"+id+"'");
line
C#
DataTable dtObj = null;
            if (Session["YourSessionKey"] != null)
            {
                dtObj= (DataTable) Session["YourSessionKey"];
            }
            else
            {
                dtObj = new DataTable();
            }
and use dtObj in the click event instead of dt and after this line :
C#
dt.Rows.Add(dtrow);
add these statements
C#
dtObj.AcceptChanges();
            Session["YourSessionKey"] = dtObj;


Finally, remove this line DataTable dt = new DataTable(); and give your own name to Session key YourSessionKey.

Hope it helps.
Good luck

Azee...
 
Share this answer
 

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