Ashok Bhanwal
Ashok Bhanwal

Reputation: 87

dataclassesdatacontext' does not contain a constructor that takes 0 arguments?

This is my DBML Designer Code

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;



public partial class DataClassesDataContext  : System.Data.Linq.DataContext 

{

    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

  #region Extensibility Method Definitions
  partial void OnCreated();
  #endregion

    public DataClassesDataContext(string connection) : 
            base(connection, mappingSource)

    {
        OnCreated();
    }

    public DataClassesDataContext(System.Data.IDbConnection connection) : 
            base(connection, mappingSource)
    {
        OnCreated();
    }

    public DataClassesDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
            base(connection, mappingSource)
    {
        OnCreated();
    }

    public DataClassesDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
            base(connection, mappingSource)
    {
        OnCreated();
    }

}

this is my C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;



public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void cmdLogin_Click(object sender, EventArgs e)
    {
        DataClassesDataContext db = new DataClassesDataContext();

    }
}

when i write the DataClassesDataContex db=new DataClassesDataContex(); it will show DataClassesDataContex does not contain a constructor that takes 0 arguments?

Upvotes: 1

Views: 9968

Answers (4)

Joshua Ginn
Joshua Ginn

Reputation: 119

The problem is that the Connection setting in the Properties window got removed.

'DataClassesDataContext' does not contain a constructor that takes 0 arguments

To see these properties

  1. Open the DBML file
  2. Click in the DBML File on the white space (not the table definition)
  3. Click from the menu View > Properties Window (F4)
  4. Scroll down to the Data section. Expand Connection. Click the white space to the right of Connection. Click the drop-down arrow. Your Connection String name from web.config or app.config should appear here.
  5. Click it twice to select it.
  6. Save
  7. Rebuild (ctrl+shift+b) and your default DataClassesDataContext constructor will now work again! :-)

See in the previously attached image that the Connection property got removed. If you can't readd the Connection, you can revert your source control to a previous history point and just re-add the tables and settings since your last commit.

Problem fixed!

Connection properly reestablished!

The default constructor in .designer.cs got restored.

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

Upvotes: 0

Thilina Chathuranga
Thilina Chathuranga

Reputation: 113

Steps:

  1. Remember your dbml file name and delete your dbml file.
  2. Then add a new dbml file with the same previous name to the same location (add -> add new Item -> LINQ to SQL Classes).
  3. Double click on the dbml file in the Solution Explorer.
  4. Now Drag and drop all the tables to that layout of dbml.
  5. Finally add all the associations with the tables as previously you have done.

Upvotes: 4

Yashar Azadvatan
Yashar Azadvatan

Reputation: 159

I fixed it with these steps:

  1. Remove your "dataclasses.dbml"
  2. Open your "Web.Config"
  3. Remove:

    <connectionString>
        <add name="exampleDB" connectionString="Data Source=example;Initial
        Catalog=example;Integrated Security=True"
        providerName="System.Data.SqlClient" />
    </connectionStrings>
    
  4. Remove:

    <assemblies>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral,
        PublicKeyToken=D...8"/>
    </assemblies>
    

You can find <assemblies> within <compilation></compilation>

If you have more than one <add assembly /> tag, you have more than one connection to DB.

  1. Add another "example.dbml" add your tables again.

  2. Create a new datacontext object.

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236218

There is no parameterless constructor defined in your class DataClassesDataContext. By default there should be another constructor generated, which gets connection string from application settings:

public DataClassesDataContext() : 
        base(global::Foo.Properties.Settings.Default.BarConnectionString,
            mappingSource)
{
    OnCreated();
}

But for some reason it is missing right now. You can:

  • Try to re-generate your data context (possibly you deleted this constructor by mistake). To do this right-click your dbml file and select Run custom tool.
  • Create partial class DataClassesDataContext and add parameterless constructor manually
  • Pass connection string to constructor when creating context instance

Upvotes: 9

Related Questions