Ashok Bhanwal Ashok Bhanwal - 19 days ago 8
SQL Question

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?

Answer

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
Comments