473,480 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Usage of global sqlconnection throughout my project

Hi all,

I have a question/problem concerning usage of my sqlconnection throughout my
program.
In my main form (frmMain) I defined an sqlconnection (I've set the
connection public so that I can open & close the connection from everywhere
in my app).
My main form is an mdi parent which contains several mdi-childs.
To open and close the sqlconnection in an mdi-child i use the following:

((frmMain)this.mdiparent).sqlconnection.open();
((frmMain)this.mdiparent).sqlconnection.close();

But this works for an mdi-child. Now I want to use this sqlconnection within
a form that I show as modal, let me explain this a bit further:
I have an mdi-child open, from there I want to open a small form (by
..ShowModal()) and in that form I must open and close the sqlconnection to
save my in that form edited data.

Can somebody help me how to address my sqlconnection from my frmMain in this
form that is showed modal?

Thank you for helping me out.
Kind regards,
Tom.

Nov 16 '05 #1
4 5165
Offhand I think you'd have to pass a reference either to the connection or
to frmMain to the constructor of the modal form, or else send it to a public
property of the modal form before calling ShowDialog() on it.

For globally available stuff like this I tend to prefer using a class of
static members rather than relying on it being in a top level form. Then
it's just a matter of something like Globals.DBConnection.Open(),
Globals.DBConnection.Close(), etc. and it works the same no matter where you
call it from.

--Bob

"The Coolest Dolphin" <Th***************@discussions.microsoft.com> wrote in
message news:13**********************************@microsof t.com...
Hi all,

I have a question/problem concerning usage of my sqlconnection throughout
my
program.
In my main form (frmMain) I defined an sqlconnection (I've set the
connection public so that I can open & close the connection from
everywhere
in my app).
My main form is an mdi parent which contains several mdi-childs.
To open and close the sqlconnection in an mdi-child i use the following:

((frmMain)this.mdiparent).sqlconnection.open();
((frmMain)this.mdiparent).sqlconnection.close();

But this works for an mdi-child. Now I want to use this sqlconnection
within
a form that I show as modal, let me explain this a bit further:
I have an mdi-child open, from there I want to open a small form (by
.ShowModal()) and in that form I must open and close the sqlconnection to
save my in that form edited data.

Can somebody help me how to address my sqlconnection from my frmMain in
this
form that is showed modal?

Thank you for helping me out.
Kind regards,
Tom.

Nov 16 '05 #2
"The Coolest Dolphin" <Th***************@discussions.microsoft.com>
wrote:
Can somebody help me how to address my
sqlconnection from my frmMain in this form
that is showed modal?


Either pass the SqlConnection to the modal form's constructor, or
declare the SqlConnection as 'static' so that no instance of the
parent form is required.

P.
Nov 16 '05 #3
Thank you Bob & Paul for helping me out.

I will use and try both options.

Kind regards,
Tom.

"The Coolest Dolphin" wrote:
Hi all,

I have a question/problem concerning usage of my sqlconnection throughout my
program.
In my main form (frmMain) I defined an sqlconnection (I've set the
connection public so that I can open & close the connection from everywhere
in my app).
My main form is an mdi parent which contains several mdi-childs.
To open and close the sqlconnection in an mdi-child i use the following:

((frmMain)this.mdiparent).sqlconnection.open();
((frmMain)this.mdiparent).sqlconnection.close();

But this works for an mdi-child. Now I want to use this sqlconnection within
a form that I show as modal, let me explain this a bit further:
I have an mdi-child open, from there I want to open a small form (by
.ShowModal()) and in that form I must open and close the sqlconnection to
save my in that form edited data.

Can somebody help me how to address my sqlconnection from my frmMain in this
form that is showed modal?

Thank you for helping me out.
Kind regards,
Tom.

Nov 16 '05 #4
actually, .NET uses connection pooling, so all you need is to have the
same connection string in all the connection instances you want to share
and it'll use the same connection in its internals

The Coolest Dolphin wrote:
Thank you Bob & Paul for helping me out.

I will use and try both options.

Kind regards,
Tom.

"The Coolest Dolphin" wrote:

Hi all,

I have a question/problem concerning usage of my sqlconnection throughout my
program.
In my main form (frmMain) I defined an sqlconnection (I've set the
connection public so that I can open & close the connection from everywhere
in my app).
My main form is an mdi parent which contains several mdi-childs.
To open and close the sqlconnection in an mdi-child i use the following:

((frmMain)this.mdiparent).sqlconnection.open() ;
((frmMain)this.mdiparent).sqlconnection.close( );

But this works for an mdi-child. Now I want to use this sqlconnection within
a form that I show as modal, let me explain this a bit further:
I have an mdi-child open, from there I want to open a small form (by
.ShowModal()) and in that form I must open and close the sqlconnection to
save my in that form edited data.

Can somebody help me how to address my sqlconnection from my frmMain in this
form that is showed modal?

Thank you for helping me out.
Kind regards,
Tom.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2359
by: Tony Johansson | last post by:
Hello! I know it's bad design to use global variables. I just want to ask a question about them. Is global variables and global static variables the same. These are define outside any...
4
9308
by: RD | last post by:
I opened a connection to the database in the login form. Now in another form I tried to insert values to a table. But the connection is not available in the form. How can I make the connection...
4
2741
by: Mark | last post by:
OK. Here we go. I have an ASP.NET application that does many hits to a SQL Server DB on a separate server. When I first created this application (2 years ago) and was very new to ASP/ASP.NET, to...
3
2997
by: Robert Walter | last post by:
I want to user the Page method LoadControl inside the Global.asax.cs but when I instantiate a page and try it I get the following error... System.NullReferenceException: Object reference not set...
4
1458
by: RC | last post by:
I'm trying to get an idea of the amount of memory is being used by my ASP.NET application. Here's what I did: I rebooted my machine and fired up VS.NET 2003, opened the project in question...
22
1625
by: UJ | last post by:
How do I create an instance of a class that is accessible to all classes/forms in my project? I already have a startup module and I was thinking putting stuff in there and then accessing it through...
6
12371
by: Greg Smith | last post by:
Is it possible to declare a global constant? I would like to have a constant visible throughout the application but I can't get the syntax correct to make it global. Any help is greatly...
3
4019
by: rhyme2ri2 | last post by:
Hi! I want to use some variables throughout my VC#.net windows based application........... How can i declare global variables that will work throughout my project... -Regards
7
1813
by: twang090 | last post by:
I find in other team member's project, they are referencing a type in following format " public static global::ErrorReport.Description Description = new global::ErrorReport.Description(); " I...
0
7055
marktang
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,...
0
7059
Oralloy
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,...
0
7103
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...
0
5362
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,...
0
4499
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...
0
3011
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...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.