ASP.NET: Use ID from query against a SQL Server view used in a GridView to control DetailsView that queries a table
<p>Please forgive my ignorance. I have a done a bit of web programming but am new to ASP.NET and find it mystifying when trying to do something not out of the box.</p>

<p>I have a GridView that gets its data from a SQL Server view. I have a DetailsView that needs to manipulate data in a table. I cannot figure out how to link the GridView to the DetailsView correctly.</p>

<p>Below is the code for the section of the aspx page that contains the GridView and DetailsView. The latest error in an endless procession of errors is: Must declare the scalar variable "@ID".</p>

<p>I would appreciate any help that anyone can offer. Thanks.</p>

<pre><code>&lt;asp:GridView ID="gvVendorContacts" runat="server" AutoGenerateColumns="False" 
    DataSourceID="dsApplications_Contacts" AllowSorting="True" CellPadding="4"   CellSpacing="2" AutoGenerateSelectButton="True" DataKeyNames="ContactID"
    onselectedindexchanged="gvVendorContacts_SelectedIndexChanged"&gt;
    &lt;AlternatingRowStyle BackColor="#E8F3FF" /&gt;
    &lt;Columns&gt;
        &lt;asp:BoundField DataField="FullName" HeaderText="Name" ReadOnly="True" 
            SortExpression="FullName" /&gt;
        &lt;asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /&gt;
        &lt;asp:BoundField DataField="Role" HeaderText="Role" SortExpression="Role" /&gt;
        &lt;asp:BoundField DataField="Office Phone" HeaderText="Office Phone" 
            SortExpression="Office Phone" /&gt;
        &lt;asp:BoundField DataField="Mobile Phone" HeaderText="Mobile Phone" 
            SortExpression="Mobile Phone" /&gt;
        &lt;asp:TemplateField HeaderText="Email" SortExpression="Email"&gt;
            &lt;ItemTemplate&gt;
                &lt;a href="Mailto:&lt;%# Eval("Email") %&gt;"&gt;&lt;%#Eval("Email")%&gt;&lt;/a&gt;
            &lt;/ItemTemplate&gt;
        &lt;/asp:TemplateField&gt;
    &lt;/Columns&gt;
&lt;/asp:GridView&gt;
&lt;asp:SqlDataSource ID="dsApplications_Contacts" runat="server" 
    ConnectionString="&lt;%$ ConnectionStrings:MDSConnectionString %&gt;" 
    SelectCommand="SELECT [ApplicationID], [ContactID], [FName], [LName], [FullName], [Title], [Role], [OfficePhone] As [Office Phone], [MobilePhone] As [Mobile Phone], [Email] FROM [vw_Applications_Contacts] WHERE ApplicationName = 'Bloomberg'"&gt;
&lt;/asp:SqlDataSource&gt;




 &lt;asp:DetailsView ID="dvVendorContacts" runat="server" AutoGenerateRows="False" 
     DataKeyNames="ID" DataSourceID="dsVendorContacts" Height="100px" 
     Width="200px" BackColor="White" BorderColor="#CC9966" BorderStyle="None" 
     BorderWidth="1px" CellPadding="4"&gt;
     &lt;EditRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /&gt;
     &lt;Fields&gt;
         &lt;asp:BoundField DataField="ContactID" HeaderText="ID" InsertVisible="False" 
             ReadOnly="True" SortExpression="ID" /&gt;
         &lt;asp:BoundField DataField="FName" HeaderText="FName" SortExpression="FName" /&gt;
         &lt;asp:BoundField DataField="LName" HeaderText="LName" SortExpression="LName" /&gt;
         &lt;asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /&gt;
         &lt;asp:BoundField DataField="Role" HeaderText="Role" SortExpression="Role" /&gt;
         &lt;asp:BoundField DataField="OfficePhone" HeaderText="OfficePhone" 
             SortExpression="OfficePhone" /&gt;
         &lt;asp:BoundField DataField="MobilePhone" HeaderText="MobilePhone" 
             SortExpression="MobilePhone" /&gt;
         &lt;asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /&gt;
     &lt;/Fields&gt;
     &lt;FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /&gt;
     &lt;HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /&gt;
     &lt;PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /&gt;
     &lt;RowStyle BackColor="White" ForeColor="#330099" /&gt;
 &lt;/asp:DetailsView&gt;

 &lt;asp:SqlDataSource ID="dsVendorContacts" runat="server" 
     ConnectionString="&lt;%$ ConnectionStrings:MDSConnectionString %&gt;" 
     DeleteCommand="DELETE FROM [Contacts] WHERE [ID] = @ID" 
     InsertCommand="INSERT INTO [Contacts] ([FName], [LName], [Title], [Role], [OfficePhone], [MobilePhone], [Email]) VALUES (@FName, @LName, @Title, @Role, @OfficePhone, @MobilePhone, @Email)" 
     SelectCommand="SELECT [ID], [FName], [LName], [Title], [Role], [OfficePhone], [MobilePhone], [Email] FROM [Contacts] WHERE ([ID] = @ID)" 
     UpdateCommand="UPDATE [Contacts] SET [FName] = @FName, [LName] = @LName, [Title] = @Title, [Role] = @Role, [OfficePhone] = @OfficePhone, [MobilePhone] = @MobilePhone, [Email] = @Email WHERE [ID] = @ID"&gt;
     &lt;DeleteParameters&gt;
         &lt;asp:Parameter Name="ID" Type="Int32" /&gt;
     &lt;/DeleteParameters&gt;
     &lt;InsertParameters&gt;
         &lt;asp:Parameter Name="FName" Type="String" /&gt;
         &lt;asp:Parameter Name="LName" Type="String" /&gt;
         &lt;asp:Parameter Name="Title" Type="String" /&gt;
         &lt;asp:Parameter Name="Role" Type="String" /&gt;
         &lt;asp:Parameter Name="OfficePhone" Type="String" /&gt;
         &lt;asp:Parameter Name="MobilePhone" Type="String" /&gt;
         &lt;asp:Parameter Name="Email" Type="String" /&gt;
     &lt;/InsertParameters&gt;
     &lt;UpdateParameters&gt;
         &lt;asp:Parameter Name="FName" Type="String" /&gt;
         &lt;asp:Parameter Name="LName" Type="String" /&gt;
         &lt;asp:Parameter Name="Title" Type="String" /&gt;
         &lt;asp:Parameter Name="Role" Type="String" /&gt;
         &lt;asp:Parameter Name="OfficePhone" Type="String" /&gt;
         &lt;asp:Parameter Name="MobilePhone" Type="String" /&gt;
         &lt;asp:Parameter Name="Email" Type="String" /&gt;
         &lt;asp:Parameter Name="ID" Type="Int32" /&gt;
     &lt;/UpdateParameters&gt;
     &lt;SelectParameters&gt;
        &lt;asp:ControlParameter ControlID="gvVendorContacts" DefaultValue="NULL" 
        Name="ContactID" PropertyName="SelectedValue" Type="Int32" /&gt;
     &lt;/SelectParameters&gt;
 &lt;/asp:SqlDataSource&gt;
</code></pre>
