Results 1 to 5 of 5

Thread: I am not able to fix this error VB 2017 Net SQLEXpress.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    262

    Question I am not able to fix this error VB 2017 Net SQLEXpress.

    This little Module Creates a new database, but before creating it, it checks if the database exists. There are two options (YesNo): delete or leave.
    I have a problem in the connection My string is good because it works in another program it is a local database SQLExpress.
    The Error message is this: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
    I have tried for more than a week now, and I do not understand this error. All I know is the connection, but I do not know how to fix it. Thank you in advance if someone wants to help me.

    Here is the code:

    Module Module1
    Sub Main()
    Dim connectionString As String = "Data Source=Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;"


    Dim databaseName As String = "Appoinment"
    Dim dbExists As Boolean = False

    Using connection As New SqlConnection(connectionString)
    connection.Open()
    Dim cmd As New SqlCommand("SELECT database_id FROM sys.databases WHERE name = @dbName", connection)
    cmd.Parameters.AddWithValue("@dbName", databaseName)

    Dim result = cmd.ExecuteScalar()
    If result IsNot Nothing Then
    dbExists = True
    End If
    End Using

    If dbExists Then
    Dim result As DialogResult = MessageBox.Show("Database exists. Do you want to delete it?", "Confirm Delete", MessageBoxButtons.YesNo)
    If result = DialogResult.Yes Then
    Using connection As New SqlConnection(connectionString)
    connection.Open()
    Dim cmd As New SqlCommand("DROP DATABASE " & databaseName, connection)
    cmd.ExecuteNonQuery()
    MessageBox.Show("Database deleted successfully.")
    End Using
    End If
    Else
    MessageBox.Show("Database does not exist.")
    End If
    End Sub
    End Module
    mannyso

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,837

    Re: I am not able to fix this error VB 2017 Net SQLEXpress.

    Which line throws the exception?

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,990

    Re: I am not able to fix this error VB 2017 Net SQLEXpress.

    Seriously?
    Dim connectionString As String = "Data Source=Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;"
    https://www.connectionstrings.com/sql-server/
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    262

    Re: I am not able to fix this error VB 2017 Net SQLEXpress.

    Line 7 connection.open()
    mannyso

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,990

    Re: I am not able to fix this error VB 2017 Net SQLEXpress.

    I seriously doubt this:
    My string is good because it works in another program
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width