开发者

Populating textboxes using LINQ2SQL

开发者 https://www.devze.com 2023-03-05 14:18 出处:网络
In my web forms, I have text boxes and one radiolist which I need to populate through a开发者_Python百科 LINQ2SQL query. So far I coded this query to fetch particular records which is going to be popu

In my web forms, I have text boxes and one radiolist which I need to populate through a开发者_Python百科 LINQ2SQL query. So far I coded this query to fetch particular records which is going to be populated into the DB.

 using (dbTestDataContext db = new dbTestDataContext())
        {
            var query = from r in db.Table1
                        where r.Code == getCode
                        select new
                        {
                            //Account Info
                            r.Name,
                            r.Num,
                            r.AcctNum,
                            r.CorpAcct, //Bool


                        };
        };

Now I know that the record which this query is going to be fetching is gonna be only 1 unique record. I want to show the record in these textboxes and select that radio button:

tbName

tbNum

tbAcctNum

rbtnCorpAcct

How should I do this? Thank you in advance!


Very simply:

 using (dbTestDataContext db = new dbTestDataContext())
        {
            var query = (from r in db.Table1
                        where r.Code == getCode
                        select new
                        {
                            //Account Info
                            r.Name,
                            r.Num,
                            r.AcctNum,
                            r.CorpAcct, //Bool


                        }).FirstOrDefault();

             if (query != null)
             {
                tbName.Text = query.Name;
                tbNum.Text = query.Num;
                //and so on
                rbl.SelectedValue = query.SomeValue;
             }
        };


Same as others have answered with addition of radio button:

tbName.Text = query.Name;
tbNum.Text = query.Num;
tbAcctNum.Text = query.AcctNum;

if(query.CorpAcct)
    rbtn.SelectedValue = "Yes"; \\Where "Yes" is one of the radio button values
else
    rbtn.SelectedValue = "No";

\\Could also use SelectedIndex, rbtn.SelectedIndex = 0 or 1


Try the following:

using (dbTestDataContext db = new dbTestDataContext())
{
    var query = 
    (
        from r in db.Table1
        where r.Code == getCode
        select new
        {
            //Account Info
            r.Name,
            r.Num,
            r.AcctNum,
            r.CorpAcct, //Bool
        }
    ).FirstOrDefault();

    tbName.Text = query.Name;

    ....

};


The first thing you need to do is retrieve a single result from your query. As you have it written, you are returning an IQueryable object which is now stored in the variable "query"

To get a single object, do this

var myObject = query.SingleOrDefault();

Then you can access the individual properties of that object and assign them like this

tbName.Text = myObject.Name

0

精彩评论

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