Reputation: 855
Using Entity framework I want to add data to a text box. I have done this before using a connection string and myReader. But new to EF.
private void displayCust()
{
using (Entities c = new Entities())
{
cbUsers.ItemsSource = c.customer.ToList();
}
}
To display:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (Entities c = new Entities())
{
string sFirst = c.customer.ToString();
txtFirst.Text = sFirst;
}
}
Upvotes: 1
Views: 1692
Reputation: 4040
Let's say you customer object looks like this:
public class Customer
{
public int ID { get; set; }
public string Name {get;set}
}
You can get the name of the first row in your database like this:
using (Entities c = new Entities())
{
string sFirst = c.customer.FirstOrDefault().Name.ToString();
txtFirst.Text = sFirst;
}
Or the customer name with a certain ID:
using (Entities c = new Entities())
{
string sFirst = c.customer.FirstOrDefault(x => x.ID == 2).Name.ToString();
txtFirst.Text = sFirst;
}
Upvotes: 1