Filter the grid view depending on selected index value of drop down list. RRS feed

  • Question

  • User1234927972 posted


    I currently have a database column bound to a drop down list, which I want to be used to filter what is displayed in the gridview below the drop down column. 

    I have tried assigning a query to a data adapter, that executes depending on what the selected index changed is. 

    Please find my C# code below for the Selected Index Changed. Apologies my SQL statement is so long, i needed lots of different columns from different tables in the gridview. 

    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, Stock_Take.StockDate, 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 Username =  = '" & DropDownList1.SelectedValue & "'"), con);
                DataTable table = new DataTable();
                GridView1.DataSource = table;

    However i'm getting an error on Sql Adapter line 'Operator & cannot be applied to type 'string' and 'string'

    Monday, April 16, 2018 3:29 PM

All replies

  • User-82109136 posted

    Looks like you are doing C#, so to do string concatination the "&" should be an "+".

    Monday, April 16, 2018 3:43 PM
  • User-1838255255 posted

    Hi fredbrass,


    However i'm getting an error on Sql Adapter line 'Operator & cannot be applied to type 'string' and 'string'

    According to your description and code, you could through the following way to stitching SQL statements by String.Format(). 

    Sample code: 

    SqlDataAdapter sda = new SqlDataAdapter(string.Format("select * from Data  where Id='{0}'", DropDownList1.SelectedValue), conn);
    Or Stitching strings  directly.
    SqlDataAdapter sda = new SqlDataAdapter("select * from Data  where Id='"+DropDownList1.SelectedValue+"'", conn);


    Best Regards,

    Eric Du 

    Tuesday, April 17, 2018 8:32 AM