Using a design time connection from a class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I've created an SQLConnection by dragging a DataAdapter onto the form. The
connection was named sqlConnection1.

When I try to reference this connection in code from another class, I cannot
find it. Intellisense only finds the classe SQLConnection. This must be a
scope issue.

How can I reference the forms connection object from code? Do I need to
create another connection in the class, or pass it to the class, or is there
a way of directly referencing it?

Many thanks for your advice in advance.
Ant
 
Ant,

I assume that you use a version before 2004. Don't use a form to drag on,
use a component. Than that component is the class which you can instance in
all your forms.

I hope this helps,

Cor
 
Hi Cor,

Thanks again. Yes I am using VS2003. What you mention sounds promising but
I'm not sure what you mean when you say,'don't use a form to drag on, use a
component'. Are you suggesting that you can drag onto a class? What is an
example of a 'component' that you talk about. i think this might solve the
problem.

Thanks kindly
Ant
 
Ant,

Have a look at File->Add New Items and than choose component class, it is a
class on which you can drag as you said components (It has included more).

Cor
 
Ant:

I'd just take the connectionstring that is generated from that connection
and create a new class or module with a static/shared property of type
SqlConnection. Set its connection string to the one that's generated in the
drag on connection (or better yet, store it in your app.config file) and
then you can reference it from anywhere in the application.
 
my opinion about this from my understanding is dont drag the sql
connection rather than that in the class it self write the coding for
the connection by importing the sqlclient and write coding using the
sqlconnection object and sqlcommand object to write the command.Then
create the new object for the class in the form and then use the
connection.If you write in this way the class if be a reuable one and
can be used in the entire application.
 
Back
Top