Solved

Connection String... vb.net

Posted on 2014-10-05
8
338 Views
Last Modified: 2014-10-05
The following code gives me an error: 40- could not open a connection to SQL Server.

Question: How can I handle this?
Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlCommand

Public Class Form1

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

        Dim cnn As SqlConnection = New SqlConnection

        Try
            Dim strCnn As String = "Server='Mike-Vaio';Database='NORTHWND.MDF';Trusted_Connection=True;"
            Dim cmd As SqlCommand = New SqlCommand("Select * From tblProduct", cnn)
            cnn.ConnectionString = strCnn
            cnn.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader

            If dr.HasRows Then
                MessageBox.Show("Has Records")
            Else
                MessageBox.Show("Has no Records")
            End If
            ' DataTable dt = new DataTable();
            'dt.Load(dr);
            'dataGridView1.DataSource = dt;
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            cnn.Close()
        End Try

    End Sub
End Class

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 6
  • 2
8 Comments
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40362293
This sounds like you're trying to load the NORTHWND database as user instance, but it is already active. Just open SSMS (SQL Server Management Studio) and take a look at the online databases.

Try a different connection string: Server=.\SQLEXPRESS12;Database=NORTHWND;Trusted_Connection=True;
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40362299
Hi ste5an,

FYI, I have revised (changed and improved) my original question several times. My apology for this. I want just make sure you have read my latest version.

Mike
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40362308
re:> and take a look at the online databases.

How do I do this?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40362313
Oh, under task 'Take Offline" shows enabled. This indicates it is already online.

BTW, I am using SQL Express 12 and vs2012

Also, I checked and found out the following:

SQL Server (SQLEXPRESS12)  is running
SQL Server Agent (SQLEXPRESS12)  is stopped
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40362406
I was missinf SQLEXPRESS12 in:

Server='Mike-Vaio\SQLEXPRESS12';Database

Dim strCnn As String = "Server='Mike-Vaio';Database='NORTHWND.MDF';Trusted_Connection=True;"
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40363007
I've requested that this question be closed as follows:

Accepted answer: 0 points for eghtebas's comment #a40362406
Assisted answer: 500 points for ste5an's comment #a40362293

for the following reason:

I was missinf SQLEXPRESS12 in:

Server='Mike-Vaio\SQLEXPRESS12';Database

Dim strCnn As String = "Server='Mike-Vaio';Database='NORTHWND.MDF';Trusted_Connection=True;"
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40362536
So my answer was correct. You should give the points.
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40363008
I thought I gave the points, apparently there was a mistake.

Sorry,

Mike
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question