Warm tip: This article is reproduced from stackoverflow.com, please click
asp.net vb.net

Returning a selected value based on drop down list

发布于 2020-03-27 10:25:05

So I have a database for Orders. I recently added a column in gridview that correlates to fiscal year. I also added a drop down list above that has a list of years 2019,2020,2021. I want to make it so whenever someone selects one of the years it only returns orders in the gridview that matches the fiscal year of the dropdown to the fiscal year in the column. I don't have a SQL column that matches fiscal year....is the going to be required? I don't know if it's necessarily something that needs capturing as much as its something to make it easier to navigate through the data. For example users fillout an order for fiscal year 2020. The drop down list will then only show rows with FY column 2020

     <asp:DropDownList ID="DDYear" runat="server" AutoPostBack="True" >
      <asp:ListItem Value="1">2019</asp:ListItem>
      <asp:ListItem Value="2">2020</asp:ListItem>
      <asp:ListItem Value="3">2021</asp:ListItem>
  </asp:DropDownList>


    Dim con As New SqlClient.SqlConnection(Constraint)
    Dim cmd As New SqlClient.SqlCommand("SELECT PurchaseOrder.PoId, PurchaseOrder.Vendor_Name, PurchaseOrder.POAmount,PurchaseOrder.DateFrom, PurchaseOrder.DateTo, PurchaseOrder.Balance, PurchaseOrder.CodeId, PurchaseOrder.PoNumber, PurchaseOrder.FiscalYear, BPNumber, ClassCode.CodeId AS Expr1, ClassCode.CodeDefinition, PurchaseOrder.Notes FROM PurchaseOrder INNER JOIN ClassCode ON PurchaseOrder.CodeId = ClassCode.CodeId")

    Try
        con.Open()
        Dim sda As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        sda.Fill(ds)
        DDYear.DataSource = ds
        DDYear.DataValueField = "2019,2020,2021"
        DDYear.DataBind()
        con.Close()
    Catch ex As Exception

Getting an OnRowCancelingEdit is not a valid attribute of element DetailsView

Questioner
Joe Black
Viewed
86
S. MacKenzie 2019-07-03 22:45

It sounds like you want to filter your gridview based on the year you select in your drop down list. In your SQL, you can add a where clause to your SQL based on DateFrom, DateTo, or both, like:

Where DatePart(YEAR, PurchaseOrder.DateFrom) = @Year

then add a parameter to your query:

Dim cmd As ... 
Dim intYear as Integer = DDYear.SelectedValue 'Get the year
cmd.Parameters.Add("@Year",  SqlDbType.Int) 'Make a Parameter
cmd.Parameters("@Year").Value = intYear  'Fill parameter value

One problem in your code is that you are re-binding the DDYear to your new list, but DDYear is just the drop down list of years. You might want to do something like:

Remove this:

DDYear.DataSource = ds
DDYear.DataValueField = "2019,2020,2021"
DDYear.DataBind()

And add this:

Dim gvw As GridView = myGridView 'use your gridview name
gvw.DataSource = ds
gvw.DataBind()

This will be efficient, returning only the rows for the selected year, plus it loads the gridview instead of the drop down list with the new rows. Add your code to the OnSelectedIndexChanged event of DDYear. This will trigger it to refresh the gridview each time you change the year in your drop down list.

If you need a rows where the FiscalYear is the date used, then use that field instead of DateFrom. If you need purchase orders with a start or end date within the year, then add OR to the Where clause above using DatePart in the same way.