DataContext does not contain a constructor with 0 parameters

I am learning with this so bear with me...I am trying to create the DB object to access the table.

AppDatabaseDataContext appDb = new AppDatabaseDataContext();

This gives me an error, so I create the constructor...

public AppDatabaseDataContext()
{

} 

Now this tells me that System.Data.Linq.DataContext does not contain a constructor that takes 0 parameters. Why?

Thanks!

Answers


You need to specify a connection string. If you know it's always going to be the same (for example, localhost) you can just hard-code it in your parameterless constructor:

public AppDatabaseDataContext()
    : this(@"Data Source=localhost;Initial Catalog=Foo;Integrated Security=True")
{
}

Notice how we chain the constructor to call the version that consumes a connection string. This way, you can instantiate the data context using the parameterless constructor, and it will use this default connection string.


Sometimes LINQ2SQL might become messy on properties window and the connection string might disapear. If you can, restart all over, remove de Dbml and but DONĀ“T FORGET to delete from the web.config the connection string. Add a new Linq2SQL to your project and let the LINQ2SQL to create the connection string for U in the web.config.


It's strange. Every time I've used a "LINQ to SQL" DataContext in the past, it's always created a 0-argument constructor for me.

But today, I was finding that sometimes, in the Designer, it wouldn't let me select a particular (known) connection string in the Properties window... and when this happened, it refused to create this 0-argument constructor.

The solution was to manually add it myself.

My web.config contained a connection string called MikesConnectionString :

<configuration>
  <connectionStrings>
    <add name="MikesConnectionString" connectionString="Data Source=localhost;Initial Catalog=MikesDatabase;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

... so in my survey.designer.cs file, I added this:

public SurveyDataContext() :
            base(global::System.Configuration.ConfigurationManager.ConnectionStrings["MikesConnectionString"].ConnectionString, mappingSource)
{
     OnCreated();
}

Job done.

But odd. Very odd.


Just Change your connection string just like  
<connectionStrings>
    <add name="DBLabConnectionString" 
         connectionString="Data Source=.\SQLExpress;Initial Catalog=DatabaseName;
         Integrated Security=True" 
         providerName="System.Data.SqlClient" />
 </connectionStrings>

Need Your Help

Need a FullScreen JavaScript or any logic?

javascript html jsp web

Need a full screen where nothing appears excpet body part of my page. No task Bar at bottom and no title bar, addressbar, location bar, toolbar, command menu at top.

Iterate and apply XSLT identity transformation to directory of documents?

xml xslt xslt-2.0

I have a folder with HTML files which look more or less like this: