Connection String Issue
Connection String Issue
Connection String Issue
Connection String Issue
Connection String Issue
Connection String Issue Connection String Issue Connection String Issue Connection String Issue Connection String Issue Connection String Issue Connection String Issue Connection String Issue
Connection String Issue Connection String Issue
Connection String Issue
Go Back  Xtreme Visual Basic Talk > > > Connection String Issue


Reply
 
Thread Tools Display Modes
  #1  
Old 11-16-2009, 05:49 AM
6foot7 6foot7 is offline
Newcomer
 
Join Date: May 2009
Posts: 9
Default Connection String Issue


Hello,

I'm relatively new to VB and have working on a project to add details to a database. I'm having trouble with the connection string, the code is as follows:

Code:
Public Function AddContactToDatabase(ByVal sFirstName As String, ByVal sLastName As String, ByVal sAddress1 As String, ByVal sAddress2 As String, ByVal sAddress3 As String, ByVal sAddress4 As String, ByVal dDOB As Date, ByVal sPhone As String) As Boolean
        Dim myConnection As SqlConnection = Nothing
        Dim sConnect As String = "Data Source=6foot7Laptop\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\6foot7\My Documents\ContactsDB.mdf; Integrated Security=True"
        Try
            myConnection = New SqlConnection(sConnect)
            myConnection.Open()
            Dim myCommand As New SqlCommand
            With myCommand
                .Connection = myConnection
                .CommandText = "AddContact"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .Add("@FirstName", SqlDbType.NVarChar, 20).Value = sFirstName
                    .Add("@LastName", SqlDbType.NVarChar).Value = sLastName
                    .Add("@Address1", SqlDbType.NVarChar).Value = sAddress1
                    .Add("@Address2", SqlDbType.NVarChar).Value = sAddress2
                    .Add("@Address3", SqlDbType.NVarChar).Value = sAddress3
                    .Add("@Address4", SqlDbType.NVarChar).Value = sAddress4
                    .Add("@DOB", SqlDbType.Date).Value = dDOB
                    .Add("@Phone", SqlDbType.NVarChar).Value = sPhone
                End With
                myCommand.ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            Dim oUtilities As New Utils
            oUtilities.ReportError("Error adding contact", ex)
            Return False
        Finally
            If Not myConnection Is Nothing Then
                If myConnection.State = ConnectionState.Open Then
                    myConnection.Close()
                End If
            End If
        End Try
    End Function
Any help would be grealy appreciated....I'm using www.connectionstrings.com as point of reference but still having trouble.

Have SQL Server 2005 Express Edition installed.

Any help would be greatly appreciated.

Regards...
Reply With Quote
  #2  
Old 11-16-2009, 06:37 AM
6foot7 6foot7 is offline
Newcomer
 
Join Date: May 2009
Posts: 9
Default

The error occurs when trying to open the connection, details as follows:

'An attempt to attach an auto-named database for file C:\Documents and Settings\6foot7\My Documents\ContactsDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.'
Reply With Quote
  #3  
Old 11-16-2009, 09:47 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

1. Verify that there is no DB named ContactsDB already attached to your SQL Server instance.

2. If there is already one attached, then you could either unattach the one already attached, or you could change your connection string to point to the attached DB.

3. If there isn't one, then it is likely that the SQL Service account that was used to start the SQL Server service does not have access permissions to this location: C:\Documents and Settings\6foot7\My Documents\. You can

a. copy the ContactsDB.mdf file to the SQL Server DATA folder and change the connection string to point to this new location, or
b. change the SQL service account used to start the service to an account that have access to the My Documents folder, or
c. grant access to the My Documents folder to the SQL service account (not recommended).

On my PC, the SQL Server DATA folder is
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA
(SQL server 2008 developers edition default install folder). It may not be the same as yours, it is provided as an example.

BTW the connection string is different for attached DBs and unattached DBs.

HTH,
<iunknown />
__________________
win7 : vs 2008 : .Net 3.5
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Connection String Issue
Connection String Issue
Connection String Issue Connection String Issue
Connection String Issue
Connection String Issue
Connection String Issue Connection String Issue Connection String Issue Connection String Issue Connection String Issue Connection String Issue Connection String Issue
Connection String Issue
Connection String Issue
 
Connection String Issue
Connection String Issue
 
-->