0

Im trying to set a connection string for a my data access layer which is using a value from my Web.Config file from another project.

I create a new class library and add a dbml (Linq to SQL) file and drag a table.

I then add this code to the code file

Partial Public Class MyDataContext
    Public Sub New()
        MyBase.New(ConfigurationManager.ConnectionStrings("WebConnectionString").ConnectionString, mappingSource)
        OnCreated()
    End Sub
End Class

This gave me an error "'Public Sub New()' has multiple definitions with identical signatures."

I understood what the error means so did a quick search on a way around it as when i recompiled the project the same problem remained. The way to approach this was/is to override the OnCreated method so i changed the code to:

Private Sub OnCreated()
   Me.New(ConfigurationManager.ConnectionStrings("WebConnectionString").ConnectionString, mappingSource)
End Sub

This gave the error "Constructor call is valid only as the first statement in an instance constructor" but not a lot of ways to overcome it (well i see a few C# examples but im sure im an converting it to the correct VB .Net code)

After further research no matter what i do, i dont seem to be able to set the connection string in my DAL which should be using the Web.Config connection string value from another project.

What could i be doing wrong?

1 Answer 1

3

OK, In C# first:

 partial class MyDataContext
 {
     public static MyDataContext Create()
     {
          return new MyDataContext(ConfigurationManager.ConnectionStrings("WebConnectionString").ConnectionString, 
           mappingSource);
     }
 // etc
 }

Now, my attempt to translate that in VisualBasic.NET

 Partial Public Class MyDataContext
 Public Shared Function Create() as MyDataContext
    return New MyDataContext (ConfigurationManager.ConnectionStrings("WebConnectionString").ConnectionString, 
           mappingSource)
End Function
End Class

Called via:

   Dim db as MyDataContext = MyDataContext.Create()
Sign up to request clarification or add additional context in comments.

2 Comments

I get the error "Constructor call is valid only as the first statement in an instance constructor.". In case im doing something wrong then i right click the dbml file and select View Code. Thats where the above code is placed.
Sorry, my vb is a bit shaky. I fixed the syntax. Try it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.