0

Do I have to add connection string somewhere else like we do in web config in Asp.net but in vb.net windows form I can't find anything like web config Below is code for connection...

 Dim Conn As New SqlConnection
    Conn.ConnectionString = "Data Source=Abc;Initial Catalog=Test_Database;Integrated Security=True"
    Conn = New SqlConnection
    ' If Conn Is Nothing Then
    If Conn.State = ConnectionState.Open Then
        MsgBox("Connected")
    Else
        MsgBox("Not Connected")
6
  • In your project you should have an app.config file. That's the file to use Commented Sep 14, 2017 at 14:25
  • I am using Vb.net 2015 for windows form. Where can I find app.config file? Commented Sep 14, 2017 at 14:26
  • In your solution explorer, it should be the 4th item in the list. Commented Sep 14, 2017 at 14:39
  • I don't see any :( Commented Sep 14, 2017 at 14:51
  • Then Add it - Add New Item, General, Application Configuration File. You can use the same ConnectionStrings section that you can see in a web.config Commented Sep 14, 2017 at 15:16

1 Answer 1

2

It is not working, because you are 'Newing' the SqlConnection after assigning the connection string.

THis is your current code;

Dim Conn As New SqlConnection
    Conn.ConnectionString = "Data Source=Abc;Initial Catalog=Test_Database;Integrated Security=True"
    Conn = New SqlConnection
    ' If Conn Is Nothing Then
    If Conn.State = ConnectionState.Open Then
        MsgBox("Connected")
    Else
        MsgBox("Not Connected")

However you have to take out the second new for the connection string, should look like this;

Dim Conn As New SqlConnection
Conn.ConnectionString = "Data Source=Abc;Initial Catalog=Test_Database;Integrated Security=True"

' If Conn Is Nothing Then
If Conn.State = ConnectionState.Open Then
    MsgBox("Connected")
Else
    MsgBox("Not Connected")
Sign up to request clarification or add additional context in comments.

2 Comments

Now I got new error..Can not open System.Data.SqlClient.SqlException:Login fail for user
What kind of database are you trying to connect to? Would it be SQL server? Error points that it is trying to connect, but it can not log in to the server. Have a look at connectionstrings.com the site list the proper connection string to various database types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.