0

Here is my code in vb.net below. The problem with this connection string is that the system reads it, compiles it but when I'm trying to pass any input into the database it tells me network instance error.

You see here the original code for the connection:

  Module MyFunction
Public cnn As SqlClient.SqlConnection
Public Sub MakeConnection()
    cnn = New SqlClient.SqlConnection
    If Not cnn.State = ConnectionState.Open Then

        cnn.ConnectionString = System.IO.File.ReadAllText(Application.StartupPath & "\cnn.txt")
        cnn.Open()
    End If 

This gives me error that "executereader is not initialized"
So I changed it like this:

    Public cnn As SqlClient.SqlConnection
Public Sub MakeConnection()
    cnn = New SqlClient.SqlConnection("Data Source=USARES-PC\SQLEXPRESS;Initial Catalog=HotelDB;Integrated Security=True")
    cnn.Open()
    '  MessageBox.Show("Connection opened!")
End Sub 

I also tried this way:

       Public cnn As SqlClient.SqlConnection
    Public Sub MakeConnection() 
     If cnn.State = ConnectionState.Open Then
        cnn.ConnectionString = "Data Source=USARES-PC\SQLEXPRESS;Initial       Catalog=HotelDB;Integrated Security=True"
          cnn.Close()
        End If
Damien
  • 1,161
  • 2
  • 19
  • 31
  • One possible reason could be your local firewall blocking access to your local SQLExpress instance. Try creating a connection as described in [my answer to "How to get the connection string from a database"](http://stackoverflow.com/a/10480011/205233) to test connectivity outside your program code. – Filburt May 03 '15 at 15:43
  • Open SQL Server Management Studio (SSMS) which comes with SQL Server. Use same instance in the login window of SSMS as the DataSource in you connection string. – jdweng May 03 '15 at 16:22
  • i'm using the same instance as what i'm using with connection string – Leah Nikka Salvador May 03 '15 at 16:33

0 Answers0