I would like to add the result of a LINQ 2 SQL query to a label or textbox.
I use a class “CustomerClass” as a datalayer. In here I have a method with a LINQ query.
This result needs to be returned to the code behind file and added to a label or textbox.
Method in Class:
public static object SelectCustomerByUser(string user)
{
var query = (from p in dc.Customers
where p.No_ == user
select p).Single();
return query;
}
Code behind file:
protected void Page_Load(object sender, EventArgs e)
{
string user = Membership.GetUser().UserName.ToString().ToUpper();
var queryresult = CustomerClass.SelectCustomerByUser(user);
lblStreet.Text = ?????????
}
When i set the LINQ query direclty into the code behind file I can assign lblStreet.Text directly like: lblStreet.Text = queryresult.StreetBut I want to keep the LINQ queries separatly from the code behind file.
The method
SelectCustomerByUserreturns a boxed object of Customer type so you need tounbox(type cast) it and assign properties/fields value to your user-interface.Something like:
It would be better if you change the return type of
SelectCustomerByUserand useFirstOrDefaultinstead ofSinglemethod.