You have to create instance of your sqlconnection and sqlcommand with new operator.
No need to write dr.Close();
Please try this way. It works.
try
{
string connectionString = "Your connection string";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.CommandText = "select count(*) from test;";
cmd.Connection = con;
int a = (int)cmd.ExecuteScalar();
con.Close();
string status = Convert.ToString(a);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
this.Close();
}