Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to store multiple Records into my DataTable but it's replaced the existing one. This is the code i wrote .

Can anyone help me to do this....

C#
if (e.CommandName == "Add")
{
    GridView grid = (GridView)sender;

    DropDownList ddlMaterial = (DropDownList)grid.FooterRow.FindControl("ddlMaterial_Type");

    TextBox txtMaterialQty = (TextBox)grid.FooterRow.FindControl("txtMaterialQty");
    
   //I think pblm is on here why bcz everytime i call empty DataTable.But i 
  //Don't  no how to over come this issue
    DataTable dt1 = new DataTable(); 
   dt1.Columns.Add("DED_Name", typeof(string));
    dt1.Columns.Add("Quantity", typeof(string));

    DataRow dr;
    dr = dt.NewRow();

    dr["DED_Name"] = ddlMaterial.SelectedItem.ToString();
    dr["Quantity"] = txtMaterialQty.Text.ToString();
            
    dt1.Rows.Add(dr);
    Msgbox1.alert(dt1.Rows.Count.ToString());
           
    Bind_DocumentNo();
}


Thanks in advance...
Posted
Updated 27-Sep-12 19:51pm
v3

1 solution

this wrong because every time u creating new table that is why it replacing the data

u should create table only once and store in session
ON PAGE LOAD EVENT

if(!IsPostBack)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("DED_Name", typeof(string));
dt1.Columns.Add("Quantity", typeof(string));
session["table"]=dt1;

}

if (e.CommandName == "Add")
{
GridView grid = (GridView)sender;

DropDownList ddlMaterial = (DropDownList)grid.FooterRow.FindControl("ddlMaterial_Type");

TextBox txtMaterialQty = (TextBox)grid.FooterRow.FindControl("txtMaterialQty");


DataTable dt1 = session["table"] as DataTable ;
dt1.Rows.Add(ddlMaterial.SelectedItem.ToString(), txtMaterialQty.Text.Trim());
session["table"]=dt1;
// u can bind your grid here or u can call your bind function but remember if u r calling your bind function then u should bind your grid with session table .

Bind_DocumentNo();
}


Hope this will help u .
 
Share this answer
 
Comments
[no name] 28-Sep-12 4:59am    
Thanks for ur Reply i'm also solve my Problem like u sent.

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