对象引用未设置为对象的实例

时间:2022-03-05 14:31:07

Is my machine playing me today?

我的机器今天在玩吗?

When I run the TestConnectionString() method of this object I get the error at the connection string setting.

当我运行此对象的TestConnectionString()方法时,我在连接字符串设置中得到错误。

public class CustomerDAL
{
   string connectionString = ConfigurationManager.ConnectionStrings["myConnection"].Name;

    public CustomerDAL()
    {

    }

    public string TestConnection()
    {
        System.Data.SqlClient.SqlConnection conn =  new System.Data.SqlClient.SqlConnection(connectionString);
        conn.Open();

        if (conn.State == System.Data.ConnectionState.Open)
        {
            return "Open";
        }
        else
        {
            return "Close";
        }
     }
}

3 个解决方案

#1


If ConnectionStrings["myConnection"] returns null, then dereferencing it to get the Name property will fail in the constructor. Is that definitely not where the bug is?

如果ConnectionStrings [“myConnection”]返回null,则取消引用它以获取Name属性将在构造函数中失败。这绝对不是bug的地方吗?

Why not put a breakpoint on that line and take a look in ConfigurationManager.ConnectionStrings to check what it thinks it's got - and check very carefully for typos.

为什么不在该行上设置一个断点并查看ConfigurationManager.ConnectionStrings来检查它认为它有什么 - 并仔细检查拼写错误。

After that, put a breakpoint on the first line of the method, and check what the value of connectionString is. Passing in null to the SqlConnection constructor doesn't actually throw an exception, but you'd get an InvalidOperationException when you try to open it.

之后,在方法的第一行放置一个断点,并检查connectionString的值是什么。将null传递给SqlConnection构造函数实际上不会抛出异常,但是当您尝试打开它时会出现InvalidOperationException。

#2


I have three suggestions

我有三个建议

  1. you need to add a .dll reference for configuration manager
  2. 您需要为配置管理器添加.dll引用

  3. recheck if your connection string is right
  4. 如果连接字符串正确,请重新检查

  5. check if you have more than one configuration file ex. web.config and app.config; so the ConfigurationManager is referencing the wrong file in the solution.
  6. 检查您是否有多个配置文件。 web.config和app.config;所以ConfigurationManager在解决方案中引用了错误的文件。

#3


I had same problem with a perfectly working code this morning. The problem was the db server itself, it was refusing the connections from our ip address (network provider changed our ip addresses).

今天早上我的代码完全正常,我遇到了同样的问题。问题是数据库服务器本身,它拒绝来自我们的IP地址的连接(网络提供商改变了我们的IP地址)。

Somehow SqlConnection object gives 'Object reference not set to an instance of an object' instead of relaying the original response from the db server.

不知何故,SqlConnection对象将“对象引用未设置为对象的实例”,而不是从数据库服务器中继原始响应。

#1


If ConnectionStrings["myConnection"] returns null, then dereferencing it to get the Name property will fail in the constructor. Is that definitely not where the bug is?

如果ConnectionStrings [“myConnection”]返回null,则取消引用它以获取Name属性将在构造函数中失败。这绝对不是bug的地方吗?

Why not put a breakpoint on that line and take a look in ConfigurationManager.ConnectionStrings to check what it thinks it's got - and check very carefully for typos.

为什么不在该行上设置一个断点并查看ConfigurationManager.ConnectionStrings来检查它认为它有什么 - 并仔细检查拼写错误。

After that, put a breakpoint on the first line of the method, and check what the value of connectionString is. Passing in null to the SqlConnection constructor doesn't actually throw an exception, but you'd get an InvalidOperationException when you try to open it.

之后,在方法的第一行放置一个断点,并检查connectionString的值是什么。将null传递给SqlConnection构造函数实际上不会抛出异常,但是当您尝试打开它时会出现InvalidOperationException。

#2


I have three suggestions

我有三个建议

  1. you need to add a .dll reference for configuration manager
  2. 您需要为配置管理器添加.dll引用

  3. recheck if your connection string is right
  4. 如果连接字符串正确,请重新检查

  5. check if you have more than one configuration file ex. web.config and app.config; so the ConfigurationManager is referencing the wrong file in the solution.
  6. 检查您是否有多个配置文件。 web.config和app.config;所以ConfigurationManager在解决方案中引用了错误的文件。

#3


I had same problem with a perfectly working code this morning. The problem was the db server itself, it was refusing the connections from our ip address (network provider changed our ip addresses).

今天早上我的代码完全正常,我遇到了同样的问题。问题是数据库服务器本身,它拒绝来自我们的IP地址的连接(网络提供商改变了我们的IP地址)。

Somehow SqlConnection object gives 'Object reference not set to an instance of an object' instead of relaying the original response from the db server.

不知何故,SqlConnection对象将“对象引用未设置为对象的实例”,而不是从数据库服务器中继原始响应。