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);
}