4

My code does not run when the DataReader is empty. Below is my code.

My work is about Date Scheduling. And my problem is about a holiday constraint. When the user enters the dates (start date and end date), the program will check whether the entered dates have any holidays in between. If the DataReader doesn't have any data, the entered dates should be saved, or if the DataReader has data, then the entered dates are not saved and the program gives an error message.

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);
}
5
  • And how do you want to add records using a DataReader, an object that reads data? Is this all the code you have?
    – Marcote
    Oct 22, 2012 at 3:18
  • since your condition for the while loop is dr.Read(), dr cannot be null when inside the loop. If it is null, while loop will not be executed at all. What do you want to achieve through checking dr for null?
    – manas
    Oct 22, 2012 at 3:30
  • @Markust, I'm not using the DataReader to save my record, I'm only using the DataReader for fetching my data. My code above are not complete bcoz some of those are not part of this problem and they are all working. Oct 22, 2012 at 3:45
  • @silent_warrior, now I understand that dr.Read() cannot be null, I really want to check for the values. Oct 22, 2012 at 3:46
  • Possible duplicate of how to check if a datareader is null or empty
    – Liam
    Jun 30, 2017 at 10:11

5 Answers 5

10

Your problem is, the while loop runs only if dr has one or more records. But, if the dr is null then the while loop will never run.

Better solution is to have a System.Data.SqlClient.SqlDataReader.

And check,

if (!dr.HasRows)
{
    // Your code to save the records, if no holidays found
}
else
{
    // Your code to show the error message, if there is one or more holidays
}
4
  • where should I add the System.Data.SqlClient.SqlDataReader? Thanks Oct 22, 2012 at 3:53
  • dr should be type of System.Data.SqlClient.SqlDataReader, I assume you are using Microsoft.VisualStudio.Data.DataReader. If not no need to define anything, just use the code above.
    – manas
    Oct 22, 2012 at 3:55
  • what is not working? Please be specific. and what do you want to achieve?
    – manas
    Oct 22, 2012 at 4:00
  • thanks for helping me out an idea. I just pass the data to be shown in datagridview then create a condition if the datagridview has value, the record will not be saved. then if the record doesn't have any record, the entered data will be saved. Oct 22, 2012 at 4:09
0

I tried my idea and it answered my question. What I did was to show the fetched data in datagridview then I created a condition if the datagridview has a record then my records will not be saved and if the datagridview doesn't have any record, then the record will be saved.

Thanks Everyone!

1
  • if you don't need to show the datagridview to the user, then just use the if condition on my answer to save the records. Adding and checking the datagridview can be an unecessary overhead
    – manas
    Oct 22, 2012 at 4:14
0

Try this:

while (sqlreader.Read())
{
    tbSubscriptionInfo.Text = sqlreader["nr_ore"].ToString();
}

if (!sqlreader.HasRows)
{
    tbSubscriptionInfo.Text = "";
}
0
if (dr == null || !dr.HasRows)
{
    // Your code to show the error message, if there is one or more holidays       
}
else
{
    // Your code to save the records, if no holidays found
}
0

while(dr.Read()) means dr has at least 1 Row.

so else will never execute

can you do else condition before while?

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.