Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 31st, 2010, 01:29 PM
Authorized User
 
Join Date: Jun 2003
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
 
Old April 1st, 2010, 02:04 PM
Authorized User
 
Join Date: Jun 2003
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.