Solved

Connection String... vb.net

Posted on 2014-10-05
8
328 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 32

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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