473,722 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.DataBi nd() 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 3288
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.Get Type("System.St ring"); // 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.Get Type("System.St ring"); // 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)S ession["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.DataSo urce=dt;
DataGrid.DataBi nd();

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.Eventhou gh 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********@rem ovethis-hotmail.com> wrote in message
news:Oa******** ******@TK2MSFTN GP12.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.DataBi nd() 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
3638
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 a row. Everything works just fine. BUT I would like to add a button to (for example) the DataGrid header, which when pressed will add a new row to the datagrid. This should then allow the user to enter information into text boxes (in some...
3
2264
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 allowed for the table to show only 5 rows at a time, for example, I want to get the 5 rows that are displayed on page 3 of 8. How do I do this???? Thanks!
4
4726
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 keeps losing rows during postbacks)
3
4879
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 the best method? Do you have a sample of how to do this?
2
2385
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 to think how does .Net do it if I turn the viewstat on. If you use the Request.Form then you can get all the controls tha
2
2436
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 the Columns dialog box from the properties of the datagrid, on the left that contains a select button. So now, when you press the button the whole row is highlighted. Now, what I want to do is have the user highlight as many rows as they wish...
1
3352
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 able to find any examples which demonstrate how to do this. My Code:
2
3566
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 DataTable("MyTable"); In the classes constructor, after InitializeComponent() I call the following
2
3617
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, the football team, the athletics team etc). Team membership is managed on a standard web page with two ListBox controls - the one on the left shows which team(s) the student is not a
0
8739
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9238
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.