2

I'm trying to set up the GridView to filter the Credentials that belong to a specific Employee. The dropdownlist provides the list of employees, once selected I want the gridview to only populate entries that belong to the specific employee.

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:DropDownList ID="DropDownListEmployee" runat="server"  
                      AutoPostBack="True" 
                      OnSelectedIndexChanged="SelectionHasChanged"
                      DataSourceID="SqlDataSource2" DataTextField="Fullname"
                      DataValueField="Employee_ID" AppendDataBoundItems="true"
                      Width="214px">
        <asp:ListItem>Select</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                       ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
                       SelectCommand="SELECT Employee.F_Name + ' ' + Employee.L_Name AS Fullname, Employee.Primary_Address, Employee.Primary_Phone, Employee.E_mail, Credentials.Degree, Credentials.Years_Experience, Credentials.Certifications, Credentials.Positions, Employee.Employee_ID FROM Employee INNER JOIN Credentials ON Employee.Employee_ID = Credentials.Employee_ID"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                  DataSourceID="SqlDataSource2" DataKeyNames="Employee_ID">
        <Columns> 
            <asp:BoundField DataField="Fullname" HeaderText="Fullname" 
                            ReadOnly="True" SortExpression="Fullname" />
            <asp:BoundField DataField="Primary_Address" HeaderText="Primary_Address" 
                            SortExpression="Primary_Address" />
            <asp:BoundField DataField="Primary_Phone" HeaderText="Primary_Phone" 
                            SortExpression="Primary_Phone" />
            <asp:BoundField DataField="E_mail" HeaderText="E_mail" 
                            SortExpression="E_mail" />
            <asp:BoundField DataField="Degree" HeaderText="Degree" 
                            SortExpression="Degree" />
            <asp:BoundField DataField="Years_Experience" HeaderText="Years_Experience" 
                            SortExpression="Years_Experience" />
            <asp:BoundField DataField="Certifications" HeaderText="Certifications" 
                            SortExpression="Certifications" />
            <asp:BoundField DataField="Positions" HeaderText="Positions" 
                            SortExpression="Positions" />
            <asp:BoundField DataField="Employee_ID" HeaderText="Employee_ID" 
                            InsertVisible="False" ReadOnly="True" 
                            SortExpression="Employee_ID" />
        </Columns>
    </asp:GridView>
</asp:Content>
Tim
  • 28,212
  • 8
  • 63
  • 76
nick
  • 23
  • 3

1 Answers1

1

You didn't say what happenes with your current code, but I'm betting that when you select an employee, the page reloads and you get a list of all the employees in your database, right?

If that's true, the reason is because the same SqlDataSource is bound to both your DropDownList and your GridView, and the Select command for the SqlDataSource retrieves all the employees - there is no WHERE criteria to select a desired employee.

I would use 2 SqlDataSources - one for the DropDownList, and one for the GridView. The second SqlDataSource would have a Select command to get the desired employee's information, based on the selection in the DropDownList.

You can modify your SqlDataSource2's SelectCommand to return only the fullname and EmployeeID fields, as you don't need the rest for your DropDownList:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:DropDownList ID="DropDownListEmployee" runat="server"  
                      AutoPostBack="True" 
                      OnSelectedIndexChanged="SelectionHasChanged"
                      DataSourceID="SqlDataSource2" DataTextField="Fullname"
                      DataValueField="Employee_ID" AppendDataBoundItems="true"
                      Width="214px">
        <asp:ListItem>Select</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                       ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
                       SelectCommand="SELECT F_Name + ' ' + L_Name AS Fullname, Employee_ID FROM Employee"></asp:SqlDataSource>

In your second SqlDataSource, you'll need to add a parameter (EmployeeID) to the SelectParameters collection as shown below, and update your SelectCommand to take the parameter.

    <asp:SqlDataSource ID="gvDataSource" runat="server"
                       ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
                       SelectCommand="SELECT Employee.F_Name + ' ' + Employee.L_Name AS Fullname, Employee.Primary_Address, Employee.Primary_Phone, Employee.E_mail, Credentials.Degree, Credentials.Years_Experience, Credentials.Certifications, Credentials.Positions, Employee.Employee_ID FROM Employee INNER JOIN Credentials ON Employee.Employee_ID = Credentials.Employee_ID WHERE Employee.EmployeeID = IsNull(@EmployeeID, EmployeeID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownListID" 
                 ConvertEmptyStringToNull="true" Name="EmployeeID"
                 PropertyName="SelectedValue" />
        </SelectParameters>

Then assign this SqlDataSource to the GridView instead of the first one.

Note that there is no validation by the SqlDataSource on the values submitted into the parameters - which can be a security threat.

SqlDataSource.Select Method

SqlDataSource.SelectParameters Property

Tim
  • 28,212
  • 8
  • 63
  • 76
  • Thanks for the help. Sorry to bother you, when I tried this I keep running into an error: "DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Employee_ID'." I'm guessing this is because I don't have a column in my GridView called Employee_ID? – nick Aug 22 '11 at 00:03
  • @nick - You do in the markup you posted. It's set to InsertVisible false, though. Were you trying to do an insert, or was this simply loading the employee's information? Try removing the InsertVisible="false" and see what happens (I understand why InsertVisible="false", just trying to see what the underlying problem is - you'll want to keep it when you're, so most likely EmployeeID in your database is an identity column). – Tim Aug 22 '11 at 00:26
  • Got it working, I had to delete: "Select" which was used to direct the user to make a selection, also I had to change the query for datasource1 to: "SELECT Employee_ID AS Fullname FROM Employee" or I would get the error: "Conversion failed when converting the nvarchar value 'Joe' to data type int." Which now leaves a list of numbers (ID). ~ Thanks for all the help, really appreciate it! – nick Aug 22 '11 at 01:23
  • Mistake on my part, I changed the DataValueField to Fullname when it should of been Employee_ID, anyways Thank you for the help! :P – nick Aug 22 '11 at 01:35