By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

Adding rows to DataGrid across multiple PostBacks

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.