Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Connection String... vb.net

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

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

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
re:> and take a look at the online databases.

How do I do this?
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
I was missinf SQLEXPRESS12 in:

Server='Mike-Vaio\SQLEXPRESS12';Database

Dim strCnn As String = "Server='Mike-Vaio';Database='NORTHWND.MDF';Trusted_Connection=True;"
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;"
So my answer was correct. You should give the points.
I thought I gave the points, apparently there was a mistake.

Sorry,

Mike