Hi got the following problem, it's more a programming problem then really a
database problem. I have a Database class which connects to a mySQL database on
the network. In WebForm1.aspx i connect, using the Database class, to this
database. Works fine. In
Webform1.aspx i have the main view (SELECT * FROM) of the database. In
Webform2.aspx i want to see certain details. So when i click on a button in
Webform1.aspx i go to Webform2.aspx.
In webform2.aspx i want to use the same (open) connection. What happens now is
that the connection is closed and made over again, this is needed because else
the Class Database is not known in WebForm2.aspx. (The Database class is the
class where i initiate the database)
My question is how can i use the Database class without initiating it again in
WebForm2.aspx ??
This is my Database Class
using System;
using System.Data;
using ByteFX.Data.MySQLClient;
using System.Collections;
namespace webAPP
{
public class Database
{
private MySQLConnection con;
private string server, database, user, password;
public Database(string server, string database, string user, string
password)
{
this.server = server;
this.database = database;
this.user = user;
this.password = password;
}
public void Connect()
{
string connectionString = "Server="+server+
";Database="+database+";User ID="+user+";Password="+password+";";
try
{
con = new MySQLConnection(connectionString);
con.Open();
if(con!=null)
Console.WriteLine("succes");
}
catch(Exception e)
{
throw new Exception(e.Message);
}
}
public void Close()
{
if(con!=null) con.Close();
}
public string[] getValue(string value)
{
String[] arrValues = null;
MySQLDataAdapter adapter = new MySQLDataAdapter("SELECT * from pc where
testnr =" + value, con);
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable table = ds.Tables[0];
ArrayList kolomWaarden = new ArrayList();
foreach (DataRow row in table.Rows)
{
arrValues = new String[table.Columns.Count];
for (int i=0; i<table.Columns.Count; i++)
arrValues[i] = Convert.ToString(row[i]);
}
return arrValues;
}
}
}
In WebForm1.aspx i have this code to connect to the database:
private void openGb()
{
try
{
if(gb != null)
gb.Close();
gb = new Database("servername", "databasenaam", "username", "password");
//initiate database class
gb.Connect();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
In WebForm2.aspx i declare the Database Class by saying
Database gb;
In the page_load i then want to run the SQL-Query in gb.getValue( ) But i then
get a Object reference not found error because the Database Class is not
instantiated in WebForm2.aspx. That is like: gb = new Database("servername",
"databasenaam", "username", "password");
Is there a way to prevent this ?? 4 1546
<ciach>
Use Session objects
pozdrawiam
Przemek Sulikowski
<ciach>
Use Session objects
pozdrawiam
Przemek Sulikowski
It should also be said that performing database operations in this way
is a very bad idea. Generally speaking, when you want to connect to a
database, you should open the connection, use it, and then close it. If you
are changing contexts like this (i.e. moving from page to page), you
definitely should close the connection. If something goes wrong during the
transition, then the open connection is left hanging, which is definitely
another downside.
However, this doesn't mean that you can't store the result of the query
(a DataSet) somewhere (like the session).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com
"Selvin" <selvin_nospam@nospam_osadkowski.com.pl> wrote in message
news:bk**********@news.onet.pl... <ciach>
Use Session objects
pozdrawiam
Przemek Sulikowski
Hi Reinier,
You can do several things, one of then is keep the instance of DataBase
class in a session variable , this is not a very good solution as you will
have several open connection opened at the same time ( one per session )
this greatly affect the performance and scalability of your solution.
another solution would be make the DataBase class a singleton, doing this
will assure you that only one instance of the class exist in your
application so all the request will use the same connection to the DB.
Any of the above forms solve your problem of reuse the same instance in
webform2.aspx page .
Now to finish, I would suggest you to take a look at the connection
pooling capabilities of ADo.NET and the SQLServer provider: http://msdn.microsoft.com/library/de...taprovider.asp
there you will see that you don;t have to worry for this, as the provider
provide a pooling feature that handle this situation for you.
Remember you should ALWAYS close the connection AS EARLY AS POSSIBLE.
Hope this help,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Reinier Beeckman" <rj*********@hotmail.com> wrote in message
news:uc**************@TK2MSFTNGP10.phx.gbl... Hi got the following problem, it's more a programming problem then really
a database problem. I have a Database class which connects to a mySQL
database on the network. In WebForm1.aspx i connect, using the Database class, to this database. Works fine. In Webform1.aspx i have the main view (SELECT * FROM) of the database. In Webform2.aspx i want to see certain details. So when i click on a button
in Webform1.aspx i go to Webform2.aspx.
In webform2.aspx i want to use the same (open) connection. What happens
now is that the connection is closed and made over again, this is needed because
else the Class Database is not known in WebForm2.aspx. (The Database class is
the class where i initiate the database)
My question is how can i use the Database class without initiating it
again in WebForm2.aspx ??
This is my Database Class
using System; using System.Data; using ByteFX.Data.MySQLClient; using System.Collections; namespace webAPP {
public class Database { private MySQLConnection con; private string server, database, user, password;
public Database(string server, string database, string user, string password) { this.server = server; this.database = database; this.user = user; this.password = password; }
public void Connect() { string connectionString = "Server="+server+ ";Database="+database+";User ID="+user+";Password="+password+";"; try { con = new MySQLConnection(connectionString); con.Open(); if(con!=null) Console.WriteLine("succes"); } catch(Exception e) { throw new Exception(e.Message); } }
public void Close() { if(con!=null) con.Close(); }
public string[] getValue(string value) { String[] arrValues = null; MySQLDataAdapter adapter = new MySQLDataAdapter("SELECT * from pc
where testnr =" + value, con); DataSet ds = new DataSet(); adapter.Fill(ds); DataTable table = ds.Tables[0]; ArrayList kolomWaarden = new ArrayList(); foreach (DataRow row in table.Rows) { arrValues = new String[table.Columns.Count]; for (int i=0; i<table.Columns.Count; i++) arrValues[i] = Convert.ToString(row[i]); } return arrValues; }
} }
In WebForm1.aspx i have this code to connect to the database:
private void openGb() { try { if(gb != null) gb.Close(); gb = new Database("servername", "databasenaam", "username",
"password"); //initiate database class gb.Connect(); } catch(Exception e) { Console.WriteLine(e); } }
In WebForm2.aspx i declare the Database Class by saying
Database gb;
In the page_load i then want to run the SQL-Query in gb.getValue( ) But i
then get a Object reference not found error because the Database Class is not instantiated in WebForm2.aspx. That is like: gb = new
Database("servername", "databasenaam", "username", "password");
Is there a way to prevent this ??
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Martin |
last post by:
When was inheritance intruduced into object oriented programming?
More generally, does anyone know or have any sources on when the different
features were introduced into object oriented...
|
by: G. |
last post by:
Hi all,
During my degree, BEng (Hons) Electronics and Communications Engineering, we
did C programming every year, but I never kept it up, as I had no interest
and didn't see the point. But now...
|
by: user |
last post by:
Hi all,
At the outset, I regret having to post this slightly OT post here.
However, I strongly feel that people in this group would be the best to
advise me on my predicament.
I am working as...
|
by: evolnet.regular |
last post by:
I've been utilising C for lots of small and a few medium-sized personal
projects over the course of the past decade, and I've realised lately
just how little progress it's made since then. I've...
|
by: Robert Seacord |
last post by:
The CERT/CC has just deployed a new web site dedicated to developing
secure coding standards for the C programming language, C++, and
eventually other programming language.
We have already...
|
by: Jakle |
last post by:
I have been googling, but can seem to find out about C GUI libraries.
My main platform is Windows, but it would be nice to find a cross platform
library.
I've been programming with php, which...
|
by: Thierry Chappuis |
last post by:
Hi,
I'm interested in techniques used to program in an object-oriented way
using the C ANSI language. I'm studying the GObject library and Laurent
Deniau's OOPC framework published on his web...
|
by: Enteng |
last post by:
Hi I'm thinking about learning C as my first programming language.
Would you recommend it?
Also how do you suggest that I learn it?What books/tutorials should I
read for someone like me?
Thanks...
|
by: deko |
last post by:
For building Windows desktop apps, the clear favorite is C#. But my clients
can't afford to buy Microsoft products. So I need to develop software for Linux
users and web applications.
In the...
|
by: CoreyWhite |
last post by:
I bought this book years ago, when I was just learning C++. Since
then I've gone through every math course offered at my college, taken
courses on coding C & thinking in terms how how to make the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
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...
|
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...
| |