locked
Get a value from Dropdownlist and display a message RRS feed

  • Question

  • User899592849 posted

    Hello,

    I am trying to change a dropdownlist in VB.net. The list is being populated by complicated code in the program through a temporary table. I think.

    The dropdownlist is:

    Dropdownlist1
    (Items) (Quantity)
    (laptop ---- 90)
    (Keyboard----100)
    (Mouse-------0)
    (PC----------25)

    I would like to do is When an item is selected with quantity is 0 then a message popup "No more, select another item" or something indicate the item is no longer available and blocking users from choosing that item.  In this case, When clicking on Mouse would have a message pop up.  Thanks

    I tried the following....but nothing worked yet.

    First code:
    if (dropdownlist1.SelectedIndex = 2) Then label1.Enabled = true End If
    Second code: Dim qValue As integer = DropDownList.SelectedItem.Value if qValue < 0 Then label1.Enabled = true End If

    Monday, January 21, 2019 7:29 AM

All replies

  • User899592849 posted

    I am taking a slightly different approach by checking the ID and the quantity as the dropdownlist1 populate.  Code sample is below.  i am able to get the label fire correctly base on the set condition. sample code below.  However, I am unable to disable/disappear the list item with 0 quantity. How do I make the item in the list with 0 quantity disable? Please advice.  thanks

    If ((ds.Tables(0).Rows(i)("ID") = 6) And (Quantity = 0)) Then
        label1.Visible = True 'works
    dropdownlist1.Items.Remove(dropdownlist1.SelectedValue) ' not working,
    dropdownlist1.Items.Remove(dropdownlist1.SelectedItem) ' not working dropdownlist1.Items.Remove("disabled") ' not working
    else if ((ds.Tables(0).Rows(i)("ID") = 10) And (Quantity = 0)) Then label2.Visible = True 'works

    dropdownlist1.Items.Remove(dropdownlist1.SelectedValue) dropdownlist1.Items.Remove(dropdownlist1.SelectedItem) dropdownlist1.Items.Remove("disabled")
    else if ((ds.Tables(0).Rows(i)("ID") = 12) And (Quantity = 0)) Then label.Visible = True 'works

    dropdownlist1.Items.Remove(dropdownlist1.SelectedValue) dropdownlist1.Items.Remove(dropdownlist1.SelectedItem) dropdownlist1.Items.Remove("disabled")
    End if

    Monday, January 21, 2019 5:46 PM
  • User-893317190 posted

    Hi sn002, 

    Why not  write a query that filtering items whose quantity is zero?

    For example , select * from table where quantity !=0, and then bind your data to dropdownlist.

    Or if you could control your sql , you could filter the data in your code behind.

    For example, if the table I get from database has the following data.

     Dim table As DataTable = New DataTable()
            table.Columns.Add(New DataColumn("items", GetType(String)))
            table.Columns.Add(New DataColumn("quantity", GetType(Int32)))
            table.Rows.Add("laptop", 90)
            table.Rows.Add("keyboard", 100)
            table.Rows.Add("mouse", 0)
            table.Rows.Add("pc", 25)
    

    Then you could filter items whose quantity is 0. Linq is a quick way.

    table.AsEnumerable() make the table could be queried with linq. 

    Function(row) Convert.ToInt32(row("quantity")) <> 0 is a predicate that means if quantity <> 0 return true so that rows whose quantity is not zero will be chosen.

    row in Function(row)  represents every row in the datatable.

    CopyToDataTable() converts the result to datatable.

     table = table.AsEnumerable().Where(Function(row) Convert.ToInt32(row("quantity")) <> 0).CopyToDataTable()

    Then you could bind.

            DropDownList1.DataValueField = "quantity"
            DropDownList1.DataTextField = "items"
            DropDownList1.DataSource = table
            DropDownList1.DataBind()

    Best regards,

    Ackerly Xu

    Tuesday, January 22, 2019 2:07 AM
  • User899592849 posted

    Hi Ackerly,

    Thanks for your input. I will explore your suggestions.  I like the idea of quantity<>0. I don't deal a lot with coding so it is going to be a big challenge for me to make a change to the code. The Linq query is above me.

    Wednesday, January 23, 2019 4:45 PM
  • User-893317190 posted

    Hi sn002,

    If you are not familiar with linq, you could also try the code below , it is more complex but simple to understand.

      Dim table As DataTable = New DataTable()
            table.Columns.Add(New DataColumn("items", GetType(String)))
            table.Columns.Add(New DataColumn("quantity", GetType(Int32)))
    
    
            table.Rows.Add("laptop", 90)
            table.Rows.Add("keyboard", 100)
            table.Rows.Add("mouse", 0)
            table.Rows.Add("pc", 25)
    
    
           
            Dim newTable As DataTable = New DataTable() 'create a new table
            newTable.Columns.Add(New DataColumn("items", GetType(String)))
            newTable.Columns.Add(New DataColumn("quantity", GetType(Int32)))
    
            For Each row As DataRow In table.Rows  'add the data of row's whose's quantity<>0  
                                                    'to the newtalbe
                If Convert.ToInt32(row("quantity")) <> 0 Then
                    newTable.Rows.Add(row("items"), row("quantity"))
                End If
            Next
    
            DropDownList1.DataValueField = "quantity"  
            DropDownList1.DataTextField = "items"
            DropDownList1.DataSource = newTable      'bind the new table
            DropDownList1.DataBind()

    Best regards,

    Ackerly Xu

    Thursday, January 24, 2019 5:51 AM
  • User899592849 posted

    Hi Ackerly,

    I appreciated your assistance and your patience. But I am still learning to understand about this task and other duties. From what I see the DropDownList1 is being populated using VB.net code that pull from a stored procedure which pull from multiple tables with some sort of calculation. I am trying to change the query in store procedure to exclude 0 but no luck. I tried to incorporate your code example but did not work since I am a newbie to VB.net...arg.

    Wednesday, January 30, 2019 3:55 AM
  • User-893317190 posted

    Hi sn002,

    Not clearly about your table and query , but you could use CTE to help you store the result of your query as  a temp result and then you could filter the item whose quantity is 0.

    For example , your original query is    select * from table where a='b' order by ... group by ... and even more complex.

    Then your could write as follows.

      with temp as (
      
         // your query to execute a complex query with 0 quantity
      
      )
      select * from temp where quantity <> 0

    temp is the identifier to refer to your original result, you could specify your own identifier.

    Best regards,

    Ackerly Xu

    Wednesday, January 30, 2019 4:15 AM