Saving database data collects from few pages

  • Thread starter Thread starter Mario
  • Start date Start date
M

Mario

I need an advice for a most common and memory efficient method. I collect
data on a three pages (1,2,3 step) and need to save data to database. Is it
ok to open connection and insert data on every page, or to somehow save to
global variables and then save on a last page ?
 
Mario said:
I need an advice for a most common and memory efficient method. I collect
data on a three pages (1,2,3 step) and need to save data to database. Is
it ok to open connection and insert data on every page, or to somehow save
to global variables and then save on a last page ?

Correct me if I'm wrong but in a
Case1: saving data to database, open and close connection - I need to find
out the ID and that may cause some problem if two users inserted in the same
time:

SqlCommand id_cmd = new SqlCommand("select scope_identity() as id",
connection);

Object new_id = id_cmd.ExecuteScalar();

Case 2: I use session variable but it acts on a strange way, sometimes I got
a null without any particular reason.
 
Mario said:
I need an advice for a most common and memory efficient method. I collect
data on a three pages (1,2,3 step) and need to save data to database. Is it
ok to open connection and insert data on every page, or to somehow save to
global variables and then save on a last page ?

You should use an object with public properties to hold the data from
each page and pass the object as a session variable between pages.

If you have some flag in the object that it has indicated the user has
hit the last page, then you can go backwards to the other pages.

Up until the point of hitting the last page, the Save button on the two
pages prior to the last page will have the Save button hidden on the
page. User hits the last page, the indicator in object is set to
indicate last page has been hit, then you unhide the Save button on the
the first two pages so the user can save the data from any page at that
point.

Everything is kept in the session object, and controls for each page are
populated from the object, not using a control's view state --
(disabled).

Of course, the Save button on each page should have the same logic to
validate the object before it's persisted to the database and the same
persist logic.

The Save button on each page being hidden and unhidden is for the
creation process.

If it's the maintenance process, the the Save button is unhidden out the
gate for each page with the first page being show to the user being page #1.

You should look into using a tab control that allows the user to move to
each page effortlessly when the last page is reached in the creation
process, and the tab control is enabled on each page in the maintenancee
process out the gate.
 
Mario said:
Correct me if I'm wrong but in a
Case1: saving data to database, open and close connection - I need to find
out the ID and that may cause some problem if two users inserted in the same
time:
If you had some kind of problem, then a SQL Exception is thrown and
nothing is saved there is no ID to look at.

If you use ADO.NET Transaction using a Trans.Setcomplete after the
successful processing path has completed is a best practice.
SqlCommand id_cmd = new SqlCommand("select scope_identity() as id",
connection);

Object new_id = id_cmd.ExecuteScalar();

The problem of two user inserting at the same time will never happen if
using ADO.NET Transaction and wrapping the whole process in a
transaction is a best practice.
Case 2: I use session variable but it acts on a strange way, sometimes I got
a null without any particular reason.

It's most likely the user sat on a page and did nothing, a Session
Timeout happened and the Session variable is lost for the session. The
variable is dead/null and the session is dead at that point, and the
user has to start over.

If this is a problem, then you need to find a way to save the session
data to a database table, so that if the session has timed-out, you can
go to the database and get the session data and put the session data
back into session to allow the Web application to continue.
 
Mr. Arnold said:
You should use an object with public properties to hold the data from each
page and pass the object as a session variable between pages.

If you have some flag in the object that it has indicated the user has hit
the last page, then you can go backwards to the other pages.

Up until the point of hitting the last page, the Save button on the two
pages prior to the last page will have the Save button hidden on the page.
User hits the last page, the indicator in object is set to indicate last
page has been hit, then you unhide the Save button on the the first two
pages so the user can save the data from any page at that point.

Everything is kept in the session object, and controls for each page are
populated from the object, not using a control's view state --
(disabled).

Of course, the Save button on each page should have the same logic to
validate the object before it's persisted to the database and the same
persist logic.

The Save button on each page being hidden and unhidden is for the creation
process.

If it's the maintenance process, the the Save button is unhidden out the
gate for each page with the first page being show to the user being page
#1.

You should look into using a tab control that allows the user to move to
each page effortlessly when the last page is reached in the creation
process, and the tab control is enabled on each page in the maintenancee
process out the gate.

You give me idea with the Tab control, so I think I that multiview will
solve the problem, to avoid session variables.
Still don't see the purpose of ADO.NET and also LINQ. They are also use
dataset, connection which I use for editing database, so they are to
imaginary terms for me.
Thanks
 
Mario said:
You give me idea with the Tab control, so I think I that multiview will
solve the problem, to avoid session variables.
Still don't see the purpose of ADO.NET and also LINQ. They are also use
dataset, connection which I use for editing database, so they are to
imaginary terms for me.

ADO.NET is the .NET technology that provides the database providers like
(MS SQL server, Oracle etc, etc) to be used to access their database in
a .NET program.

What is ADO.NET?

ADO.NET is a set of computer software components that programmers can
use to access data and data services. It is a part of the base class
library that is included with the Microsoft .NET Framework. It is
commonly used by programmers to access and modify data stored in
relational database systems, though it can also access data in
non-relational sources. ADO.NET is sometimes considered an evolution of
ActiveX Data Objects (ADO) technology, but was changed so extensively
that it can be considered an entirely new product.

http://en.wikipedia.org/wiki/ADO.NET


LINQ is about the following and take note on the word 'object'.

What is Object-oriented-programming?

(OOP) is a programming paradigm that uses "objects" and their
interactions to design applications and computer programs.
The key concepts of OOP are the following:

Class
Object
Instance
Method
Message passing
Inheritance
Abstraction
Encapsulation
Polymorphism
Decoupling

http://en.wikipedia.org/wiki/Object-oriented_programming

No matter what development platform Java, .Net or others OOP is OOP.

http://math.hws.edu/eck/cs124/downloads/OOP2_from_Univ_KwaZulu-Natal.pdf
http://www.blackwasp.co.uk/ObjectOrientedConcepts.aspx


What are design patterns?

Design patterns are recurring solutions to software design problems you
find again and again in real-world application development. Patterns are
about design and interaction of objects, as well as providing a
communication platform concerning elegant, reusable solutions to
commonly encountered programming challenges.

http://www.developer.com/design/article.php/1502691
http://www.dofactory.com/Patterns/Patterns.aspx
http://computerprogramming.suite101.com/article.cfm/patterns_and_antipatterns
http://msdn.microsoft.com/en-us/library/ms954638.aspx
http://www.designpatternsfor.net/Presentations.aspx?tid=3&cid=4


What is Language Integrated Query?

LINQ is a Microsoft .NET Framework component that adds native data
querying capabilities to .NET languages.
Microsoft LINQ defines a set of query operators that can be used to
query, project and filter data in arrays, enumerable classes, XML,
relational database, and third party data sources. While it allows any
data source to be queried, it requires that the data be encapsulated as
objects. So, if the data source does not natively store data as objects,
the data must be mapped to the object domain. Queries written using the
query operators are executed either by the LINQ query processing engine
or, via an extension mechanism, handed over to LINQ providers which
either implement a separate query processing engine or translate to a
different format to be executed on a separate data store (such as on a
database server as SQL queries). The results of a query are returned as
a collection of in-memory objects that can be enumerated using a
standard iterator function such as C#'s foreach.
Many of the concepts that LINQ has introduced were originally tested in
Microsoft's C? research project. LINQ was released as a part of .NET
Framework 3.5 on November 19, 2007.

http://en.wikipedia.org/wiki/Language_Integrated_Query

What is Object Relational Mapping?

(ORM) is a programming technique for converting data between
incompatible type systems in relational databases and object-oriented
programming languages. This creates, in effect, a "virtual object
database," which can be used from within the programming language. There
are both free and commercial packages available that perform
object-relational mapping, although some programmers opt to create their
own ORM tools.

http://en.wikipedia.org/wiki/O-RM


If you're using LINQ, then dump the datasets and use an object or
collections of objects.

LINQ-2-SQL or LINQ-2-Object used with the ADO.NET Entity Framework
allows one to use objects.

What is Linq-to-SQL?

LINQ to SQL, a component of Visual Studio Code Name "Orcas", provides a
run-time infrastructure for managing relational data as objects without
losing the ability to query. It does this by translating
language-integrated queries into SQL for execution by the database, and
then translating the tabular results back into objects you define. Your
application is then free to manipulate the objects while LINQ to SQL
stays in the background tracking your changes automatically.

http://msdn.microsoft.com/en-us/library/bb425822.aspx

What is ADO.NET Entities framework?

ADO.NET Entity Framework is an object-relational mapping (ORM) framework
for the .NET Framework. This framework is Microsoft's first ORM offering
for the .NET Framework. While Microsoft provided objects to manage the
Object-relational impedance mismatch (such as a DataSet).
ADO.NET Entity Framework is included with .NET Framework 3.5 Service
Pack 1 and Visual Studio 2008 Service Pack 1, released on 11 Aug 2008.
It also includes the capability of executing LINQ against ADO.NET Entity
Framework entities

http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework


If you want to put it altogether, then make the $79 investment for yourself.

http://www.dofactory.com/Framework/Framework.aspx
 
Mr. Arnold said:
ADO.NET is the .NET technology that provides the database providers like
(MS SQL server, Oracle etc, etc) to be used to access their database in a
.NET program.

What is ADO.NET?

ADO.NET is a set of computer software components that programmers can use
to access data and data services. It is a part of the base class library
that is included with the Microsoft .NET Framework. It is commonly used by
programmers to access and modify data stored in relational database
systems, though it can also access data in non-relational sources. ADO.NET
is sometimes considered an evolution of ActiveX Data Objects (ADO)
technology, but was changed so extensively that it can be considered an
entirely new product.

http://en.wikipedia.org/wiki/ADO.NET


LINQ is about the following and take note on the word 'object'.

What is Object-oriented-programming?

(OOP) is a programming paradigm that uses "objects" and their interactions
to design applications and computer programs.
The key concepts of OOP are the following:

Class
Object
Instance
Method
Message passing
Inheritance
Abstraction
Encapsulation
Polymorphism
Decoupling

http://en.wikipedia.org/wiki/Object-oriented_programming

No matter what development platform Java, .Net or others OOP is OOP.

http://math.hws.edu/eck/cs124/downloads/OOP2_from_Univ_KwaZulu-Natal.pdf
http://www.blackwasp.co.uk/ObjectOrientedConcepts.aspx


What are design patterns?

Design patterns are recurring solutions to software design problems you
find again and again in real-world application development. Patterns are
about design and interaction of objects, as well as providing a
communication platform concerning elegant, reusable solutions to commonly
encountered programming challenges.

http://www.developer.com/design/article.php/1502691
http://www.dofactory.com/Patterns/Patterns.aspx
http://computerprogramming.suite101.com/article.cfm/patterns_and_antipatterns
http://msdn.microsoft.com/en-us/library/ms954638.aspx
http://www.designpatternsfor.net/Presentations.aspx?tid=3&cid=4


What is Language Integrated Query?

LINQ is a Microsoft .NET Framework component that adds native data
querying capabilities to .NET languages.
Microsoft LINQ defines a set of query operators that can be used to query,
project and filter data in arrays, enumerable classes, XML, relational
database, and third party data sources. While it allows any data source to
be queried, it requires that the data be encapsulated as objects. So, if
the data source does not natively store data as objects, the data must be
mapped to the object domain. Queries written using the query operators are
executed either by the LINQ query processing engine or, via an extension
mechanism, handed over to LINQ providers which either implement a separate
query processing engine or translate to a different format to be executed
on a separate data store (such as on a database server as SQL queries).
The results of a query are returned as a collection of in-memory objects
that can be enumerated using a standard iterator function such as C#'s
foreach.
Many of the concepts that LINQ has introduced were originally tested in
Microsoft's C? research project. LINQ was released as a part of .NET
Framework 3.5 on November 19, 2007.

http://en.wikipedia.org/wiki/Language_Integrated_Query

What is Object Relational Mapping?

(ORM) is a programming technique for converting data between incompatible
type systems in relational databases and object-oriented programming
languages. This creates, in effect, a "virtual object database," which can
be used from within the programming language. There are both free and
commercial packages available that perform object-relational mapping,
although some programmers opt to create their own ORM tools.

http://en.wikipedia.org/wiki/O-RM


If you're using LINQ, then dump the datasets and use an object or
collections of objects.

LINQ-2-SQL or LINQ-2-Object used with the ADO.NET Entity Framework allows
one to use objects.

What is Linq-to-SQL?

LINQ to SQL, a component of Visual Studio Code Name "Orcas", provides a
run-time infrastructure for managing relational data as objects without
losing the ability to query. It does this by translating
language-integrated queries into SQL for execution by the database, and
then translating the tabular results back into objects you define. Your
application is then free to manipulate the objects while LINQ to SQL stays
in the background tracking your changes automatically.

http://msdn.microsoft.com/en-us/library/bb425822.aspx

What is ADO.NET Entities framework?

ADO.NET Entity Framework is an object-relational mapping (ORM) framework
for the .NET Framework. This framework is Microsoft's first ORM offering
for the .NET Framework. While Microsoft provided objects to manage the
Object-relational impedance mismatch (such as a DataSet).
ADO.NET Entity Framework is included with .NET Framework 3.5 Service Pack
1 and Visual Studio 2008 Service Pack 1, released on 11 Aug 2008. It also
includes the capability of executing LINQ against ADO.NET Entity Framework
entities

http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework


If you want to put it altogether, then make the $79 investment for
yourself.

http://www.dofactory.com/Framework/Framework.aspx
Thank you, I will read more about ADO.NET. Until now, I can't figure the
practical advantage of ADO if you consider the way how I update/insert
database in my web application:
//string sql =
// "INSERT [dbo].[OsobniPodaci] ([ime], [ime0], [grad],
[posta], [oib], [nosac], [boja])" +
// " VALUES (@ime, @ime0, @grad, @posta, @oib, @nosac,
@boja)";
//using (
//SqlConnection myConnection = new
SqlConnection(ConfigurationManager.ConnectionStrings["BazaConnectionString"].ConnectionString))
//{
// SqlCommand myCommand = new SqlCommand(sql, myConnection);

// //Upis osobnih podataka u bazu
// //Koristim sesion varijablu
// //ovi sa znakom @ su u gornjem stringu sql, kako se ne bi
ponavljao
// myCommand.Parameters.Add(new SqlParameter("@ime",
// (string)Session["ime"]));
// myCommand.Parameters.Add(new SqlParameter("@ime0",
// (string)Session["ime0"]));
// myCommand.Parameters.Add(new SqlParameter("@grad",
// (string)Session["grad"]));
// myCommand.Parameters.Add(new SqlParameter("@posta",
// (string)Session["posta"]));
// myCommand.Parameters.Add(new SqlParameter("@oib",
// (string)Session["oib"]));
// //Upis nosaca i boje otiska u bazu
// myCommand.Parameters.Add(new SqlParameter("@nosac",
// (string)Session["nosac"]));
// myCommand.Parameters.Add(new SqlParameter("@boja",
// (string)Session["boja"]));


// myConnection.Open();
// myCommand.ExecuteNonQuery();

// //uzmi ID reda, odnosno IDNarudzbe
// //myCommand = new SqlCommand("SET @IDNarudzbe =
CAST(SCOPE_IDENTITY() AS INT)",
// // myConnection);
// //myCommand.ExecuteNonQuery();

// myConnection.Close();
 
Mario said:
Thank you, I will read more about ADO.NET. Until now, I can't figure the
practical advantage of ADO if you consider the way how I update/insert
database in my web application:

Well, if you use Linq-2-SQL or ADO.NET Entity Framework, it would do it
for you with the CRUD operations, which they still both use ADO.NET and
T-SQL behind the scene.

And there is no practical advantage with ADO.NET, it's a requirement a
must, because without it, you can do nothing with a database -- period.

If you're unhappy about using ADO.NET SQL Command Object, then you
should look at the two solutions above.
 
Back
Top