473,396 Members | 1,748 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,396 software developers and data experts.

Database Connection Designer Variables

Hi,

I am working on a database application and have used the database
connection wizard to connect to an SQL database. I now need to
reference the connection variable (cnn) from another form in the
program and i cannot do this. How can i make the variable global or
somehow pass the variable to the form?

Thanks in advance

James

Dec 2 '06 #1
5 1345
Assuming VS2005:

Store the connection string in your project Settings:

Double-click on "My Project", go to the Settings tab.

Put in a name (anything you like, but hopefully something
that makes sense).

Set the type to "(ConnectionString)".

Set the scope to Application.

Put the connection string in the value field.

Save it.

Now you can use it anywhere in your project as
My.Settings.theConnectionStringName.

Robin S.
--------------------
"jimmy" <ja**************@tiscali.co.ukwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
Hi,

I am working on a database application and have used the database
connection wizard to connect to an SQL database. I now need to
reference the connection variable (cnn) from another form in the
program and i cannot do this. How can i make the variable global or
somehow pass the variable to the form?

Thanks in advance

James

Dec 3 '06 #2
Thanks alot for that, that should come in really useful in my
application. I never knew about these settings! Just out of interest is
there any way i can store the actual connection itself in there?

I have made a new variable and set its type to
System.Data.SqlClient.SqlConnection however i dont know what to set its
value to? Can i make a connection this way or do i need to create a new
connection each time using the connection string?

Thanks

James

Dec 3 '06 #3
I don't think so. You should close your connection
when you're done with it. It's not exactly difficult
to open one.

I'm not sure what you mean by this:
I have made a new variable and set its type to
System.Data.SqlClient.SqlConnection however i dont know
what to set its value to?
You don't really set its value to anything, you just open it.
Here's an example of how to use a connection and command object to fill a
datatable.
This disposes of the connection when it hits the
"End Using".

-------------------
Dim dt as DataTable
Using cnn As New SqlConnection(My.Settings.ProductConnectionString)
cnn.Open()

'define the command
Dim cmd As New SqlCommand
cmd.Connection = cnn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "ProductRetrieveByID_sp"
cmd.Parameters.AddWithValue("@productid", 1)

'define the data adapter and fill the data table
Dim da As New SqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)
End Using
return dt
-----------------

Robin S.
--------------------------

"jimmy" <ja**************@tiscali.co.ukwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com...
Thanks alot for that, that should come in really useful in my
application. I never knew about these settings! Just out of interest is
there any way i can store the actual connection itself in there?

I have made a new variable and set its type to
System.Data.SqlClient.SqlConnection however i dont know what to set its
value to? Can i make a connection this way or do i need to create a new
connection each time using the connection string?

Thanks

James

Dec 3 '06 #4
By that i meant that i had created a new setting in My.Settings with a
type of System.Data.SqlClient.SqlConnection however i didnt know what
to set its value to, however i now realise that i dont need to do this
and i can simply create a new connection each time i need one and then
dispose of it after, like you mentioned in your post.

Thanks for all your help

Dec 3 '06 #5
You're welcome. Good luck.
Robin S.
---------------
"jimmy" <ja**************@tiscali.co.ukwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
By that i meant that i had created a new setting in My.Settings with a
type of System.Data.SqlClient.SqlConnection however i didnt know what
to set its value to, however i now realise that i dont need to do this
and i can simply create a new connection each time i need one and then
dispose of it after, like you mentioned in your post.

Thanks for all your help

Dec 3 '06 #6

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

Similar topics

9
by: Pam Ammond | last post by:
I need the code to update the database when Save is clicked and a text field has changed. This should be very easy since I used Microsoft's wizards for the OleDBAdapter and OleDBConnection, and...
7
by: | last post by:
I am having trouble figuring out to call a database INSERT procedure from a simple submit form. It appears I should use the onclick event to trigger the procedure called BUT when I do this I...
7
by: Luis Mendes | last post by:
Hello, I've a application in VB.NET that uses a connection to a Access data base stored in my Hard Drive. How can I change the location of the database (To a network) and change the conection...
3
by: DC Gringo | last post by:
Hi, I'm trying to use a custom action to modify a database (rather than create one) using the VS.NET '03's help example called "Custom Action to Create Database During Installation". I've made...
18
by: Wim | last post by:
Hi, I would like to make and put a query in my database by VB.NET code. Is this possible? Thanks for your help, wim
5
by: plenahan68 | last post by:
I've made a small vb.net program to read my database and delete records that are specific to my needs. Well, the program says it's complete but nothing is deleted from my database. I've listed the...
11
by: CM Manager via DotNetMonster.com | last post by:
I am very frustrated due to this exception error I am receiving. I've tried searching numerous user support groups, Microsoft Support Net, Google, etc. and haven't found exactly my situation....
1
by: Rico | last post by:
I have tried to access a database using asp.net. after some entries like 120+, I got an unspecified error message. Anyone know what is happening, it seems to be stuck at the same line even when i...
5
by: Edwin Smith | last post by:
Hello: I'm using VS2005 with an ODBC database. I am trying to add a Dialog to an existing Database application to allow the user to select the database. We have 2 different databases with the...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...

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.