8

I'm playing around with Dapper for the first time. Looks like a pretty handy little tool. But I'm running into one problem. In the little console app below, the first method runs as expected. However the second method returns this error:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll Additional information: The ConnectionString property has not been initialized.

I can turn the order of the methods around and get the same results. It's always on the second call that I get the error. Not sure what I'm doing wrong. I also tried not using the db.Close(), but I got the same result.

The error is on this line in whatever method is called second:

db.Open();

Any ideas? Thanks!

class Program
{
    static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DapperConnection"].ToString());

    static void Main(string[] args)
    {
        IEnumerable<Policy> policy1 = PolicySelectAll();
        IEnumerable<Policy> policy2 = PolicyFindByLastFour("093D");
    }


    public static IEnumerable<Policy> PolicySelectAll()
    {
        var sql = "SELECT * FROM Policy";
        IEnumerable<Policy> policy;
        using (db)
        {
            db.Open();
            policy = db.Query<Policy>(sql);
            db.Close();
        }
        return policy;
    }

    public static IEnumerable<Policy> PolicyFindByLastFour(string LastFour)
    {
        var sql = string.Format("SELECT * FROM Policy WHERE PolicyNumber LIKE '%{0}'", LastFour);
        IEnumerable<Policy> policy;
        using (db)
        {
            db.Open();
            policy = db.Query<Policy>(sql);
            db.Close();
        }
        return policy;
    }
}

EDIT AFTER:

Based on the answers, this is how I solved it:

class Program
{
    static string connectionString = ConfigurationManager.ConnectionStrings["DapperConnection"].ToString();

    static void Main(string[] args)
    {
        IEnumerable<Policy> policy1 = PolicySelectAll();
        IEnumerable<Policy> policy2 = PolicyFindByLastFour("093D");
    }

    public static IDbConnection GetConnection()
    {
        return new SqlConnection(connectionString);
    }

    public static IEnumerable<Policy> PolicySelectAll()
    {
        IDbConnection db = GetConnection();
        var sql = "SELECT * FROM Policy";
        IEnumerable<Policy> policy;
        using (db)
        {
            db.Open();
            policy = db.Query<Policy>(sql);
            db.Close();
        }
        return policy;
    }

    public static IEnumerable<Policy> PolicyFindByLastFour(string LastFour)
    {
        IDbConnection db = GetConnection();
        var sql = string.Format("SELECT * FROM Policy WHERE PolicyNumber LIKE '%{0}'", LastFour);
        IEnumerable<Policy> policy;
        using (db)
        {
            db.Open();
            policy = db.Query<Policy>(sql);
            db.Close();
        }
        return policy;
    }
}
1
  • 3
    you are "using" db, it means it will be disposed after each use, so after you use it once the connection is destroyed...
    – Gusman
    Mar 22, 2016 at 14:13

1 Answer 1

4

if you move your definition of db into the method scope it will be fine. I.E.

class Program
{
    static void Main(string[] args)
    {
        IEnumerable<Policy> policy1 = PolicySelectAll();
        IEnumerable<Policy> policy2 = PolicyFindByLastFour("093D");
    }


    public static IEnumerable<Policy> PolicySelectAll()
    {
        var sql = "SELECT * FROM Policy";
        IEnumerable<Policy> policy;
        using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DapperConnection"].ToString()))
        {
            db.Open();
            policy = db.Query<Policy>(sql);
            db.Close();
        }
        return policy;
    }

    public static IEnumerable<Policy> PolicyFindByLastFour(string LastFour)
    {
        var sql = string.Format("SELECT * FROM Policy WHERE PolicyNumber LIKE '%{0}'", LastFour);
        IEnumerable<Policy> policy;
        using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DapperConnection"].ToString()))
        {
            db.Open();
            policy = db.Query<Policy>(sql);
            db.Close();
        }
        return policy;
    }
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.