0

I have a DropDownList that's databound to our SQL server with an ID, First Name, Last Name. I've then added a GridView and Configured the Data Source to this

SELECT [ID], [fName], [lName] FROM [cb1] WHERE ([ID] = @ID)

Basically what I want it to do is when someone selects the first name from the dropdown it will pop up with the ID, First Name, and Last Name in the GridView.

The Problem is, when it's loaded the DropDownList is empty and has no data, and the GridView is also missing.

Here is the markup.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display.">
        <Columns>
            <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
            <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" 
                SortExpression="ID" />
            <asp:BoundField DataField="fName" HeaderText="fName" SortExpression="fName" />
            <asp:BoundField DataField="lName" HeaderText="lName" SortExpression="lName" />
        </Columns>
    </asp:GridView>
    <asp:DropDownList ID="DropDownList1" runat="server" 
        DataSourceID="SqlDataSource1" DataTextField="ID" DataValueField="ID">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:comboConnectionString1 %>" 
        DeleteCommand="DELETE FROM [cb1] WHERE [ID] = @ID" 
        InsertCommand="INSERT INTO [cb1] ([fName], [lName]) VALUES (@fName, @lName)" 
        ProviderName="<%$ ConnectionStrings:comboConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [ID], [fName], [lName] FROM [cb1] WHERE ([ID] = @ID)" 
        UpdateCommand="UPDATE [cb1] SET [fName] = @fName, [lName] = @lName WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="fName" Type="String" />
            <asp:Parameter Name="lName" Type="String" />
        </InsertParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="ID" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="fName" Type="String" />
            <asp:Parameter Name="lName" Type="String" />
            <asp:Parameter Name="ID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
</p>
2
  • What is the question? And where is the code behind? Commented Jul 26, 2012 at 17:54
  • can you share the sample code behind code plz ? Commented Jul 26, 2012 at 18:04

2 Answers 2

1
Table Contains id,Name,department and age


 Department :
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource2" DataTextField="Department" 
            DataValueField="Department">
        </asp:DropDownList>
        <br />
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:demoConnectionString %>" 
            SelectCommand="SELECT DISTINCT [Department] FROM [profile]">
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="id" DataSourceID="SqlDataSource3">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                    ReadOnly="True" SortExpression="id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Department" HeaderText="Department" 
                    SortExpression="Department" />
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:demoConnectionString %>" 
            SelectCommand="SELECT * FROM [profile] WHERE ([Department] = @Department)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Department" 
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

Result:
![Result][1]


  [1]: https://i.sstatic.net/WrfP7.png
Sign up to request clarification or add additional context in comments.

Comments

1

The Problem is, when it's loaded the DropDownList is empty and has no data, and the GridView is also missing.

When your page first time loads it fires this Sql query (SELECT [ID], [fName], [lName] FROM [cb1] WHERE ([ID] = @ID) to return results for sqldatasource1 which is used by the grid and the combo

means no results will be returned because @ID will be null or 0

So you will have (SELECT [ID], [fName], [lName] FROM [cb1] WHERE ([ID] = @ID OR [ID] IS NOT NULL)

3 Comments

@WaqarJanjua your absolutely right i will be happy to upvote you if you want to put your suggestion as answer
Because ID is a primary key. Will it let me use the parse ID IS NOT NULL ??
@vadim yes i have tested this query and it will work even if the ID column have a primary key on it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.