Kenely de Oliveira
Kenely de Oliveira

Reputation: 3

LINQ to SQL: retrieve data and put it in a textBox

I'm kinda new to LINQ, so sorry if my question is dumb.

I need to retrieve some values from a database and put them in textBoxes. Just that. Something like the code below, but using LINQ:

EDIT: actually, I want to retrieve more than one field. Like that:

SqlDataReader dr = new functionThatReturnsDataReader();
if (dr.HasRows) {
    dr.Read();

    txtId = dr["Id"].ToString();
    txtName = dr["Name"].ToString();
}

I've found this solution online:

IDbCommand command = dc.GetCommand(query);
command.Connection = dc.Connection;
command.Connection.Open();
IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

However, it seems like I'm trowing away everything that LINQ stands for if I mix it with a standard executeReader. There's no reason in building a data context and query and them execute them like ADO...

How can I achieve the same without using IDbCommand and IDataReader?

Upvotes: 0

Views: 10284

Answers (4)

Arslan jappa
Arslan jappa

Reputation: 9

If you are using entity Framework then you dont need any query just write id in text box and hit search button it will show you all records in text box of that id.

      EntityFramework_mvcEntities db = new EntityFramework_mvcEntities();


        int i =Convert.ToInt32( txtsrch.Text);
        Employee p = db.Employees.Find(i);



        TextBox1.Text = p.Name;
         TextBox2.Text = p.Email;
        TextBox4.Text = p.Mobile;

        db.SaveChanges();
    }

Upvotes: 0

eric lou
eric lou

Reputation: 1

var q = from c in context.GetTable<tbl_user>()
    where c.user_ID == lbuserid.Text.ToString()
    select new
    {
        c.Username,
        c.firstname
    };

foreach (var item in q)
{
    lbusername.Text = item.Username;
    lbfirstname.Text = item.firstname;
}

Upvotes: -1

Adam Robinson
Adam Robinson

Reputation: 185593

One thing to think about when comparing a LINQ (or Entity Framework, or other ORM) solution to an ADO.NET solution is that the ORM's are--generally--strongly typed. This means that you need to apply object-oriented principles to them.

If you're dealing with the context, query, and results all in the same function, you can do this:

using(var context = new YourContext())
{
    txtId.Text = (from t in context.YourTable
                  where t.Conditions
                  select t.Id).FirstOrDefault();
}

If they're not in the same function (and really, they shouldn't be), then something like this would work:

string FunctionThatReturnsId()
{
    using(var context = new YourContext())
    {
        return (from t in context.YourTable
                      where t.Conditions
                      select t.Id).FirstOrDefault();
    }
}

...

txtId.Text = FunctionThatReturnsId();

Upvotes: 1

tpayne84
tpayne84

Reputation: 192

I think you need to create a LINQ to SQL class from your database: http://msdn.microsoft.com/en-us/library/bb384428.aspx

Then you can treat the database table like an object and query it with linq... without the specifics of the query you are trying to run, I cannot construct the linq query for you, but creating the DBML file is the first step.

Upvotes: 1

Related Questions