Solved

Connection String... vb.net

Posted on 2014-10-05
8
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c# DateTime Format validation 4 68
Get sourcecode path 14 46
Error building VS2105 solution from repository 1 35
VB.net Filesystem watcher not working 5 37
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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