VB.NET 2005 connecting to SQL Server Express, No errors but cmds don't run

2.2k views Asked by At

I am trying to connect to SQL Server Express locally using VB.NET 2005. I pulled my connection string directly from the app.config file. When I run, I get NO errors and the connection states returns open, however the commands are not being processed.

Imports System.Data
Imports System.Data.SqlClient

Public Class frmAddMovie
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click    
    Dim conString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True;User Instance=True;"
    Dim con As New SqlConnection(conString)
    Dim cmd As New SqlCommand("Insert Into tblMovies(fldTitle, fldDirector, fldRating)Values('Solar Babies', 'PG', 'Rick Flair')", con)

    Using con
       con.Open()
       cmd.ExecuteNonQuery()
    End Using

    If MessageBox.Show("Movie Added") = Windows.Forms.DialogResult.OK Then
        Me.Close()
    End If

End Sub
End Class
2

There are 2 answers

1
Pandagrrl On BEST ANSWER

Are you running this inside of a virtual machine?

I run on Snow Leopard and have to access VS2010 from time to time from my mac. I have had similar issues on a VM but when using the same code otherwise it runs perfectly.

I am not exactly sure if that is the issue, but the solution is to try it on a windows-based PC. If it works, then at least you know it is Virtual Machine-related.

0
Amen Ayach On

AttachDbFilename=|DataDirectory is in the bin/debug folder and by default your is recopyed each time you run your project, so what you a have inserted will be overwritten.

The solution is simply: 1 - Connect directly to main data

or

2 - In your project select the Movies.mdf, hit F4 Set Copy to output directory to 'Do not copy'