Results 1 to 5 of 5

Thread: [SOLVED][2005]test connection connected ...? how?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    172

    [SOLVED][2005]test connection connected ...? how?

    Hi guys,

    I have got a test connection button which tests the connectionstring that is been made by the user by typing in all relevant info.

    How do i check whether it has connected or it cant connect due to a faulty password for example.


    heres my code:

    currently i am using the state property but everytime it cant connect it just breaks the program, saying it cant hold a null value or something.

    Code:
       Private Sub bntTestConn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntTestConn.Click
            Dim myConnection As New SqlConnection
    
            If rdoWinAuth.Checked = True Then
                Dim WinConn As New ServerConnection
                Dim server As New Server(WinConn)
    
                WinConn.LoginSecure = True
                WinConn.DatabaseName = txtSQLDatabase.Text
                WinConn.ServerInstance = txtSQLServer.Text
    
                'Open the Win Connection
                'WinConn.Connect()
    
                If server.State = 0 Then
                    MsgBox(" Unable to establish an SQL connection - Connection Failed")
                Else : MsgBox("Connection Succesfull")
                End If
    
    
                'Close the Win connection
                WinConn.Disconnect()
            End If
    
            If rdoSecAuth.Checked = True Then
                Dim SecConn As New ServerConnection
                Dim server As New Server(SecConn)
    
             
    
                If txtPassword.Text = "" Or txtUserName.Text = "" Then
                    MsgBox("Please enter your Username/Password")
                Else
    
                    'Declare the connectionstring
                    SecConn.LoginSecure = False
                    SecConn.DatabaseName = txtSQLServer.Text
                    SecConn.ServerInstance = txtSQLDatabase.Text
                    SecConn.Login = txtUserName.Text
                    SecConn.Password = txtPassword.Text
    
                  
                    'Open the Sec connection 
                    SecConn.Connect()
    
                    If server.State = 0 Then
                        MsgBox(" Unable to establish an SQL connection - Connection Failed")
                    Else : MsgBox("Connection Succesfull")
                    End If
    
                    'Close the Sec connection
                    SecConn.Disconnect()
                End If
            End If
        End Sub
    Any help is appreciated.

    Thanks!

    Rob
    Last edited by Devdude; Oct 9th, 2007 at 06:06 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005]test connection connected ...? how?

    The only way to test a connection is to open it:
    vb.net Code:
    1. Try
    2.     myConnection.Open()
    3.     myConnection.Close()
    4.     MessageBox.Show("Success")
    5. Catch ex As Excpetion
    6.     MessageBox.Show("Failure")
    7. End Try
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    172

    Re: [2005]test connection connected ...? how?

    Thanks JMC that seems to work and show me a message box.
    Only problem still is that it still highlights the line showing up an error:
    Failed to connect to server2.
    What can i do about this?

    Thanks,
    Rob

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    172

    Re: [2005]test connection connected ...? how?

    ok sorry about that,
    i think i will be able to figure that one out.
    i think i understand why its doing that.

    my code needs a little facelift.

    ill post back my final result and let you know how its going.

    Thanks,

    Rob

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    172

    Re: [2005]test connection connected ...? how?

    Thanks JMC,

    That worked out fine, so far I do not have any issues.

    Thanks for helping me out with this one!



    Rob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width