Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I currently have a drop down list that has values from a column in a database bound to it. I have c# code that i need to change the displayed data in the gridview depending on the selected value from the drop down list.

At the moment, there is no errors at all when running, however the gridview is not appearing.

Any help will be appreciated!

Page Load C#
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlConnection con = new SqlConnection(@"Data Source=(local)\;Initial Catalog=SmallBatch;Integrated Security=True;");

                con.Open();

                String getState = "Select Username From Site ";


                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(getState, con);
                da.Fill(dt);


                DropDownList1.DataSource = dt;
                DropDownList1.DataTextField = "Username";
                DropDownList1.DataValueField = "Username";
                DropDownList1.DataBind();


                con.Close();
            }


Selected Index Changed C#
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
       {
           SqlConnection con = new SqlConnection(@"Data Source=(local)\;Initial Catalog=SmallBatch;Integrated Security=True;");
           SqlDataAdapter DataAdapter = new SqlDataAdapter(string.Format("SELECT Stock_Take.Username, Item.ItemID, Item.ItemDesc, Stock_Take_Item.BarQuantity, Stock_Take_Item.StorageQuantity, Stock_Take.StockTakeIDNew FROM Item INNER JOIN Stock_Take_Item ON Item.ItemID = Stock_Take_Item.ItemID INNER JOIN Stock_Take ON Stock_Take_Item.StockTakeIDNew = Stock_Take.StockTakeIDNew where Stock_Take.Username =  = '" + DropDownList1.SelectedValue + "'"), con);

           DataTable table = new DataTable();
           DataAdapter.Fill(table);
           GridView1.DataSource = table;
           GridView1.DataBind();
       }


Gridview code

ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
            
            <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal">
                <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
                <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F7F7F7" />
                <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
                <SortedDescendingCellStyle BackColor="#E5E5E5" />
                <SortedDescendingHeaderStyle BackColor="#242121" />

                <Columns>
                    <asp:TemplateField HeaderText="Item ID" HeaderStyle-CssClass="gridview-header">
                        <ItemTemplate>
                            <asp:Label ID="itemIDAdmin" Text='<%# Eval("ItemID")%>' runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="Item Description" HeaderStyle-CssClass="gridview-header">
                        <ItemTemplate>
                            <asp:Label ID="itemDescAdmin" Text='<%# Eval("ItemDesc")%>' runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                      
                    <asp:TemplateField HeaderText="Bar Quantity" HeaderStyle-CssClass="gridview-header">
                        <ItemTemplate>
                            <asp:Label ID="barQuantityAdmin" Text='<%# Eval("BarQuantity")%>' runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField HeaderText="Storage Quantity" HeaderStyle-CssClass="gridview-header">
                        <ItemTemplate>
                            <asp:Label ID="storageQuantityAdmin" Text='<%# Eval("StorageQuantity")%>' runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>


What I have tried:

I have tried populating the gridview in a seperate method, but still same result.
Posted
Comments
an0ther1 16-Apr-18 17:21pm    
Use your debugger - does the event handler DropDownList1_SelectedIndexChanged fire?
Does the query actually return results? It may be a typo but your query has the following in the where clause;
Stock_Take.Username = = '" + DropDownList1.SelectedValue + "'"
Note the two equals signs - that is not valid

Kind Regards
Member 13658881 17-Apr-18 3:29am    
set autopostback=true
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" autopostback="true">
Member 13658881 17-Apr-18 3:31am    
set stock_Take.Username='"+DropDownList1.SelectedValue+"'"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900