Results 1 to 10 of 10

Thread: [RESOLVED] database connection problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Resolved [RESOLVED] database connection problem

    Hi, I'm trying to connect a Linq-to-SQL context to a database with a relative path using DataDirectory but I get the following error:

    "You need to specify the full path to an executable file"

    My data access class constructor in a separate DLL looks like this:

    Code:
        Private Sub New()
            dc = New MilkywayDataContext()
        End Sub
    In the app.config folder for the same DLL I have this:

    Code:
        <connectionStrings>
            <add name="MilkywayDataContext"
                connectionString="data source=(LocalDB)\v11.0; AttachDbFilename =|DataDirectory|\MilkywayDB.mdf;Integrated Security=True"
                providerName="System.Data.SqlClient" />
        </connectionStrings>
    And in the web.config for the main project, I have the exact same.

    I don't know why it's not working as I have another project which uses the same method, albeit as an Entity Framework Model, and it works fine.

    Any help will be greatly appreciated!

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Re: database connection problem

    I should have pointed out that the New MilkywayDataContext() contained a ConnectionString parameter from My.Settings previously. I've deleted this setting and the compiler now points to this code:

    Code:
    Imports System.Linq.Expressions
    Imports System.Reflection
    
    
    <Global.System.Data.Linq.Mapping.DatabaseAttribute(Name:="MilkywayDB")>  _
    Partial Public Class MilkywayDataContext
    	Inherits System.Data.Linq.DataContext
    	
    	Private Shared mappingSource As System.Data.Linq.Mapping.MappingSource = New AttributeMappingSource()
    	
      #Region "Extensibility Method Definitions"
      Partial Private Sub OnCreated()
      End Sub
      Partial Private Sub InsertBatch(instance As Batch)
        End Sub
      Partial Private Sub UpdateBatch(instance As Batch)
        End Sub
      Partial Private Sub DeleteBatch(instance As Batch)
        End Sub
      Partial Private Sub InsertUserRole(instance As UserRole)
        End Sub
      Partial Private Sub UpdateUserRole(instance As UserRole)
        End Sub
      Partial Private Sub DeleteUserRole(instance As UserRole)
        End Sub
      Partial Private Sub InsertImage(instance As Image)
        End Sub
      Partial Private Sub UpdateImage(instance As Image)
        End Sub
      Partial Private Sub DeleteImage(instance As Image)
        End Sub
      Partial Private Sub InsertIngredient(instance As Ingredient)
        End Sub
      Partial Private Sub UpdateIngredient(instance As Ingredient)
        End Sub
      Partial Private Sub DeleteIngredient(instance As Ingredient)
        End Sub
      Partial Private Sub InsertProduct(instance As Product)
        End Sub
      Partial Private Sub UpdateProduct(instance As Product)
        End Sub
      Partial Private Sub DeleteProduct(instance As Product)
        End Sub
      Partial Private Sub InsertProductIngredient(instance As ProductIngredient)
        End Sub
      Partial Private Sub UpdateProductIngredient(instance As ProductIngredient)
        End Sub
      Partial Private Sub DeleteProductIngredient(instance As ProductIngredient)
        End Sub
      Partial Private Sub InsertStatus(instance As Status)
        End Sub
      Partial Private Sub UpdateStatus(instance As Status)
        End Sub
      Partial Private Sub DeleteStatus(instance As Status)
        End Sub
      Partial Private Sub InsertUser(instance As User)
        End Sub
      Partial Private Sub UpdateUser(instance As User)
        End Sub
      Partial Private Sub DeleteUser(instance As User)
        End Sub
      #End Region
    	
    	Public Sub New()
            MyBase.New(Global.MilkywayDLL.My.MySettings.Default.MilkywayDBConnectionString, mappingSource)
    		OnCreated
    	End Sub
    How can I change the invocation to the parent constructor so it works with the connections string in the config file?

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: database connection problem

    Well the error message looks clear enough to me! Short of rewriting VB itself I'm not sure what we can do other than suggest you do as you're told.

    albeit as an Entity Framework Model
    So, no difference there then?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: database connection problem

    How exactly did you generate that connection string? I just added a Service-based Database item to a project in VS 2012 and got something similar except that the name looked like this: "WindowsApplication1.My.MySettings.Database1ConnectionString".

    Also, are you sure that it's the connection string that's the issue? The fact that the error message says:
    You need to specify the full path to an executable file
    suggests otherwise.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: database connection problem

    Quote Originally Posted by garry79 View Post
    I should have pointed out that the New MilkywayDataContext() contained a ConnectionString parameter from My.Settings previously. I've deleted this setting and the compiler now points to this code:

    Code:
    Imports System.Linq.Expressions
    Imports System.Reflection
    
    
    <Global.System.Data.Linq.Mapping.DatabaseAttribute(Name:="MilkywayDB")>  _
    Partial Public Class MilkywayDataContext
    	Inherits System.Data.Linq.DataContext
    	
    	Private Shared mappingSource As System.Data.Linq.Mapping.MappingSource = New AttributeMappingSource()
    	
      #Region "Extensibility Method Definitions"
      Partial Private Sub OnCreated()
      End Sub
      Partial Private Sub InsertBatch(instance As Batch)
        End Sub
      Partial Private Sub UpdateBatch(instance As Batch)
        End Sub
      Partial Private Sub DeleteBatch(instance As Batch)
        End Sub
      Partial Private Sub InsertUserRole(instance As UserRole)
        End Sub
      Partial Private Sub UpdateUserRole(instance As UserRole)
        End Sub
      Partial Private Sub DeleteUserRole(instance As UserRole)
        End Sub
      Partial Private Sub InsertImage(instance As Image)
        End Sub
      Partial Private Sub UpdateImage(instance As Image)
        End Sub
      Partial Private Sub DeleteImage(instance As Image)
        End Sub
      Partial Private Sub InsertIngredient(instance As Ingredient)
        End Sub
      Partial Private Sub UpdateIngredient(instance As Ingredient)
        End Sub
      Partial Private Sub DeleteIngredient(instance As Ingredient)
        End Sub
      Partial Private Sub InsertProduct(instance As Product)
        End Sub
      Partial Private Sub UpdateProduct(instance As Product)
        End Sub
      Partial Private Sub DeleteProduct(instance As Product)
        End Sub
      Partial Private Sub InsertProductIngredient(instance As ProductIngredient)
        End Sub
      Partial Private Sub UpdateProductIngredient(instance As ProductIngredient)
        End Sub
      Partial Private Sub DeleteProductIngredient(instance As ProductIngredient)
        End Sub
      Partial Private Sub InsertStatus(instance As Status)
        End Sub
      Partial Private Sub UpdateStatus(instance As Status)
        End Sub
      Partial Private Sub DeleteStatus(instance As Status)
        End Sub
      Partial Private Sub InsertUser(instance As User)
        End Sub
      Partial Private Sub UpdateUser(instance As User)
        End Sub
      Partial Private Sub DeleteUser(instance As User)
        End Sub
      #End Region
    	
    	Public Sub New()
            MyBase.New(Global.MilkywayDLL.My.MySettings.Default.MilkywayDBConnectionString, mappingSource)
    		OnCreated
    	End Sub
    How can I change the invocation to the parent constructor so it works with the connections string in the config file?
    If you open your DBML file in the designer then open the Properties window you will see the properties for the DataContext, including the Connection property.

  6. #6
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: database connection problem

    |DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.


    For example, instead of having the following connection string:

    "Data Source= c:\program files\MyApp\Mydb.sdf"


    Using DataDirectory, you can have the following connection string:

    “Data Source = |DataDirectory|\Mydb.sdf”


    To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:

    •For applications that are put in a folder on the user's computer, the database folder uses the application folder.
    •For applications that are running under ClickOnce, the database folder uses the specific data folder that is created
    See above quote

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Re: database connection problem

    Thanks for your comments!

    I've changed the constructor to this, to try to get the path from the web config file:

    Code:
        Public Sub New()
            'MyBase.New(Global.MilkywayDLL.My.MySettings.Default.MilkywayDBConnectionString, mappingSource)
            MyBase.New(WebConfigurationManager.ConnectionStrings("MilkywayDataContext").ToString())
            OnCreated()
        End Sub
    And I'm getting the same error again

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Re: database connection problem

    I don't remember how I generated the connection string. The error continues to pop up. It's so annoying because it doesn't point to the code. How am I supposed to find the error if it doesn't point to it? Aghhh!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: database connection problem

    Quote Originally Posted by garry79 View Post
    I don't remember how I generated the connection string. The error continues to pop up. It's so annoying because it doesn't point to the code. How am I supposed to find the error if it doesn't point to it? Aghhh!
    By reading the stack trace of the exception.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Posts
    230

    Re: database connection problem

    I've recreated the DAL and it works now

    Thanks
    Last edited by garry79; Nov 2nd, 2012 at 04:54 PM.

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