473,382 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

C# : How to make connection avilable to all forms & classes in an Application?

RD
My application contains several Classes & Forms .
From the Login form I will get the User ID and Password for the
SqlConnection.
I established the connection through another class and I can get it in the
Login form.

In another form , say Employee form , I want to save the Employee details
to the Employee table. But when I tried to use connection which is already
opened I couldn't get it.

Then I tried to make the User Id and Password available to all classes and
forms in the project.
That way I can close and reopen it whereever I want in the application.
Anyway I am not able
to do it.

Please help me.

Thanks
RD

Nov 15 '05 #1
2 2753
Hi RD,
To make a connection available in a all application, you must declare it
static in class. Perhaps, the
best solution is to use a Singleton pattern.
The code should look like this:

public class ConnectionSingleton
{
private static _connection = new SqlConnection();

public static CurrentConnection
{
get { return _connection; }
}
}

Anyway, this solution doesn´t work in a multithread application where all
trheads share this connection because some concurrency problem can arise.

I hope this can help you
Pablo Cibraro.

"RD" <di******@alltel.net> wrote in message
news:Gw**************@fe01.usenetserver.com...
My application contains several Classes & Forms .
From the Login form I will get the User ID and Password for the
SqlConnection.
I established the connection through another class and I can get it in the
Login form.

In another form , say Employee form , I want to save the Employee details to the Employee table. But when I tried to use connection which is already opened I couldn't get it.

Then I tried to make the User Id and Password available to all classes and
forms in the project.
That way I can close and reopen it whereever I want in the application.
Anyway I am not able
to do it.

Please help me.

Thanks
RD


Nov 15 '05 #2
RD,

You can also create another constructor for the second form that
accepts ref objects from the first form (including that whole form if
you like!)... Don't forget to use this() to call the default
constructor when you use this method. Instantiate the form2 and pass
over whatever objects you like as reference parameters. Within the
second form just assign the ref objects to an internal variable and
use it throughout your new form...

private Form1 MyParentForm;

ie. in the new form create a second constructor

public Form2 (ref OleDbConnection oleConn, ref Form1
myMainForm):this()
{
...do more work with the connection, et. al.;
this.MyParentForm = myMainForm;
}

If I were you I would get all of my data into datasets and then
just pass the dataset over to the new form...
--
~~~~~~~~~~~~~
Tommie Carter
--
"Pablo M. Cibraro" <pc******@hotmail.com> wrote in message news:<ei*************@TK2MSFTNGP12.phx.gbl>...
Hi RD,
To make a connection available in a all application, you must declare it
static in class. Perhaps, the
best solution is to use a Singleton pattern.
The code should look like this:

public class ConnectionSingleton
{
private static _connection = new SqlConnection();

public static CurrentConnection
{
get { return _connection; }
}
}

Anyway, this solution doesn´t work in a multithread application where all
trheads share this connection because some concurrency problem can arise.

I hope this can help you
Pablo Cibraro.

"RD" <di******@alltel.net> wrote in message
news:Gw**************@fe01.usenetserver.com...
My application contains several Classes & Forms .
From the Login form I will get the User ID and Password for the
SqlConnection.
I established the connection through another class and I can get it in the
Login form.

In another form , say Employee form , I want to save the Employee

details
to the Employee table. But when I tried to use connection which is

already
opened I couldn't get it.

Then I tried to make the User Id and Password available to all classes and
forms in the project.
That way I can close and reopen it whereever I want in the application.
Anyway I am not able
to do it.

Please help me.

Thanks
RD


Nov 15 '05 #3

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

Similar topics

2
by: MikeY | last post by:
Hiya All, I'm coding in C#, & making a windows application. I am trying to figure out how to put my dynamic control code in a seperate class (say ie ButtonClass.cs) so that I can re-use the code...
1
by: Geoff Jones | last post by:
Hi I'm starting to look at C# after programming in C++ and was wondering if anybody could help me with the following questions: (1) What is the difference between a Project and Solution In...
4
by: dm1608 | last post by:
Hi -- I have a VB6 application that I plan on rewriting in C#.NET. The program today basically connects to a SQL Server and parses varies system stats from our mainframes and rolls the data up...
7
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
1
Maidenz08
by: Maidenz08 | last post by:
Hi, How can i pass parameters between forms in windows application. I have a function in form1 called as populatedata(txtsearch.text). Now I have a textbox in form2 whose text i need to pass to...
1
by: undertruck | last post by:
Hello, Someone posted this topic and I was not allowed to post a reply. So pardon me for asking this here again. Someone has to say something on this? I'm kinda looking for the same thing. ...
0
by: Ramesh | last post by:
Hi, How to open Web appication forms in windows application. I need to open some web application from in windows application. Please help me in this.
6
by: sravan_reddy001 | last post by:
I am creating a website. where the data to be stored in my server. I had my webspace from some service provider. How can i connect the website and thee database. --website is deployed at webspace...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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 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.