PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Entity Framework, Classes, and Comboboxes (How to Fill)-VBForums
Results 1 to 9 of 9

Thread: [RESOLVED] Entity Framework, Classes, and Comboboxes (How to Fill)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2019
    Posts
    22

    Resolved [RESOLVED] Entity Framework, Classes, and Comboboxes (How to Fill)

    First off, I'll admit that I am not very familiar with EF or Classes, but I am trying to learn.

    I am trying to find the best way to fill comboboxes by either straight query to the table(s) using EF or by building classes. I also need to be able to add a "NULL" or String.Empty value as the first option. When using DataSource it doesn't let me use .add to manually add them. I have tried to use a JOIN with the EF query to insert a NULL value, but was not successful.

    I have had a little bit of luck figuring out how to do classes and bind a CB when a single column is needed (without NULL value added,) but I run into issues when I need a DisplayMember and ValueMember from two different columns. I have tried to create both a DataView and DataTable and use those as the DataSource, but I am getting conversion errors, (see below,) which I also get when I'm trying to do the classes. I'm sure it's because I don't know what I'm doing or understand Classes/EF enough, but I cannot find the answer to my issue.

    ERRORS:
    Name:  Errors.jpg
Views: 114
Size:  45.1 KB

    Here is the CLASS code that I currently have (not exactly everything I have tried):
    Code:
    Public Class cbox_Last_Name
        'Single Column
        Private ReadOnly Property Last_Name As List(Of String)
        Public Sub New()
            Using dbACL As New ACL_Entities
                Last_Name = From items In dbACL.data_Demog
                            Select items.Last_Name.ToList()
            End Using
        End Sub
    End Class
    
    Public Class cbox_First_Name
        'Single Column
        Private ReadOnly Property First_Name As List(Of String)
        Public Sub New()
            Using dbACL As New ACL_Entities
                First_Name = From items In dbACL.data_Demog
                             Select items.First_Name.ToList()
            End Using
        End Sub
    End Class
    
    Public Class cbox_DOB
        'Single Column
        Private ReadOnly Property dDOB As List(Of Date?)
        Public Sub New()
            Using dbACL As New ACL_Entities
                dDOB = (From items In dbACL.data_Demog
                        Select items.DOB).ToList()
            End Using
        End Sub
    End Class
    
    Public Class cbox_DGroup
        'Two Column
        Public Property Name_DGroup As String
        Public Property ID_DGroup As String
    End Class

    And here is the code on the form that is supposed to load the comboboxes:
    Code:
        Private Sub LoadComboBoxes()
            Try
                Using SQLcmd As New SqlCommand With {
                    .Connection = Vars.sqlConnACL,
                    .CommandText = SQL.TableSet 'SQL String
                    }
    
                    dsTables = New DataSet
                    Using adapter As New SqlDataAdapter With {.SelectCommand = SQLcmd}
                        If Not SQLcmd.Connection.State = ConnectionState.Open Then SQLcmd.Connection.Open()
                        adapter.Fill(dsTables)
                    End Using
                End Using
    
                dsTables.Tables(0).TableName = "tbl_Disease_Group"
                Dim dtDGroup As New DataTable
    
                'dtDGroup = dsTables.Tables("tbl_Disease_Group")
                'Original try to fill via DataSet
    
                'Tried to fill via EF so I could create a class - Got conversion errors
                Using dbACL As New ACL_Entities
                    dtDGroup = From items In dbACL.tbl_Disease_Group
                               Select items.ID_Disease_Group, items.Disease_Group_Name
                End Using
    
                With Me.cboxLastName
                    .DataSource = New cbox_Last_Name
                    '.Items.Add(String.Empty)
                    '.Sorted = True
                End With
    
                With Me.cboxFirstName
                    .DataSource = New cbox_First_Name
                End With
    
                With cboxDOB
                    .DataSource = New cbox_DOB
                End With
    
                With cboxDiseaseGroup
                    .DisplayMember = "Name_DGroup"
                    .ValueMember = "ID_DGroup"
                    .DataSource = dtDGroup
                End With
    
            Catch ex As Exception
                MessageBox.Show(Me, ex.Message, Err.Number, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    End Class

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,728

    Re: Entity Framework, Classes, and Comboboxes (How to Fill)

    If you have done mathematics at school then you know that you need to use parentheses when you want to apply an operation to a group instead of something in the group. This:
    Code:
                Last_Name = From items In dbACL.data_Demog
                            Select items.Last_Name.ToList()
    should be this:
    Code:
                Last_Name = (From items In dbACL.data_Demog
                            Select items.Last_Name).ToList()
    Also, good practice requires that you don't use singular names for variables that refer to lists. That should be Last_Names rather than Last_Name. Even better, it should be LastNames.

    EDIT: I just realised that, no only are you using a singular where a plural is appropriate, you're using a plural where a singular is appropriate. In a query you are dealing with one item at a time so items should be item.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2019
    Posts
    22

    Re: Entity Framework, Classes, and Comboboxes (How to Fill)

    Quote Originally Posted by jmcilhinney View Post
    If you have done mathematics at school then you know that you need to use parentheses when you want to apply an operation to a group instead of something in the group. This:
    Code:
                Last_Name = From items In dbACL.data_Demog
                            Select items.Last_Name.ToList()
    should be this:
    Code:
                Last_Name = (From items In dbACL.data_Demog
                            Select items.Last_Name).ToList()
    Also, good practice requires that you don't use singular names for variables that refer to lists. That should be Last_Names rather than Last_Name. Even better, it should be LastNames.

    EDIT: I just realised that, no only are you using a singular where a plural is appropriate, you're using a plural where a singular is appropriate. In a query you are dealing with one item at a time so items should be item.
    JM: Yes, you are correct - I do know that parentheses need to be applied to the group. I have actually tried it BOTH ways and still got the same errors. So it's not a matter of where the parentheses are, it's a conversion issue. I don't know what the classes should be set up as. I've tried DataTable, DataView, List, Array, IEnumerable... just to name a few. Again, this only seems to be an issue when there is more than one column I need returned.

    Also, and again yes, I know my singulars/plurals are all mixed up. All the presented code is from a test project and not the production project. All the variables are just a place-holders until I could get the code working - and THEN I would change to something meaningful and correct. And thank you for your tips on naming conventions. While I have read the "standards," I have been slow to adopt them - need to break the bad (coding) habits.

    I am trying to learn and apply the best/correct methods. It's not easy, but I am trying.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,728

    Re: Entity Framework, Classes, and Comboboxes (How to Fill)

    I didn't look at those error messages closely enough. Why is there any reference to a DataTable if you're using EF? Why do you have ADO.NET code in there? What I said stands but I have no idea what you're doing after that that would expect a DataTable.

    Those extra classes you've defined are useless and just add a layer of complexity. If you want to populate a ComboBox with the values from a specific property of a specific entity then just do that:
    vb.net Code:
    1. myComboBox.DataSource = myDbContext.SomeEntitySet.Select(Function(e) e.SomeProperty).ToArray()
    The query syntax version of that function syntax would be:
    vb.net Code:
    1. myComboBox.DataSource = (From e In myDbContext.SomeEntitySet
    2.                          Select e.SomeProperty).ToArray()

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2019
    Posts
    22

    Re: Entity Framework, Classes, and Comboboxes (How to Fill)

    Quote Originally Posted by jmcilhinney View Post
    I didn't look at those error messages closely enough. Why is there any reference to a DataTable if you're using EF? Why do you have ADO.NET code in there? What I said stands but I have no idea what you're doing after that that would expect a DataTable.
    JM: As I stated in my first post: I have little idea of what I am doing, but I am trying to learn. To give my reasoning; I have 16 tables that hold static values, these values are simply used to populate comboboxes throughout the application. I figured if I load them all into a DataSet at load time, then that should save time and resources by not having to query them each time I needed them. Especially when I have my main form that is filling 14 CB's (8 from tables, 6 Yes/No). And since I am new to using classes, I thought it appropriate to build classes for each of them. Apparently that was wrong.

    Quote Originally Posted by jmcilhinney View Post
    Those extra classes you've defined are useless and just add a layer of complexity. If you want to populate a ComboBox with the values from a specific property of a specific entity then just do that:
    vb.net Code:
    1. myComboBox.DataSource = myDbContext.SomeEntitySet.Select(Function(e) e.SomeProperty).ToArray()
    The query syntax version of that function syntax would be:
    vb.net Code:
    1. myComboBox.DataSource = (From e In myDbContext.SomeEntitySet
    2.                          Select e.SomeProperty).ToArray()
    THIS is working for me, now. Guess I didn't realize, or try, to query straight from DataSource. Now that I know that, I will use that instead of building classes. THANK YOU!

    I was also able to add a blank value to the single column CB's with the following code:
    Code:
    With Me.cboxLastName
    	Dim lst = dbACL.data_Demog.OrderBy(Function(a) a.Last_Name).Select(Function(b) b.Last_Name).ToList()
    	lst.Insert(0, "")
    	.DataSource = lst
    End With
    That works for ONE column, but I am unable to get the same to work when I am selecting TWO columns. This is what I have tried:
    Name:  TwoCol.jpg
Views: 25
Size:  14.9 KB

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2019
    Posts
    22

    Re: Entity Framework, Classes, and Comboboxes (How to Fill)

    After enough searching the web (I can't seem to find anything easily) I was able to accomplish everything I needed. That includes returning a DISTINCT list, sorting it, and adding a blank value to the beginning. I did have to create a class for the TWO column combobox, it just would not work without it.

    For anyone who is interested, or just in case anyone knows a better way, here is the code:

    Code:
    Using dbACL As New ACL_Entities
    
    	With Me.cboxLastName
    		Dim lst = (From e In dbACL.data_Demog
    			   Select e.Last_Name
    			  ).Distinct().OrderBy(Function(i) i).ToList
    		lst.Insert(0, "")
    		.DataSource = lst
    	End With
    
    	With Me.cboxFirstName
    		Dim lst = (From e In dbACL.data_Demog
    			   Select e.First_Name
    			  ).Distinct().OrderBy(Function(i) i).ToList
    		lst.Insert(0, "")
    		.DataSource = lst
    	End With
    
    	With cboxDOB
    		Dim lst = (From e In dbACL.data_Demog
    			   Select e.DOB
    			   ).Distinct().OrderByDescending(Function(i) i).ToList
    		Dim lst2 As New List(Of String)
    		For Each d As Date In lst
    			lst2.Add(d)
    		Next
    		lst2.Insert(0, "")
    		.DataSource = lst2
    	End With
    
    	With cboxDiseaseGroup
    		Dim lst = (From e In dbACL.tbl_Disease_Group
    			   Order By e.Disease_Group_Name
    			   Select New cbox_DGroup With {.ID_DGroup = e.ID_Disease_Group, .Name_DGroup = e.Disease_Group_Name}
    				).ToList()
    
    		Dim d = New cbox_DGroup With {.ID_DGroup = 0, .Name_DGroup = ""}
    		lst.Insert(0, d)
    
    		.DataSource = lst
    		.DisplayMember = "Name_DGroup"
    		.ValueMember = "ID_DGroup"
    	End With
    
    End Using
    Code:
    Public Class cbox_DGroup
        'Two Column
        Public Property Name_DGroup As String
        Public Property ID_DGroup As Integer
    End Class

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,400

    Re: Entity Framework, Classes, and Comboboxes (How to Fill)

    Suggestion:
    Code:
    		.DataSource = lst
    		.DisplayMember = "Name_DGroup"
    		.ValueMember = "ID_DGroup"
    Flip that around so that the datasource is set last. When you set the datasource, it will try to render the combobox right away, but you haven't given it the display or value members yet... with a combo box it's probably a non-issue, but it's still a good habit to get into so that when you run into other things like a list box, it WILL make a difference because it won't re-render when the display/value members are set.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2019
    Posts
    22

    Re: Entity Framework, Classes, and Comboboxes (How to Fill)

    Quote Originally Posted by techgnome View Post
    Suggestion:
    Code:
    		.DataSource = lst
    		.DisplayMember = "Name_DGroup"
    		.ValueMember = "ID_DGroup"
    Flip that around so that the datasource is set last. When you set the datasource, it will try to render the combobox right away, but you haven't given it the display or value members yet... with a combo box it's probably a non-issue, but it's still a good habit to get into so that when you run into other things like a list box, it WILL make a difference because it won't re-render when the display/value members are set.

    -tg
    techgnome: Thanks! I have seen several examples/posts where they stated that putting DataSource last may solve some issues. I have never had an issue, but you are correct - it's a good habit to get into.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,400

    Re: Entity Framework, Classes, and Comboboxes (How to Fill)

    I think it depends on the implementation of the .ToString method of the object. IF it's built right and returns something, most of the time, it's what we would expect, and so it ends up looking right. Until that first time you bind to something like a datatable and it encounters datarows... well... the .ToString implentation of a DataRow is to return the datatype as a string... because it otherwise can't possibly know what you want to return... so then suddenly your comboBox or listBox is filled with System.Data.DataRow .... over and over and over... and it's almost always because the datasource was set first.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width