Posts

Showing posts from April, 2014

Data Entity Random Database Record

You've seen those random quotes on pages, probably most of them are done with Javascript.  This one is done using a database table (three fields, id, linenumber,linetext) and a data entity call and features the SKIP method. You have to have an orderby in your query to be able to use SKIP So put a label on the page or webcontrol and format it as you wish From the page load call the function to get the data lblRandom.InnerText = getrandomvalue();  public string getrandomvalue()         {             using (my_maintenanceEntities context = new my_maintenanceEntities())             {                 var query = (from c in context.tbl_texttable                              orderby c.LineNumber                              select c.LineText);                 int count = query.Count();                 int index = new Random().Next(count);                           return query.Skip(index).FirstOrDefault();             }         }