سلام
کلا اینطوری نوشتید
string sel = "select * from test where pid ='" + dg1.Rows[i].Cells["pid"].Value + "' And  Month= '" + Month.SelectedIndex + "' And  year= '" + year.Text + "'  "; 

if (sel.Count() < 0)

یعنی کوئری اجرا نشده است تا نتیجه رو چک کنید فقط داخل شرط طول متغییر string چک میشه