Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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....
 
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 27-Sep-12 19:39pm
Edited 27-Sep-12 20:51pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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

  Print Answers RSS
0 Peter Leow 315
1 Mika Wendelius 275
2 CHill60 190
3 TheRealSteveJudge 184
4 Abhinav S 170
0 Sergey Alexandrovich Kryukov 8,613
1 OriginalGriff 6,546
2 Peter Leow 3,897
3 Zoltán Zörgő 3,456
4 Richard MacCutchan 2,472


Advertise | Privacy | Mobile
Web01 | 2.8.150123.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2015
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