Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics 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 May 18th, 2007, 11:03 PM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 70
Thanks: 0
Thanked 1 Time in 1 Post
Default public sub or function ?

I have been trying to move the creation of my database connection string to a public subroutine or function in Public Class Main, instead of putting it in each form.
I put a call to the sub/function inside the Main_Load routine.
The problem is the routine isn't passing back the value like I thought it should to all the routines in the application.

Here's the creation code:
Code:
Public Class Main
    ' The application is using Windows authentication.
    ' The name format is DOMAIN\USERNAME.
    Public APC_WirelessConnectionString As String
    '"Data Source=KAREN\APCD1;Initial Catalog=APC_Wireless;Integrated Security=True"        ' Laptop
    '"Data Source=APC-WS01\APCP1;Initial Catalog=APC_Wireless;Integrated Security=True"     ' Development
    '"Data Source=APC-SRV01\APCP1;Initial Catalog=APC_Wireless;Integrated Security=True"    ' Production

    Public Function DetermineServer(ByVal strConn As SqlConnection)
        Dim strParts() As String = Split(My.User.Name, "\")
        Dim strDomain As String = strParts(0)
        Dim strUserName As String = strParts(1)
        Dim strComputerName As String = My.Computer.Name
        Dim strLaptopSrvr As String = "KAREN\APCD1"
        Dim strDvlpSrvr As String = "APC-WS01\APCP1"
        Dim strProdSrvr As String = "APC-SRV01\APCP1"
        Dim strSrvr As String = String.Empty

        Select Case strComputerName
            Case "APC-WS01"
                strSrvr = strDvlpSrvr
            Case "KAREN"
                strSrvr = strLaptopSrvr
            Case Else
                strSrvr = strProdSrvr
        End Select
        APC_WirelessConnectionString = "Data Source=" & strSrvr & _
            ";Initial Catalog=APC_Wireless;Integrated Security=True"
        Return APC_WirelessConnectionString
    End Function
    Private Sub Main_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strConn As New SqlConnection
        Call DetermineServer(strConn)
        'APC_WirelessConnectionString = strConn
        'MsgBox(APC_WirelessConnectionString)
    End Sub
When I try to use the connection string in other subroutines in the application and open the connection, I get an exception error about the string being uninitialized.
Code:
        ' connect to SQL Server
        Dim strConn As New SqlConnection(APC_WirelessConnectionString)
        strConn.Open()
Does anyone have a clue how to help me straighten out the mess I'm trying hard NOT to create? I really am trying to streamline my code, but I'm not being real successful so far.
I would really appreciate your help.

Karen

Reply With Quote
  #2 (permalink)  
Old May 19th, 2007, 01:46 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Karen,

Is it the purpose of DetermineServer to simply define the connection string, and not the connection? If so, simply have the method return the connection string, without passing a connection object to it. Then use the returned connection string to initialize the connection. Something like this should give you an idea:

Public Function DetermineServer(ByVal strConn As SqlConnection) As String
  ' Do whatever you need to do here, building up APC_WirelessConnectionString
  ' as you currently do

  ' then return the connection stirng
  Return APC_WirelessConnectionString
End Function

Since this code returns a connection string, you can initialize a connection object like this:

Dim strConn As New SqlConnection (DetermineServer())

With this code, DetermineServer is called first. It returns the proper connection string which is then passed to the constructor of the SqlConnection object.

Hope this helps,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #3 (permalink)  
Old May 20th, 2007, 09:33 AM
Authorized User
 
Join Date: Dec 2006
Location: , , .
Posts: 70
Thanks: 0
Thanked 1 Time in 1 Post
Default

Imar,
Thanks for the help.

Using the initialization method for the connection string,
Dim strConn As New SqlConnection (DetermineServer())

I get the message that DetermineServer is not declared, which I really don't understand.
DetermineServer is a publicly declared function, defined in the main.vb file.

What else do I need to do to make this work? I am really confused.

Karen

Reply With Quote
  #4 (permalink)  
Old May 20th, 2007, 03:14 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

If it's declared elsewhere (e.g. outside the class you're using it in), you may need to define it as a Shared function:

Public Shared Function DetermineServer.....

End Function

Then you should be able to access it as Main.DetermineServer()

Imar
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
public article - public articledetails _keysersoze_ BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 September 8th, 2007 08:38 AM
Public classes myousman ASP.NET 1.0 and 1.1 Professional 4 October 23rd, 2006 01:18 PM
public functions stojkovmarjan ASP.NET 2.0 Basics 1 September 19th, 2006 03:57 PM
Modules and public variables EricJ Pro VB.NET 2002/2003 3 July 1st, 2005 08:57 AM
public function execute = HELP! Return value ? davidkwang VS.NET 2002/2003 0 April 28th, 2004 12:18 PM



All times are GMT -4. The time now is 03:33 PM.


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