Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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
  #1 (permalink)  
Old March 31st, 2010, 01:29 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default Create a DB Connection (Once) and Call Connection When Needed

Hi, I have a DB connection that works within a class. What I would like to do is be able to create a class with the connection in it (myClass) and reference that connection whenever I need it.

I have the following that works:

Code:
PublicSub sqlCon()
'Uses this connection to access TCIS database
Dim Conn As New SqlClient.SqlConnection
Dim dt As New DataTable()
Conn.ConnectionString = "Server=myServer;Database=myDB; Integrated Security=SSPI;"
Conn.Open()
Try
'Insert code to process data.
Dim sqlCmd As New SqlCommand("sp_mySproc", Conn)
Dim sqlDa As New SqlDataAdapter(sqlCmd)
Dim i As Integer = 0
sqlDa.Fill(dt)
For i = 0 To dt.Rows.Count - 1
blah, blah
 
Next i
Catch ex As Exception
MessageBox.Show(ex.Message, "Warning Message", MessageBoxButtons.OK)
Finally
Conn.Close()
End Try
End Sub
That works fine but I do not want to write this out everytime I need to access the DB. What I now need to do is be able to reference the above code residing in the myClass Class.

I created the myClass class and tried calling the connection from my Windows form like so:
Code:
 
Dim nmyClass As New myClass
Dim sqlCmd As New SqlCommand("sp_mySproc", nmyClass.SqlCon)
But I get a squiggly line under nmyClass.SqlCon that says:
"Expression does not produce a value".

Can anyone tell me where I am going wrong? Thanks!
__________________
SLBIBS
Reply With Quote
  #2 (permalink)  
Old April 1st, 2010, 02:04 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi everyone, I was just wondering if I needed to re-phrase the question (or not). I am a novice client/server programmer and I thought that I was doing something silly to generate the error on a rather simple request.

Please advise if I need to move the question to another VB.Net forum. I tried searching the help for what I need (I thought my request would be fairly routine or so simple everyone would know how to do it but me :o)

Thanks for your time.
__________________
SLBIBS
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
More on the DB connection leonardpogrady Dreamweaver (all versions) 1 January 29th, 2010 11:19 AM
Database Connection / Login help needed kscase VB Databases Basics 4 April 9th, 2007 10:26 AM
db connection dfeuerborn Excel VBA 1 August 19th, 2005 02:24 AM
Data base connection string needed pramos.21d ADO.NET 3 March 8th, 2005 01:21 PM
DB Connection stu9820 ASP.NET 1.0 and 1.1 Basics 1 September 26th, 2004 09:01 PM



All times are GMT -4. The time now is 12:26 AM.


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