DataReader
が空の場合、コードが実行されません。以下は私のコードです。
私の仕事は日付のスケジューリングに関するものです。私の問題は、休日の制約に関するものです。ユーザーが日付 (開始日と終了日) を入力すると、プログラムは入力された日付の間に休日があるかどうかをチェックします。DataReader
にデータがない場合は、入力した日付を保存する必要がDataReader
あります。 にデータがある場合は、入力した日付は保存されず、プログラムはエラー メッセージを表示します。
try
{
econ = new SqlConnection();
econ.ConnectionString = emp_con;
econ.Open();
ecmd = new SqlCommand("SELECT CD_Date FROM CONS_DATES where CD_Date between '" + Convert.ToDateTime(dtpStart.Text) + "' and '" + Convert.ToDateTime(dtpEnd.Text) + "'", econ);
ecmd.CommandType = CommandType.Text;
ecmd.Connection = econ;
dr = ecmd.ExecuteReader();
while (dr.Read())
{
DateTime cdname = (DateTime)dr["CD_Date"];
//This code is working
if (Convert.ToDateTime(cdname) >= Convert.ToDateTime(dtpStart.Text) || Convert.ToDateTime(cdname) <= Convert.ToDateTime(dtpEnd.Text))
{
MessageBox.Show("Holiday Constraint. Creating Record Denied.");
} //if
//This code is not working. When the program fetch with no record, it should be continue to add the record but it's not working
else
if (dr == null || !dr.HasRows)
{
//In this area is my code for inserting the entered data.
MessageBox.Show("Add na|!!!. Creating Record Denied.");
}//if else
}//while
}//try
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Connection Status", MessageBoxButtons.OK, MessageBoxIcon.Error);
}