Toxic heavy metals, particularly mercury II ions, must be selectively detected in industrial effluent to restrict their percentage and thereby aid in environmental protection. The present study describes the synthesis of 6-(4-(4-(5-cyano-6-oxo-4-styryl)-1,6-dihydropyridin-2-yl)-5-methyl-1H-1,2,3-triazol-1-yl)phenyl)-2-oxo-4-styryl)-1,2-dihydropyridine-3-carbonitrile (TOHC) and its ability as a novel selective fluorescent probe for the detection of mercury II ions in different real water samples. The structure of the prepared TOHC was confirmed by both FTIR and 1HNMR analysis, while crystal structure and thermal stability was investigated using XRD and TGA techniques. The optical properties of TOHC were investigated using both UV–vis and fluorescence spectroscopy at different pH values. Its luminescent properties obtained when TOHC is excited by a wavelength equal to 424 nm, where a maximum emission peak has observed at a wavelength equal to 466.6 nm. Moreover, using Stern-Volmer equation indicate that there is a good correlation between the concentration of Hg2+ ions and the fluorescence intensity of TOHC accompanied with high quenching rate equal to 2.24 x 104 mol−1.L. Selectivity of TOHC toward Hg2+ ions was confirmed by the assistance of fifteen different metal cations. In addition, Job’s Plot method and UV–vis spectroscopy were used to estimate the stoichiometry as well as understand the quenching mechanism between TOHC sensor and Hg2+ ions. The applicability of TOHC sensor for the detection of Hg2+ ions in real samples was further validated using five different real water samples and exhibited excellent results. Lastly, DFT calculations were conducted to provide atomistic insights into the chemical interaction between Hg(II) and TOHC sensor.