We can also set GridView's properties to
automatically generate columns at runtime.
Add the following code to the constructor of the class to create a connection to the
Deserts database, and this will also create an object using the Deserts DataContext.
The Deserts variable can be declared as public, as it is referred to throughout
the application.
public Deserts dataBase;
Deserts database = new Deserts("Data Source=.\sqlexpress;Initia
Catalog=Deserts;Integrated Security=true");
Building an ASP.NET Application
[ 226 ]
Now add the following code to the Page_Init event of the page to load the
categories drop-down. We will also make use of a LINQ to SQL query to fetch the list
of categories from the database.
protected void Page_Init(object sender, EventArgs e)
{
var icecreams = from cat in dataBase.Categories
select cat.Category;
DropDownList1.DataSource = icecreams;
DropDownList1.DataBind();
lblCount.Visible = false;
lblTotalItems.Visible = false;
}
Now save the application and execute it.
Pages:
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329