Database Reference
In-Depth Information
OnSelectedIndexChanged="docsList_SelectedIndexChanged"
AutoPostBack="true">
</asp:ListBox>
<br />
<br />
<asp:Label ID="infoLabel" runat="server"></asp:Label>
<br />
<br />
<asp:DropDownList ID="authorList" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>
The last thing you need to do is add the events that are defined in the code above, specifically the
docsList_SelectedIndexChanged event. Click the Design tab for the Default.aspx page and double click
the list box, which will create the docsList_SelectedIndexChanged event. However, before you put code
in this event, you need to put some code in the Page_Load event as well as define a few variables. First,
add the following using statements:
using System.Data.Services.Client;
using WebRole1.TechBioServiceReference;
Next, add the following declarations. These define the DataServiceContext and URI to the data
service. You can get the URL from the Add Service Reference box shown in Figure 6-13:
private TechBioEntities context;
private Uri svcUri = new Uri("http://localhost:51176/TechBioDataService.svc");
Next, add the following code to the Page_Load event. This instantiates the data service context, and
loads the list box with available documents and the combo box with available users:
context = new TechBioEntities(svcUri);
DataServiceQuery<Doc> docs = context.Docs;
DataServiceQuery<User> users = context.Users;
foreach (Doc d in docs)
{
docsList.Items.Add(new ListItem(d.Name, d.ID.ToString()));
}
foreach (User u in users)
{
authorList.Items.Add(new ListItem(u.Name, u.ID.ToString()));
}
Finally, add the following code to the docsList_SelectedIndexChanged event. This code queries the
docs table to get the document information for the selected doc and displays the associated document
description and price in the label, then selects the author (userid) for the selected document. By the way,
the query you see below is LINQ to Entities, a LINQ (Language-Integrated Query) query that enables
developers to write queries using LINQ syntax against an Entity Framework conceptual model:
var docInfo = (from d in context.Docs
where d.ID == Convert.ToInt32(docsList.SelectedItem.Value)
select d).FirstOrDefault();
Search WWH ::




Custom Search