Solved

Convert LINQ query from Windows Form to Web UI

Posted on 2016-08-08
7
48 Views
Last Modified: 2016-08-09
I use this type of query a lot in my Windows Forms applications.  Is there a comparable for a web application UI?  I can see the list being created.  I just can't get it to populate in the combobox.

Any help or direction is appreciated!
Karen


Dim getDept = From id In d.tblDepts _
                      Where id.RespOffice = CStr(Me.PMComboBox.SelectedItem) _
                      Group By dpt = id.Dept Into Group _
                      Order By dpt Ascending _
                      Select dpt

        RemoveHandler Me.cbDept.SelectedIndexChanged, AddressOf Me.cbDept_SelectedIndexChanged
        Me.cbDept.DataSource = getDept.AsQueryable
        Me.cbDept.SelectedIndex = -1
        AddHandler Me.cbDept.SelectedIndexChanged, AddressOf Me.cbDept_SelectedIndexChanged
0
Comment
Question by:Karen Wilson
  • 3
  • 3
7 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 41747964
Linq query is the same for any.NET application.
Are you using web forms?
In which page event are you running the above code? Page_Load.
Please check the example in this link.
0
 

Author Comment

by:Karen Wilson
ID: 41747979
On page load I have this and I got it working with the databind().

 Dim getRspOff = (From id In d.tblDepts _
                        Group By ro = id.RespOffice Into Group _
                        Order By ro Ascending _
                        Select ro).ToList

        RemoveHandler Me.PMComboBox.SelectedIndexChanged, AddressOf Me.PMComboBox_SelectedIndexChanged
        Me.PMComboBox.DataSource = getRspOff
        Me.PMComboBox.DataBind()

        Me.PMComboBox.SelectedIndex = -1
        AddHandler Me.PMComboBox.SelectedIndexChanged, AddressOf Me.PMComboBox_SelectedIndexChanged

Next I want this code to run when I select an items from the PMCombobox.

Protected Sub PMComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PMComboBox.SelectedIndexChanged

        Dim getDept = (From id In d.tblDepts _
                       Where id.RespOffice = PMComboBox.SelectedItem.Text _
                       Group By dpt = id.Dept Into Group _
                       Order By dpt Ascending _
                       Select dpt).ToList

        Me.cbDept.DataSource = getDept
        Me.cbDept.DataBind()

        If cbDept.Items.Count = 1 Then

            Dim getSec = From id In d.tblDepts _
                    Where id.RespOffice = PMComboBox.SelectedItem.Text _
                    And id.Dept = getDept.Item(0) _
                    Group By sec = id.Section Into Group _
                    Order By sec Ascending _
                    Select sec

            Me.cbSection.DataSource = getSec
            Me.cbSection.DataBind()
        End If

    End Sub

But it's not happening.
0
 
LVL 2

Expert Comment

by:JesNoFear
ID: 41748030
so, two things you should keep in mind, autopostback will be your friend. and for your page load, be sure to account for postback (if ispostback = false then)

Expanding on what you have above, if you are trying to change lables, text boxes, or other items on your selects. You will probably want to look into Update Panels. They are pretty handy for triggering updates to portions of the page without having to reload the page. I have been using them lately with lists when selection changes to change text fields and such.

so, here is a horrible example / explanation on update panels. it makes you make one, but didn't describe what it was doing very well in my opinion https://msdn.microsoft.com/en-us/library/bb399001.aspx

This one is explained more of what happens and why.
https://msdn.microsoft.com/en-us/library/bb386454.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 41748302
this is a typical cascading dropdowns as shown in this example. (and it complements my previous link that states to use IsPostBack property)
From the example, your code should look like: (Notice that I did not add validation for clarity purposes - for production code must check whether there are contents  to display as per sample link)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
  If Not IsPostBack Then
	Dim getRspOff = (From id In d.tblDepts _
                         Group By ro = id.RespOffice Into Group _
                         Order By ro Ascending _
                         Select ro).ToList

         Me.PMComboBox.DataSource = getRspOff
         Me.PMComboBox.DataBind()
  End If
End Sub

Open in new window

Populating cbDept:
Protected Sub PMComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PMComboBox.SelectedIndexChanged
    Dim getDept = (From id In d.tblDepts _
                   Where id.RespOffice = PMComboBox.SelectedItem.Text _
                   Group By dpt = id.Dept Into Group _
                   Order By dpt Ascending _
                   Select dpt).ToList

    Me.cbDept.DataSource = getDept
    Me.cbDept.DataBind()
End Sub

Open in new window

Populating cbSection:
Protected Sub DeptComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbDept.SelectedIndexChanged
     Dim getSec = From id In d.tblDepts _
             Where id.RespOffice = PMComboBox.SelectedItem.Text _
             And id.Dept = cbDept.SelectedItem.Text _
             Group By sec = id.Section Into Group _
             Order By sec Ascending _
             Select sec

     Me.cbSection.DataSource = getSec
     Me.cbSection.DataBind()
End Sub

Open in new window

0
 

Author Comment

by:Karen Wilson
ID: 41748639
That worked!!  I had fumbled around and made the PMComboBox a databound box and then the other dropboxes played well when I clicked the postback option.  But the initial dropbox was still a mystery.  Thanks so much for your help!
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 41748678
No worries Karen- glad I can help you solve your issue, please assign the points and close the question.
Cheers from Sydney,
Miguel
0
 

Author Closing Comment

by:Karen Wilson
ID: 41748691
Sorry, thought I did that... thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now