dcsimg
Results 1 to 10 of 10

Thread: [RESOLVED] An Attempt to Attach Database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Resolved [RESOLVED] An Attempt to Attach Database

    Im creating a hangman game on an windows app form.. I keep getting a message when I debug on the part I have marked red on my Game Form code...


    System.Data.SqlClient.SqlException
    HResult=0x80131904
    Message=An attempt to attach an auto-named database for file C:\Users\ENJ1105\source\repos\HangmanApp\HangmanApp\bin\Debug\HangmanDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    Code:
    Public Class GameForm
        ' Define variables
        Dim dsQuestions As String           'Object variable
        Dim numWord As Integer              ' To hold index of word
        Dim word As String                  ' Hold actual word
        Dim numWrongGuesses As Integer      ' Hold number of wrong guesses
        Dim numRightGuesses As Integer      ' Hold number of right guesses
        Dim gameStarted As Boolean = False  ' Indicate if game has started or not
    
        Private Sub GameForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' Disable txtGuess textbox.
            txtGuess.Enabled = False
            ' Set numWord variable. 
            Dim numWord As Integer = 0
    
            ' Call the Load Data method.
            LoadData()
    
        End Sub
    
        Private Sub QuitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles QuitToolStripMenuItem.Click
            ' Close the form
            Me.Close()
        End Sub
    
        Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
            Dim dsQuestions As New HangmanDBDataSet
            Dim numWord As Integer = 1
            numWord += 1
            Dim word As String
            Dim numWrongGuesses As Integer = 10
            Dim numRightGuesses As Integer = 5
            Dim gameStarted As Boolean = True
    
            ' Initialize controls
            lblUnitDescription.Text = "Choose a game"
            txtGuess.Text = lblGuessALetter.Text
    
            ' Clear text of all labels.
            lblUnitDescription.Text = String.Empty
            txtGuess.Clear()
            lblLetter0.Text = String.Empty
            lblLetter1.Text = String.Empty
            lblLetter2.Text = String.Empty
            lblLetter3.Text = String.Empty
            lblLetter4.Text = String.Empty
            lblLetter5.Text = String.Empty
            lblLetter6.Text = String.Empty
            lblLetter7.Text = String.Empty
            lblLetter8.Text = String.Empty
            lblLetter9.Text = String.Empty
            lblLetter10.Text = String.Empty
            lblLetter11.Text = String.Empty
            ' Enable txtGuess. 
            txtGuess.Enabled = True
            ' Set the focus to txtGuess.
            txtGuess.Focus()
    
            ' Gets word column row
    
            Try
                word = CStr(dsQuestions.Words(numWord)(1))
                ' Gets unit description
                lblUnitDescription.Text = CStr(dsQuestions.Words(numWord).UnitsRow(1))
    
            Catch
    
                ' Set label control for character.
                lblLetter0.Text = "_"
                lblLetter1.Text = "_"
                lblLetter2.Text = "_"
                lblLetter3.Text = "_"
                lblLetter4.Text = "_"
                lblLetter5.Text = "_"
                lblLetter6.Text = "_"
                lblLetter7.Text = "_"
                lblLetter8.Text = "_"
                lblLetter9.Text = "_"
                lblLetter10.Text = "_"
                lblLetter11.Text = "_"
    
    
                Dim ch As Char = CChar(CStr(0))
                Dim ch1 As Char = "_"
                Dim ch2 As Char = "_"
                Dim ch3 As Char = "_"
                Dim ch4 As Char = "_"
                Dim ch5 As Char = "_"
                Dim ch6 As Char = "_"
                Dim ch7 As Char = "_"
                Dim ch8 As Char = "_"
                Dim ch9 As Char = "_"
                Dim ch10 As Char = "_"
                Dim ch11 As Char = "_"
            End Try
        End Sub
    
        Private Sub txtGuess_TextChanged(sender As Object, e As EventArgs) Handles txtGuess.TextChanged
            Dim gameStarted As Boolean = True
            Dim word As String = ""
            Dim numRightGuesses As Integer = 5
            Dim ch As Char
    
            If gameStarted = True Then
                If word.Contains(txtGuess.Text) Then
                    SetRightLetterLabels(word, ch)
                    numRightGuesses += 1
                End If
    
            ElseIf word.Contains(txtGuess.Text) = False Then
                pboxHangman.ImageLocation = “images\Hangman-” & numWrongGuesses & “.png”
                numWrongGuesses += 1
            End If
            CheckProgress()
    
        End Sub
    
        Private Sub CheckProgress()
            ' Calculates the total number of tries.
            Dim numTries As Integer
            numTries = numRightGuesses + numWrongGuesses
            ' Displays messages
            If numRightGuesses = 5 Then
                MessageBox.Show("You in 5 tries!" & ControlChars.CrLf &
                                "Do you want to play another game?", "You won!",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
            ElseIf numWrongGuesses = 10 Then
                MessageBox.Show("You in 10 tries. The word was ''." & ControlChars.CrLf &
                                "Do you want to play another game?", "You lost",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
            End If
        End Sub
    
        Private Sub WonGame()
    
        End Sub
    
        Private Sub LostGame()
            Dim numWrongGuesses As Integer
            If numWrongGuesses > 6 Then
                numWrongGuesses = True
            End If
            End
    
            lblLetter0.Text = "_"
            lblLetter1.Text = "_"
            lblLetter2.Text = "_"
            lblLetter3.Text = "_"
            lblLetter4.Text = "_"
            lblLetter5.Text = "_"
            lblLetter6.Text = "_"
            lblLetter7.Text = "_"
            lblLetter8.Text = "_"
            lblLetter9.Text = "_"
            lblLetter10.Text = "_"
            lblLetter11.Text = "_"
        End Sub
    
        Private Sub LoadData()
            Dim dsQuestions As New HangmanDBDataSet
            Dim taUnits As New HangmanDBDataSetTableAdapters.UnitsTableAdapter
            Dim taWords As New HangmanDBDataSetTableAdapters.WordsTableAdapter
            taUnits.Fill(dsQuestions.Units)
            taWords.Fill(dsQuestions.Words)
       End Sub
    
        Private Function FindLabel(ByVal i As Integer) As Control
            ' Finds the right label in the word panel
            Dim label = panelWord.Controls.Find("lblLetter", False)(0)
            Return label
        End Function
    
        Private Sub SetRightLetterLabels(ByVal word As String, ByVal ch As Char)
            For i As Integer = 0 To word.Length - 1
                ' Check for each letter and set the appropriate letter
                If word.Chars(i) = ch Then
                    FindLabel(i).Text = ch
                End If
            Next
        End Sub
    End Class
    These are the properties for the database file..
    (Name) C:\USERS\ENJ1105\SOURCE\REPOS\HANGMANAPP\HANGMANAPP\HANGMANDB.MDF

    The connection String for the database is showing
    Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ENJ1105\source\repos\HangmanApp\HangmanApp\H angmanDB.mdf;Integrated Security=True;Connect Timeout=30

    (Primary Path) C:\Users\ENJ1105\source\repos\HangmanApp\HangmanApp\HangmanDB.mdf

    The user interface is set to false but if I go to set it to be True it says...

    The login Instance flag is not allowed when connecting to a user instance of SQL server. The connection will be closed.

    Do I need to detach the database on sql server or take offline to make it work in Visual Studio? I have already upgraded it and put it in my project

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,661

    Re: An Attempt to Attach Database

    Only one data file with a particular name can be attached to a SQL Server instance at a time. Sometimes this error message is legitimate while other times it occurs because a previously attached data file gets "stuck" somehow. I've seen this issue in the past but not for quite a while. Try restarting the SQL Server service and you should be good to go for the time being. If it keeps happening then there may be some issue with SQL Server and/or VS that might require a repair or reinstall.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: An Attempt to Attach Database

    I only attached the one file HangmanDB.mdf to SQL Server..I tried uninstalling VS and restarting SQL server but still no difference..ughh this is the only thing left I have to fix but I dont see how I can fix this..ive looked online for answers but nothing seems to fit my problem

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,661

    Re: An Attempt to Attach Database

    Based on what I've seen of this issue in the past, here's what I would do to try to diagnose it at least:

    Restart Windows
    Open the project folder in Windows Explorer and delete the bin and obj folders
    Open the project in VS
    Run the project

    Does the error occur? If not:

    Exit the app from the app, NOT the debugger
    Run the project again

    Does the error occur? If not:

    Exit the app from the debugger
    Run the project again

    Does the error occur?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    472

    Re: An Attempt to Attach Database

    One thing I see is that the connection string you posted appears to have an extra space in it between the H and the a in HangmanDB.mdf (see the bolded piece below).

    The connection String for the database is showing
    Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ENJ1105\source\repos\HangmanApp\HangmanApp\H angmanDB.mdf;Integrated Security=True;Connect Timeout=30

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,661

    Re: An Attempt to Attach Database

    Quote Originally Posted by OptionBase1 View Post
    One thing I see is that the connection string you posted appears to have an extra space in it between the H and the a in HangmanDB.mdf (see the bolded piece below).
    I could be wrong but I suspect that that isn't in the original text and it's just the forum doing that to what was posted.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: An Attempt to Attach Database

    Quote Originally Posted by OptionBase1 View Post
    One thing I see is that the connection string you posted appears to have an extra space in it between the H and the a in HangmanDB.mdf (see the bolded piece below).
    Oh sorry that was my mistake. I accidentally added a space..the original text doesnt have a space

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: An Attempt to Attach Database

    Quote Originally Posted by jmcilhinney View Post
    Based on what I've seen of this issue in the past, here's what I would do to try to diagnose it at least:

    Restart Windows
    Open the project folder in Windows Explorer and delete the bin and obj folders
    Open the project in VS
    Run the project

    Does the error occur? If not:

    Exit the app from the app, NOT the debugger
    Run the project again

    Does the error occur? If not:

    Exit the app from the debugger
    Run the project again

    Does the error occur?
    None of those things worked..It keeps coming up the program comes up when I take that part off of my code but I need it

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,661

    Re: An Attempt to Attach Database

    Unless you actually are running two applications that are both trying to attach data files with the same name, I think that this is likely due to a bug in VS or SQL Server. I know that I have seen this happen inexplicably in the past and I know that I'm not the only one. I would suggest that you create a new test project with a SQL Server data file and see if the same thing happens. If not, it might be an issue with that specific project, in which case it may be your best option to create a new project with a new data file and then migrate all the code across. If the same thing happens in multiple projects then you may just have to uninstall VS and SQL Server and reinstall. It's an extreme solution but sometimes it's easier than trying to narrow down exactly which DLL is broken.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: An Attempt to Attach Database

    Quote Originally Posted by jmcilhinney View Post
    Unless you actually are running two applications that are both trying to attach data files with the same name, I think that this is likely due to a bug in VS or SQL Server. I know that I have seen this happen inexplicably in the past and I know that I'm not the only one. I would suggest that you create a new test project with a SQL Server data file and see if the same thing happens. If not, it might be an issue with that specific project, in which case it may be your best option to create a new project with a new data file and then migrate all the code across. If the same thing happens in multiple projects then you may just have to uninstall VS and SQL Server and reinstall. It's an extreme solution but sometimes it's easier than trying to narrow down exactly which DLL is broken.
    Well, I uninstalled both and connected again and it worked!! Thank you for your help!

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
  •  



Featured


Click Here to Expand Forum to Full Width