Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #21 (permalink)  
Old March 30th, 2004, 03:37 AM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Yeah!! I works & u helped me once again!
My problem was in OOP

Thank you so much.

Always:),
Hovik Melkomian.
Reply With Quote
  #22 (permalink)  
Old March 30th, 2004, 04:09 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Hovik,

Glad it worked out. What do you mean with "My problem was in OOP"?

Peter, thanks for catching the static private field. I guess that's what happens when you try to program in an HTML text area ;)

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #23 (permalink)  
Old March 30th, 2004, 04:19 AM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

I meant Object Oriented;)
I thanks from our Moderator to join us & help me:).


Always:),
Hovik Melkomian.
Reply With Quote
  #24 (permalink)  
Old March 30th, 2004, 04:22 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Ha, yes. It's always good to have Eagle-Eye Peter around in this forum ;)

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #25 (permalink)  
Old July 6th, 2004, 10:28 AM
Registered User
 
Join Date: Jun 2004
Location: , , Iran.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think after declaring your class as something like Imar said
-----------------------------
public class Globals
  {
    private string connectionString = "Bla bla bla, your ConnectionString here";
    public string ConnectionString()
    {
      get
      {
        return connectionString;
      }
    }
  }
---------------------------
because yo don't want to evry time create
your object you should declare it as a static object where you want to use
spmething like this(public static Globals=new....)
but I think for objects such SqlDataConnection it would not be a good idea
because in all of your project you have a open connection an also open SqlDataAdapter and...
the security of such program will not be suitable!!!

Reply With Quote
  #26 (permalink)  
Old July 7th, 2004, 05:07 PM
tnd tnd is offline
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why did you not use the static property? Review Olorin's reply.
You also asked if you need to new it on every form that needs it. You do not need an object reference to use static property.

Also most of the replys revolve around this code
(I took from Imar's reply because its the last reply)

  public class Globals
  {
    private string connectionString = "Bla bla bla, your ConnectionString here";
    public string ConnectionString()
    {
      get
      {
        return connectionString;
      }
    }
  }

You mentioned the connection is dynamic which I take to mean you need to set its initial value at run time. Given this I don't see how the above code will work for you. Yes you can a set method but it would only change the property for a single object. See the following codes, assuming there are 2 forms.

Form1 'All variables are public'
    myConst = new Constants();
    myConst.ConnectionString = "ABC";
    myConn = myConst.ConnectionString; 'myConn = "ABC"
    myConst.ConnectionString = "XYZ";
    'Call Form2 passing myConst as a parameter
    ..executions goes to form2..
..more codes..

End Form1

Form2
    myConst = new Constants();
    ' Form1.myConstant.ConnectionString = "XYZ" because it's the last value set
    ' BUT myConst.ConnectionString = ??? 'It's not "ABC" nor "XYZ" because it has never been set

So there is no global variable here. You can way to get "XYZ" because you passed Form1 as a parameter when calling Form2.

What you really need is static property which is access
Form1
   Globals.ConnectionString = "ABC";
   'Call Form2 without passing any parameters
   'When control returns the value of
   'Globals.ConnectionString = "XYZ"

Form2
   'Globals.ConnectionString = "ABC" because it was the last value set
   Globals.ConnectionString = "XYZ" 'set it to "XYZ"
   ..returns..

Static is about as close to global variable as you can get in .NET

Reply With Quote
  #27 (permalink)  
Old July 8th, 2004, 03:41 AM
Registered User
 
Join Date: Jun 2004
Location: , , Iran.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I want to say here exactly what I wanted and what I did in detail!!!
I wanted a gloabl SqlConnection object in all parts of my ASP.NET Application....
(not only in one procedure or even in one webform I mean in all of my projrct's scope!!!!)
tnd('this ID ... ':D)You are surely right but it works fine in WINDOWS APPLICATIONS not in ASP.NET APPLICATIONS you know in ASP.NET application evry time that procedure Page_Load
executes, all the variables reinitialize,because of this problem ,in ASP.NET application you should
decalre evry Gloabl Object as Static

now.....
I declar a public class in my namespase that inculdes my essential object like SqlConnection and SqlCommand and....
as you will see the name of this class is 'Connection' and the name of my namespace is 'myApplication'
I consider that I want to use a SqlConnection object as global object not a simple string variable
ofcourse I can declare my Connection class in many other ways for example
I can use inheritance and overriding the methods or something like below
(using inheritance can be much better)
---------------------------------------------------------
namespace myApplication
{
    public class Connection
    {

        public SqlConnection objConnection;
        public SqlCommand objCommand;
        public Connection(string strConnection)//public Constructor
        {
            objConnection=new SqlConnection(strConnection);
                        //here you can also create your SqlCommand and ....
        }
        public void CloseConnection()//For closing the connection
        {
        if(objConnection.State=ConnectionState.Open)
            {
            objConnection.Close();
            }
        }
        public void OpenConnection()//For opening the connection
        {
         if(objConnection.State=ConnectionState.Closed)
                {
            objConnection.Open();

                    }
        }
                //other methods that you need ,declare them as public methods


    }
//other classe are here like WebForms
    public class WebForm1 : System.Web.UI.Page
    {
               //.....
    }
    public class WebForm2 : System.Web.UI.Page
    {
               //.....
    }

}//for namespace
--------------------------------------------------------------------
after declaring my class in the namespace I go to Global.asax.cs file
in this file I declare a static public filed and in Application_Start event I create it
you know because this object is static one when it created one time it remains all the time in your scope
--------------------------------------------------------
namespace myApplication
{
    public class Global : System.Web.HttpApplication
    {
        private System.ComponentModel.IContainer components = null;
        public static myApplication.Connection c;

        public Global()
        {
            InitializeComponent();
        }

        protected void Application_Start(Object sender, EventArgs e)
        {
            string str="server=(local);database=Northwind;integrated security=true;";
            c=new connection(str);
        }
     //other events
    }//for Global
}//for namespace
-----------------------------------------------------------
now I use this object in evry webform that I want without recreating it
(because it is an static object and in Application_Start event in Gloabl.ascx has been created one time)
-----------------------------------------------------------
namespace myApplication
{
    public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;


        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            string strSql="select myColumn from myTable";
            SqlCommand objCommand=new SqlCommand(strSql,myApplication.Global.c.objConnec tion);
            myApplication.Global.c.objConnection.Open();
            DataGrid1.DataSource=objCommand.ExecuteReader();
            DataGrid1.DataBind();
            myApplication.Global.c.objConnection.Close();

        }
    }//For WebForm1

}//For namespace
-------------------------------------------------------------
ofcourse I was able to declare my Fields as private and declare Properties and....for better Encapsulation but for convenience in writing this topic ....
but I think such application is not secure at all
I prefered recreating my connection objects evry time I need and disposing it when I don't need

any better idea?


Reply With Quote
  #28 (permalink)  
Old July 8th, 2004, 04:15 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Without focusing on the code and security too much, I think this will present one huge problem.

If I read your code correctly (I haven't had enough coffee yet, so I may be missing something), you have one static myApplication.Connection object, defined in your Global.asax. This also means that throughout your entire application you have only one connection object.

If this is correct, you'll be in major troubles soon. This will work nice on a development machine where you are the only user but it will fail miserably on a multi-user system. For example, while you have a DataReader open, all access to the connection is blocked. You cannot retrieve other data over that same connection while the reader is open. So, when 2, 10, or a 1,000 users hit your site at the same time, all users except for the first will be queued....

I think you should stick to the bankrobber's 3G rule: Go in, Get what you want and Get out.
So, define a global connection string and on each page that requires data access, create a connection object, open it, get your data and immediately close your connection again. Connection pooling will minimize the cost of opening a new connection on each page.
To write cleaner code you could create helper classes or methods that do that for you, as long as each page deals with its own connection, and not with a globally shared one.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #29 (permalink)  
Old July 8th, 2004, 09:38 AM
Registered User
 
Join Date: Jun 2004
Location: , , Iran.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar thank you very much for your answer
when you answer me ,another questions generate in my mind(what a disaster for you!!!)
first I can not get this problem well,in that way you siad if there is a site with lots of users a day
by declaring many SqlConnection objects in this site ,at least some users will be queued
(I can not get the difference between one global connection object and many connections object ,when at one time many users hit the site,nevertheless many users will be queued ,because finally at one time
many user want to accsess the datastore.You exampled me DataReader but in my global Connection object when I don't need it I close it just like local SqlConnection objects ,can you explain it more)
but if this problem is something miserable I think the below method can solve it(I am not sure)
I think instead of Application_Start event in my Global.ascx file
it is better to use Init event
therfore evry user has one unique instance of my static variable(I mean class Connection) and
I think there would be no problem
(I am not sure I have not tested it for multi users)
-------------------------------------------------------------
//instead of "protected void Application_Start(Object sender, EventArgs e)" I used Init event
protected void Init(Object sender, EventArgs e)
        {
            string str="server=(local);database=Northwind;integrated security=true;";
            c=new connection(str);
        }
------------------------------------------------------------
Imar...
now don't focous on Connection objects
for example I want to use another type of objects as gloabl in my all parts of project
I think this method I mentioned will be a good one
if you know better ways tell me(also tell me about the solution I said about the first problem)
I think this topic is so hot for me that all the wrox books beside me are burning!!!:D:D
Imar if you have free time help me get this problem and solve
I am tackling with this subject about four days

Thanks again.




Reply With Quote
  #30 (permalink)  
Old July 9th, 2004, 02:22 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Under normal conditions, your Web server is a multi-tasking system. This means that when 20 users hit your site at the same time, they will all be served at the same time. Since processors aren't really that multi-tasking, the effect is achieved by carrying out little parts of a task for each user / process, then the next, and so on. This process repeats itself until everyone's task is complete.

From a user's point of view, you're all helped at the same time. The number of users that can be served at the same time is of course limited by processor capacity and memory. When many users hit your site simultaneously, the processing takes longer to complete, which causes your server to look slow. If the number of hits gets too large, you'll receive a 500 (server too busy) or a 503 (Service Unavailable) error code indicating that your request cannot be processed.

On a relatively fast machine, your server can help hundreds maybe even thousands of users at the same time, without one user noticing lag because of another user.

However, when you use a single connection instance in your application as you proposed in this thread, you'll effectively demote your system into a single-user system. When 20 users hit your site and the first opens and uses that connection to get a DataReader to get 20,000 records out, *all* other users have to wait. Even if the server has enough processing capacity to serve those other 19 users, they are queued up anyway, because they have to wait in line for the database connection to become available.

Now, if you have each page open a connection when it needs it, and closes it again immediately after it's done with it, you have your multi-user system back. Those 20 users will share the server's capacity (performing lots of micro tasks for each user) until their request is complete. Since each user has its own connection object, they can all connect to the database "at the same time" (provided you have a multi-user database system like SQL Server or Oracle) which of course increases the performance and responsiveness of your system.

Does this make any sense?

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Sunday by Sonic Youth (Track 2 from the album: A Thousand Leaves) What's This?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
comapring global variable value to local variable amhicraig XSLT 6 December 5th, 2007 12:16 PM
Global Variable techfreak123 XSLT 0 August 3rd, 2006 07:45 AM
global variable? milk_vanilla Classic ASP Basics 3 January 8th, 2005 06:50 PM
global variable in c# kobystud C# 3 April 22nd, 2004 05:56 AM



All times are GMT -4. The time now is 06:34 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.