I was working with ASP.NET and I try to change a label text and get the value from data base.
here is what i do.
First I add database then I create a linq and then I use query to load it but it won't work.
this is query code
protected void Page_Load(object sender, EventArgs e)
DataClasses1DataContext db = new DataClasses1DataContext();
var q = from dc in db.topboxes
where dc.id == (1)
Lmain11.Text = q.ToString();
SELECT [t0].[id], [t0].[title], [t0].[dc], [t0].[adminid] FROM [dbo].[topbox] AS [t0] WHERE [t0].[id] = @p0
where id = 1
Linq queries return an
IEnumerable<T> and you want to access a specific item in the collection, to a specific property. Use
FirstOrDefault(). (Read here for more about
DataClasses1DataContext db = new DataClasses1DataContext(); var q = (from dc in db.topboxes where dc.id == 1 select dc).FirstOrDefault(); Lmain11.Text = q?.SomeProperty;
FirstOrDefault might return
default(T) if such was not found in query. In your case the default is
null. Use C# 6
?. operator to safely access the desired property
Using on a linq-2-sql query
.ToString() will give you the sql that is being generated - Great when you want to see under the hood :)