简体   繁体   中英

How to resolve ASP.NET DataContext connection error

I have the following error with my ASP.NET web site. I have just moved it to another PC. Got IIS running, SQL Server Express & changed the Data Source machine name to the new machine name. However on any page with DB access, I get the error shown in the screenshot: 应用程序中的服务器错误

innovations-news.aspx creates a new DataContext thus:

dataContext = new DataContext(Config.ConnectionString);

My Config class returns the connection string based on current IP. For example:

return ConfigurationManager.ConnectionStrings["Development"].ConnectionString;

Which is in web.config:

<connectionStrings>
<add name="Development" connectionString="Data Source=WORK51C0\SQLEXPRESS;Initial Catalog=Jri;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>

The following is from LinqToSql1.designer.cs

public DataContext() : 
            base(global::Jri.Data.Properties.Settings.Default.JriConnectionString1, mappingSource)
    {
        OnCreated();
    }

    public DataContext(string connection) : 
            base(connection, mappingSource)
    {
        OnCreated();
    }

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

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

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

Any advice on how best to debug this? Or perhaps even you know whats wrong?!

Thanks,

That is an ArgumentNullException. You are somehow passing null to the constructor or DataContext.

Attach a debugger and see why Config.ConnectionString or mappingSource is returning null.

OK well this was an access problem that My Config class with ConfigurationManager. So my temporary solution has been to have the connection string directly outputted by this class rather than try to get it from web.config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM