Solved

Connection String... vb.net

Posted on 2014-10-05
8
331 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 33

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 33

Author Comment

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

How do I do this?
0
 
LVL 33

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 33

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 33

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 33

Author Closing Comment

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

Sorry,

Mike
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Duplicating a tab page content to another in TabControl control 11 41
Run time Error 4 35
Help with LINQ and XML 10 28
Show/Hide ASP.NET controls with JQuery best practices 3 21
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now