0

I am using this code to retrieve a table record from a table, I want to fill every cell data in a separate text box, what I can do next?

      Dim match = From p In students_entities.StudentsInformations
                                    Where p.ID = id
                                    Select p 
txtfirstName.text=????
5
  • what exactly do u use? or do u need? asp? Commented Apr 29, 2013 at 7:40
  • I am using vb.net code @Roar Commented Apr 29, 2013 at 7:43
  • I see, but what do u need to do with ur data? Commented Apr 29, 2013 at 7:44
  • I want for example retrieve the first name of a student who has id=1, when I use the previous statement it will retrieve a student record, how can I get his first name from the retrieved record? Commented Apr 29, 2013 at 7:47
  • txtfirstName.Text = p.First().FirstName assuming there is a match and you want to show the first match only. Commented Apr 29, 2013 at 7:52

2 Answers 2

1

in case when u select by id there is only one record should be, so u can do this

Dim match = (From p In students_entities.StudentsInformations
                                    Where p.ID = id
                                    Select p).FirstOrDefault
If match IsNot Nothing Then
   txtfirstName.text= match.FirstName
End If

or

Dim match = students_entities.StudentsInformations.FirstOrDefault(Function(f) f.ID = id)
If match IsNot Nothing Then
   txtfirstName.text= match.FirstName
End If
Sign up to request clarification or add additional context in comments.

1 Comment

@Alaa, keep in mind that second one will be better about performance, NOT IN THIS ONE, but in case it's some collection. So my tip: always use second approach.
0

You should tell the EF that you want First row. Use the FirstOrDefault or Single function.

Dim match = From p In students_entities.StudentsInformations
                                Where p.ID = id
                                Select p 
txtfirstName.text= match.FirstOrDefault().name

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.