|
-
Feb 23rd, 2018, 02:13 PM
#1
Thread Starter
Addicted Member
[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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|