简体   繁体   中英

How to databind a textbox to a DB field using LINQ to SQL, the right way?

Currently I'm writing an ASP.net webforms app, which works with a table containing 15 fields. I'm using LINQ to SQL which is very good to use and everything, but when i need to bind the fields with the text boxes (15 of em), whats the best way currently I'm doing this

...
...
var stud = db.Students.SingleOrdefault(d => d.ApplicationNo == 32)
textbox1.Text = stud.Name;
...
...

I feel like I'm "missing the point". Please advise on the best way to do this.

  • Also what should i do to implement the Move next and move previous functionality like a recordset

i mean how can i move to the next/previous "student" in db.Students collection.

You can bind to an ASP.NET DetailsView control; this control supports displaying one record at a time, and can page through the results, so you don't have to do all that extra work. You could also consider a FormView too, but you have to control that.

I think you have the right approach; you could also use ElementAt() to retrieve by an index; that can have some performance implications though.

HTH.

You can use the Skip and Take method .

Update after comment:

You don't have to iterate through the different properties of the retrieved Student object. They're simply there. So you could use it like this:

txtName.Text = stud.Name;
txtAge.Text = stud.Age.ToString();

...

If you want to iterate to another record you can use the Skip and Take methods.

Grz, Kris.

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