简体   繁体   中英

Show a message-box when the database is empty

thanks for viewing my question. Basically, when the user clicks on a button, it will either say one of the following in a message box based on what the database has: Your holiday has been authorised, your holiday has been declined or your holiday request has been sent.

I want it so that when the user clicks on the button and there isn't any data in the database because the user hasn't sent a holiday request, to receive a message box saying that they haven't booked an holiday.

Here's my code:

            private void button2_Click_1(object sender, EventArgs e)
    {
        System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["Login"];
        SundownDatabaseEntities6 db = new SundownDatabaseEntities6();
        int id = Convert.ToInt32(((Login)f).idTb.Text);

        var getrecords = db.Holidays.Where(a => a.Id == id).ToList();

            foreach (var record in getrecords)
            {
                if (record.YesOrNo == "yes")
                {
                    MessageBox.Show("The manager has accepted your holiday (From " + record.Datefrom + " - " + record.Dateto + ").");
                }
                else if (record.YesOrNo == "no")
                {
                    MessageBox.Show("The manager has declined your holiday request (" + record.Datefrom + " - " + record.Dateto + ").");
                }
                else if (record.YesOrNo == null)
                {
                    MessageBox.Show("Your holiday request (" + record.Datefrom + " - " + record.Dateto + ") has been sent.\nWaiting for manager to authorise it...");
                }
                else if (record != null)
            {
                MessageBox.Show("You have not booked an holiday.");
            }
            }
        }

Problem is on the last bit of the code, the 'else if(record != null)' doesn't check if the database is empty. Any suggestions? Thanks!

You should check getrecords.Count()

var getrecords = db.Holidays.Where(a => a.Id == id).ToList();
if (getrecords.Count() == 0) 
{
 //  ... here your logic
}

Or

if (!db.Holidays.Any ())

Because it won't go to foreach if getrecords is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM