473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3249
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 maintenance of data. I use separate logic to delete...
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 user is currently on page 3 of 8, and if I have...
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. Thanks... (i gave up on the web form TABLE since it...
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, but I am not quite sure how to do that. Is that...
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 rebind the control in every postback. S then I started...
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 database. It works great! I have added a column, via...
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 is contained in an ASPX page. I haven't been...
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 overides OnMouseDown private DataTable dt = new...
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 be members of many teams (e.g. the hockey team,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.