Hoh
Hoh

Reputation: 1184

Can't connect to server using Connection String

So here is my code:

Private adoConnection As System.Data.SqlClient.SqlConnection
Private adoadapterFillDSUsers As New System.Data.SqlClient.SqlDataAdapter()
Private ServerName As String = ""
Private ConnectionString As String = ""

 Private Sub adoconn()
        adoConnection = New System.Data.SqlClient.SqlConnection()
        adoConnection.ConnectionString = DataManager.DataManager.ConnectionString
    End Sub
    Public Sub ContinueInit()
        ReadRegistry()
        Dim currRegKey As RegistryKey
        Dim newRegKey As RegistryKey
        currRegKey = Registry.CurrentConfig
        Try
            newRegKey = currRegKey.OpenSubKey("Hoh_Dev\Data")
        Catch ex As Exception
            Return
        End Try
        Try
            MREZA = newRegKey.GetValue("Mreza")
            ServerName = newRegKey.GetValue("ServerName")
        Catch ex As Exception
            Return
        End Try

        If MREZA = "D" Then
            ConnectionString = "Data Source=" & ServerName & ";Integrated Security = SSPI;"
        Else
            If MREZA = "W" Then
                ConnectionString = "Data Source=" & ServerName & ";Database=" & DataBaseName & ";user id=xxx;pwd=xxx;"
            End If
        End If
        adoconn()
    End Sub

I am already loosing my mind since every time when I want to connect to my server and use this:

If adoConnection.State = ConnectionState.Closed Then adoConnection.Open()
Try
  DataManager.DataManager.adoCommand.ExecuteNonQuery()
Catch OpenConnectionError As System.Data.SqlClient.SqlException
End Try

I got this error:

The ConnectionString property has not been initialized.

I call ContinueInit sub from Form Load Event:

 Private Sub LogIn_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ContinueInit()

        Me.TextBox1.Focus()
        Me.TransparencyKey = BackColor
        Me.Button2.Visible = False
        Me.Button1.Visible = False
        TextBox2.PasswordChar = "*"
    End Sub

The code cracks at adoConnection.Open() which means I can't even open a connection to my server so I can execute SQL query, which leads me only to a thing that I didn't setup something good.

Upvotes: 1

Views: 269

Answers (1)

Steve
Steve

Reputation: 5545

Your connection object does not have a ConnectionString filled in.

Not sure what DataManager.DataManager is but I am guesing that you are doing something with creating a shared instance of the DataManager class in your DataManager class and you called it DataManager. (very confusing) But, I would guess you need to look into this and Step Through Your Code to find why the connection string is an empty string.

Upvotes: 1

Related Questions