开发者

ASP.NET LINQ SQL get specific fields

I am trying o set textboxes on an ASP.NET webpage using LINQ - SQL. Here is the code I have to perform the select statement:

开发者_高级运维    EQCN = Request.QueryString["EQCN"];
    var equipment = from n in db.equipments
                    where n.EQCN.ToString() == EQCN
                    select n;

How do I set TextBox1.text to be a specific field in the table?

Thanks so much

EDIT

I need to output every field in the table into different textboxes. So performing a query for ever single one seems a little much. There has to be a way to do this?

Thanks


Well you can select the appropriate field to start with:

EQCN = Request.QueryString["EQCN"];
var values = from n in db.equipments
             where n.EQCN.ToString() == EQCN
             select n.FieldYouWant;

// Or possibly Single, or First...
var singleValue = values.FirstOrDefault();

I think that's what you were after, but if it's not, please clarify your question.

EDIT: To answer your follow-up, you can use:

EQCN = Request.QueryString["EQCN"];
var query = from n in db.equipments
             where n.EQCN.ToString() == EQCN
             select n;

// Or possibly Single, or First...
var entity = query.Single();

textBox1.Text = entity.Name;
textBox2.Text = entity.Description;
textBox3.Text = entity.Title;
// etc

That's assuming you want to have access to everything in the entity. If the entity is very large and you only need a few fields, you might want to do something like this:

EQCN = Request.QueryString["EQCN"];
var query = from n in db.equipments
             where n.EQCN.ToString() == EQCN
             select new { n.Name, n.Description, n.Title };

// Or possibly Single, or First...
var projection = query.Single();

textBox1.Text = projection.Name;
textBox2.Text = projection.Description;
textBox3.Text = projection.Title;

I'm not sure I'd actually couple the data access and UI layers so closely, but that's a different matter...


You only need to perform the query once, but once that's done, you'll have to assign each field to a TextBox. Start by retrieving only the single item you want:

EQCN = Request.QueryString["EQCN"];
var equipment = (from n in db.equipments
                 where n.EQCN.ToString() == EQCN
                 select n).FirstOrDefault();

Then go through and assign each TextBox to the appropriate field:

txtName.Text = equipment.Name;
txtDescription.Text = equipment.Description;
txtValue1.Text = equipment.Value1;
txtValue2.Text = equipment.Value2;
//...

If you have several dozen TextBoxes to assign, you could set up a custom control that can be databound to an equipment object, but even then, you'll still have to write the binding code for your control.

The only way I can think of to totally automate this process is to name each TextBox after a field in your object, then use reflection to match them to values:

    var textboxes = Panel1.Controls.OfType<TextBox>();

    foreach (TextBox txt in textboxes)
    {
        string fieldname = txt.ID.Remove(0, 3); //"txtDescription" becomes "Description"

        string value = equipment.GetType().GetProperty(fieldname).GetValue(equipment, null) as string;
        txt.Text = value;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜