Adding rows to DataGrid across multiple PostBacks

A

Andrew

Hey all,

I am very new to ASP.Net (and .Net in general), but that isn't stopping the
boss from wanting to begin new projects in it. This latest project has me
kinda stumped and after a couple days of struggling, I figure asking you all
(the experts) will keep me from going down some dark and dangerous road.

The project I have is a fairly simple one, in theory anyway. The gist is to
create a page where the user enters an IDNumber, clicks a button, and the
address tied to that ID (based on a SQL Server query) is placed in a
DataGrid. The user can then put in a new IDNumber, click the button again,
and the address tied to the second IDNumber is *added* to the DataGrid. The
user continues this process until they have entered all applicable
IDNumbers, and then clicks an "All Done" button where some other stuff will
happen. My hang-up is with this process loop I just described.

I have made a stored procedure on the SQL Server that will return to me the
address per the supplied IDNumber (a record that is four fields). In
debugging the application I am successful in creating a connection, command
object, and datareader, all to go get the applicable address based on the
IDNumber.

I can populate the DataGrid no problem that first time, but what about the
second time? The third? And so on? If I simply call the
DataGrid.DataBind() method each time after I run to get the address record,
I loose the data from the previous run. I don't care about sort order,
ordering, paging, or things of that nature, I just want to keep from loosing
any data on previous runs to the SQL Server.

So, the question is basically, how do I add a new row of data to a DataGrid
that may or may not contain rows from previous runs to the database, without
loosing any data already in those rows, and have this work over/across
multiple page reloads/postbacks?

Again, I am fairly new to the ASP.Net arena, so I ask for your patience with
your responses, but I am grateful for any and all responses sent. Thanks!!

-- Andrew
 
M

Marshal Antony

Hi Andrew,

As you are new to ASP.NET it is important to understand the Object
Oriented concepts.
I am not explaining about OO concepts here which you can read through or
you might already know.
In your scenario,you are trying to add records to the datagrid first
(not adding to the database or whatever you want to perform
is only after adding information for every Id Number to the datagrid.)
You will need to create a DataTable : The datatable is an in-memory
representation of data.It is like a table in an RDBMS but
not a database table.DataSet is an in-memory cache of data retrieved from a
datasource which can contain a collection of datatables.
Read about it more here :

http://msdn.microsoft.com/library/d...ref/html/frlrfsystemdatadatasetclasstopic.asp
In your particular case,let us create a datatable in your Page_Load
event..
You can build columns and rows for this data table
try :
// declare variables globally

DataTable dt;
DataRow dr ;
In the Page_Load event
create as many columns you needed for the datatable likegiven below.Remember
you are doing this only once when the page is first loaded.that is why
if(!IsPostBack) is used.

if(!IsPostBack)

{
dt=new DataTable["Address"];
DataColumn dc = new DataColumn(); // assign a new column to
datacolumn variable
dc.DataType = System.Type.GetType("System.String"); // get the
type for the datacolumn
dc.ColumnName = "IDNO"; // get the column name for the
datacolumn
dt.Columns.Add(dc);
dc = new DataColumn(); // assign a new column to datacolumn
variable
dc.DataType = System.Type.GetType("System.String"); // get the
type for the datacolumn
dc.ColumnName = "address";
dt.Columns.Add(dc);

// add more columns as per your requirement

Session["Address"]=dt; // Store the data table in a Session variable
for reuse
//To learn about session state implementation :

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspnetsessionstate.asp

}
The next step is to add data to the datatable after executing
the stored procedure and bind the datagrid
On your button click event .
The data you obtained for each column for the ID must be added as
datarow to the datatable dt.
the code in button click is get the data from SP and
dt=(DataTable)Session["Address"];
This means getting the datatable from session.
Then
dr=dt.NewRow(); // new instance of the datarow we declared
globally.
dr["IDNO"]=ID no got from executing stored procedure;
dr["Address"]= Address got from executing the stored procedure
dt.Rows.Add(dr);//add the row to the datatable
Session["Address"]=dt; // Update the session variable with the
datatable with new row.
Finally,
DataGrid.DataSource=dt;
DataGrid.DataBind();

So as we store the datatable in session and bind the
datagrid each time, we can persist the previously added rows.
But be careful using Sessions as it will expire and can consume
memory.Eventhough with this limitation
this may be a solution in your case which you need to evaluate.

Hope this helps,

Marshal Antony
.NET Developer
http://www.dotnetmarshal.com















Andrew said:
Hey all,

I am very new to ASP.Net (and .Net in general), but that isn't stopping the
boss from wanting to begin new projects in it. This latest project has me
kinda stumped and after a couple days of struggling, I figure asking you all
(the experts) will keep me from going down some dark and dangerous road.
tThe project I have is a fairly simple one, in theory anyway. The gist is
to
 
Joined
Feb 29, 2008
Messages
1
Reaction score
0
Add Rows In Gridview Data Disappear

Hello I have a problem i have a page and placed a gridview which is bound to temprarory dataset with one column and a AddRow button.Now what i want is when i press a Addrow Button blank Row should be aadded to Gridview then second then third and as many time as i click the Addrow Button.But the problem is when i Clcik Addrow button a New Row added to Grid now i type somedata and Click Addrow button again the second row Adds to Grid but the data from Frist Row Disappear.Help me in this and give me Simple Code Example that should Work.Thanks in Advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top