user3515179
user3515179

Reputation: 13

How to read from database

I tryed to data fro database using entity framework and c# language like this:

 protected void Searchbutton_Click(object sender, EventArgs e)
        {
            StudentDBModelContainer db = new StudentDBModelContainer();
            var student = new Student();
            student =(from i in db.StudentSet where i.Id==2 select i).First();
            TextBox1.Text = student.LastName;
    
        }

but it result this problem:

Erreur du serveur dans l'application '/'.

La séquence ne contient aucun élément.

Description : Une exception non gérée s'est produite au moment de l'exécution de la requête Web actuelle. Contrôlez la trace de la pile pour plus d'informations sur l'erreur et son origine dans le code. 

Détails de l'exception: System.InvalidOperationException: La séquence ne contient aucun élément.

Erreur source: 


Ligne 19 :             StudentDBModelContainer db = new StudentDBModelContainer();
Ligne 20 :             var student = new Student();
Ligne 21 :             student =(from i in db.StudentSet where i.Id==2 select i).First();
Ligne 22 :             TextBox1.Text = student.LastName;
Ligne 23 :     

Fichier source : d:\aimen\Projects\Motashaiba\Motashaiba\Default.aspx.cs    Ligne : 21

English translation:

Server Error in '/' Application.

The sequence contains no elements.

Description: An unhandled exception occurred during the execution of the current web request. Check the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The sequence contains no elements.

Upvotes: 0

Views: 156

Answers (2)

Grant Winney
Grant Winney

Reputation: 66439

Use FirstOrDefault() instead, then check to see if student is null (which it will be, if no records are returned).

student = (from i in db.StudentSet where i.Id == 2 select i).FirstOrDefault();

if (student != null)
    TextBox1.Text = student.LastName;

Also, you could shorten this a bit using method syntax:

protected void Searchbutton_Click(object sender, EventArgs e)
{
    var db = new StudentDBModelContainer();

    var student = db.FirstOrDefault(s => s.Id == 2);

    if (student != null)
        TextBox1.Text = student.LastName;
}

While the above code will not find the record if it doesn't exist in the database, it will prevent an exception from being thrown if the expected record is not found.

Your query is extremely basic and should work just fine, if the record actually exists. Double-check the connection string and make sure you're connecting to the correct database.

Upvotes: 1

Rob Allen
Rob Allen

Reputation: 2911

Only use First() if you know there will be at least one record. In your case, if you use FirstOrDefault() as suggested by another commenter, you will then get null.

Is it ok that it's null?

I often use First() when I know that there will be data and I want to be notified of the error when there is no data. Basically meaning, if the database is jacked, or someone deleted crucial data, then I want to immediate feedback that stuff is broken, that said it really depends on what you are doing.

Upvotes: 0

Related Questions