472,337 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 software developers and data experts.

Adding rows to DataGrid across multiple PostBacks

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
Nov 18 '05 #1
1 3112
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/de...classtopic.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/de...ssionstate.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" <An********@removethis-hotmail.com> wrote in message
news:Oa**************@TK2MSFTNGP12.phx.gbl...
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 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

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Clayton Hamilton | last post by:
I have a DataGrid on a webform bound to a Datasource and can successfully use <ItemTemplate> to create edit/update/cancel functionality for user...
3
by: Carolyn Vo | last post by:
I have a datagrid in my web control class that I am trying to get the current rows displayed for. I have enabled paging on the datagrid so if the...
4
by: Michelle Stone | last post by:
I want to programatically add rows to a datagrid that is bounded. How can I do this? The ITEMS property doesn't seem to have an ADD function. ...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer,...
2
by: andla | last post by:
Hi, How does events fire in a datagrid. I know about the problem if turning the viewstate off the events wil not fire properly even if I...
2
by: Bob Hollness | last post by:
Hi group. I am a newbie to ASP.NET as you will see from some of the questions I may ask! I have a datagrid which I have populated from a...
1
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which...
2
by: Flack | last post by:
Hey guys, I have a DataGrid and DataTable field in my class : private ImageDataGrid dataGrid1; //ImageDataGrid extends dataGrid and just...
2
by: Mark Rae | last post by:
Hi, Looking for some advice again... Imagine two ListBox controls denoting something like students and team membership e.g. many students can...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.