Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

executeScalar method is giving error

$
0
0

Hi All,

public string execCount(string query)
        {
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {

                SqlCommand cmd = new SqlCommand(query,con);
               cmd.Connection = con;
                string count = cmd.ExecuteScalar().ToString();
               

                return count;
            }
        }

It is giving me error saying that 

ExecuteScalar requires an open and available Connection. The connection's current state is closed. What is the error here?

Thanks


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>