Question

I have a movie database,a listbox on the form show the titles. If the user select a dvd a want to rent it the app must check if it that movie title(foreign key) already in the rent table. If yes write it is in the database,if not write:not in the database. If i use the code below i get the error when the movie title is not in the rent table so there is some problem with that NULL. How can i fix this?

private void bt_Letsrent_Click(object sender, EventArgs e)
        {
            var c1 = (from s in db.Rent where s.Movietitle == (string)listbox1.SelectedValue select s).FirstOrDefault();

            if (c1.Movietitle==null)
            {
                MessageBox.Show("Not in the database");
            }
            else
            {
                MessageBox.Show("In the database");
            }
}
Was it helpful?

Solution

You can get count of items which meet the condition like that:

int count= db.Rent.Count(s => s.Movietitle == (string)listbox1.SelectedValue);

if (count>0)
{
    MessageBox.Show("In the database");
}
else
{
    MessageBox.Show("Not in the database");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top