Winforms Data from database across multiple forms2019 Community Moderator ElectionUsing a static DataSet as DataSourceUsing DataGridView to Update Multiple TablesWhat are the options for storing hierarchical data in a relational database?Loading all the tables into a strongly typed dataset, defined in an XSD?Using RFID in multiple windows formsUpdating records requires a valid InsertCommand when passed DataRow collection with new rowsC# Manually added dataset, how to retrieve data to text columnsChange timeout value for all TableAdapters on a formhow can i Update access database from vb.net appTrigger parent propertyChanging background color using combobox from a different form in Visual Basic

Possible Leak In Concrete

Replacing Windows 7 security updates with anti-virus?

Why did it take so long to abandon sail after steamships were demonstrated?

My story is written in English, but is set in my home country. What language should I use for the dialogue?

How to simplify this time periods definition interface?

What does it mean to make a bootable LiveUSB?

Co-worker team leader wants to inject his friend's awful software into our development. What should I say to our common boss?

Can hydraulic brake levers get hot when brakes overheat?

Is Mortgage interest accrued after a December payment tax deductible?

Is it normal that my co-workers at a fitness company criticize my food choices?

Is it true that real estate prices mainly go up?

Good allowance savings plan?

Employee lack of ownership

Can the damage from a Talisman of Pure Good (or Ultimate Evil) be non-lethal?

SQL Server Primary Login Restrictions

How do anti-virus programs start at Windows boot?

How do I hide Chekhov's Gun?

Why is stat::st_size 0 for devices but at the same time lseek defines the device size correctly?

Is a lawful good "antagonist" effective?

What is IP squat space

RegionDifference for Cylinder and Cuboid

Informing my boss about remarks from a nasty colleague

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

At what level can a dragon innately cast its spells?



Winforms Data from database across multiple forms



2019 Community Moderator ElectionUsing a static DataSet as DataSourceUsing DataGridView to Update Multiple TablesWhat are the options for storing hierarchical data in a relational database?Loading all the tables into a strongly typed dataset, defined in an XSD?Using RFID in multiple windows formsUpdating records requires a valid InsertCommand when passed DataRow collection with new rowsC# Manually added dataset, how to retrieve data to text columnsChange timeout value for all TableAdapters on a formhow can i Update access database from vb.net appTrigger parent propertyChanging background color using combobox from a different form in Visual Basic










1















I have a winforms applications that has an ms sql server backend. In my database i have lookup tables for like status, and other tables where the data rarely changes. In my application, several forms might use the same lookup tables (Some have a lot of data in them). Instead of loading/filling the data each time the form is open, is there a way to cache the data from the database that can be accessed from multiple forms. I did some searching, but couldnt find the best solution. There is caching, dictionaries, etc. What is the best solution and can you point me to the documentation that discusses it and may even have an example.



Edit:
In my original post I failed to mention that I have a strongly typed dataset and use tableadapters. I want to preload my lookup tables when my application starts, and then have these dataset tables be used throughout the application, on multiple forms without having to fill them on every form.



I have tried creating a class:



Public Class dsglobal

Public Shared EML_StaffingDataSet As EML_StaffingDataSet

Public Shared Sub populateDS()
EML_StaffingDataSet = New EML_StaffingDataSet
End Sub

Public Shared Sub loadskills()
Dim ta As New EML_StaffingDataSetTableAdapters.TSTAFFSKILLTableAdapter
ta.Fill(EML_StaffingDataSet.TSTAFFSKILL)
End Sub

End Class


I run this on a background worker when my application is starting up. So it loads the dataset table. On fill, I can see the datatable has data in it. When I open a form, i want to use the dataset table, but it seems to clear the data out. Not sure if my approach is correct or where my error is.



Edit2:
I have also tried this per comments, but not sure I am doing it correctly. If I am doing it correctly, then how do I use that as a datasource at design time, can i only do that programmatically?



Public Module lookupdata
Private EML_StaffingDataSet As EML_StaffingDataSet

Private skillvalues As List(Of skill)

Public ReadOnly Property skill As List(Of skill)
Get
If skillvalues Is Nothing Then
getskillvalues()
End If
Return skillvalues
End Get
End Property

Private Sub getskillvalues()
skillvalues = New List(Of skill)
EML_StaffingDataSet = New EML_StaffingDataSet
Dim ta As New EML_StaffingDataSetTableAdapters.TSTAFFSKILLTableAdapter
ta.Fill(EML_StaffingDataSet.TSTAFFSKILL)

For Each row As DataRow In EML_StaffingDataSet.TSTAFFSKILL
Dim skill As New skill
skill.skill_id = row("skill_id")
skill.skill_desc = row("skill_desc")
skill.skill_open_ind = row("skill_open_ind")
skillvalues.Add(skill)
Next
End Sub

End Module

Public Class skill
Public Property skill_id As Integer
Public Property skill_desc As String
Public Property skill_open_ind As Boolean
End Class









share|improve this question



















  • 1





    As simple as it could be, you can create a separate project where you have a static class with some property representing the cached data. When you use the property you check if the underlying property static field has been initialized. If not initialize it (loading data from the db), if yes just return the static field.

    – Steve
    Mar 6 at 18:48











  • I think what I am looking for is how to do a global dataset.

    – dk96m
    2 days ago











  • how do I use that as a datasource at design time do you mean specifically as a winforms control datasource?

    – djv
    2 days ago






  • 1





    In conjunction with the answer below, I think you have it. Add your control, click the properties arrow, Choose Data Source, Add Project Data Source, Object, Next, Expand your project containing the module down to the property "skill", Check "skill", Finish. You will see the designer has generated SkillBindingSource at the bottom. And your control should be populated with properties of class skill.

    – djv
    2 days ago






  • 1





    I would add that the public property of your module which has your data should probably be of type IEnumerable(Of Skill), rather than a List(Of Skill). The difference is a List exposes methods such as Add, Clear, Remove, etc. which you probably don't want to expose to the consumer, which IEnumerable is designed for the consumer to enumerate / read results only.

    – djv
    2 days ago
















1















I have a winforms applications that has an ms sql server backend. In my database i have lookup tables for like status, and other tables where the data rarely changes. In my application, several forms might use the same lookup tables (Some have a lot of data in them). Instead of loading/filling the data each time the form is open, is there a way to cache the data from the database that can be accessed from multiple forms. I did some searching, but couldnt find the best solution. There is caching, dictionaries, etc. What is the best solution and can you point me to the documentation that discusses it and may even have an example.



Edit:
In my original post I failed to mention that I have a strongly typed dataset and use tableadapters. I want to preload my lookup tables when my application starts, and then have these dataset tables be used throughout the application, on multiple forms without having to fill them on every form.



I have tried creating a class:



Public Class dsglobal

Public Shared EML_StaffingDataSet As EML_StaffingDataSet

Public Shared Sub populateDS()
EML_StaffingDataSet = New EML_StaffingDataSet
End Sub

Public Shared Sub loadskills()
Dim ta As New EML_StaffingDataSetTableAdapters.TSTAFFSKILLTableAdapter
ta.Fill(EML_StaffingDataSet.TSTAFFSKILL)
End Sub

End Class


I run this on a background worker when my application is starting up. So it loads the dataset table. On fill, I can see the datatable has data in it. When I open a form, i want to use the dataset table, but it seems to clear the data out. Not sure if my approach is correct or where my error is.



Edit2:
I have also tried this per comments, but not sure I am doing it correctly. If I am doing it correctly, then how do I use that as a datasource at design time, can i only do that programmatically?



Public Module lookupdata
Private EML_StaffingDataSet As EML_StaffingDataSet

Private skillvalues As List(Of skill)

Public ReadOnly Property skill As List(Of skill)
Get
If skillvalues Is Nothing Then
getskillvalues()
End If
Return skillvalues
End Get
End Property

Private Sub getskillvalues()
skillvalues = New List(Of skill)
EML_StaffingDataSet = New EML_StaffingDataSet
Dim ta As New EML_StaffingDataSetTableAdapters.TSTAFFSKILLTableAdapter
ta.Fill(EML_StaffingDataSet.TSTAFFSKILL)

For Each row As DataRow In EML_StaffingDataSet.TSTAFFSKILL
Dim skill As New skill
skill.skill_id = row("skill_id")
skill.skill_desc = row("skill_desc")
skill.skill_open_ind = row("skill_open_ind")
skillvalues.Add(skill)
Next
End Sub

End Module

Public Class skill
Public Property skill_id As Integer
Public Property skill_desc As String
Public Property skill_open_ind As Boolean
End Class









share|improve this question



















  • 1





    As simple as it could be, you can create a separate project where you have a static class with some property representing the cached data. When you use the property you check if the underlying property static field has been initialized. If not initialize it (loading data from the db), if yes just return the static field.

    – Steve
    Mar 6 at 18:48











  • I think what I am looking for is how to do a global dataset.

    – dk96m
    2 days ago











  • how do I use that as a datasource at design time do you mean specifically as a winforms control datasource?

    – djv
    2 days ago






  • 1





    In conjunction with the answer below, I think you have it. Add your control, click the properties arrow, Choose Data Source, Add Project Data Source, Object, Next, Expand your project containing the module down to the property "skill", Check "skill", Finish. You will see the designer has generated SkillBindingSource at the bottom. And your control should be populated with properties of class skill.

    – djv
    2 days ago






  • 1





    I would add that the public property of your module which has your data should probably be of type IEnumerable(Of Skill), rather than a List(Of Skill). The difference is a List exposes methods such as Add, Clear, Remove, etc. which you probably don't want to expose to the consumer, which IEnumerable is designed for the consumer to enumerate / read results only.

    – djv
    2 days ago














1












1








1








I have a winforms applications that has an ms sql server backend. In my database i have lookup tables for like status, and other tables where the data rarely changes. In my application, several forms might use the same lookup tables (Some have a lot of data in them). Instead of loading/filling the data each time the form is open, is there a way to cache the data from the database that can be accessed from multiple forms. I did some searching, but couldnt find the best solution. There is caching, dictionaries, etc. What is the best solution and can you point me to the documentation that discusses it and may even have an example.



Edit:
In my original post I failed to mention that I have a strongly typed dataset and use tableadapters. I want to preload my lookup tables when my application starts, and then have these dataset tables be used throughout the application, on multiple forms without having to fill them on every form.



I have tried creating a class:



Public Class dsglobal

Public Shared EML_StaffingDataSet As EML_StaffingDataSet

Public Shared Sub populateDS()
EML_StaffingDataSet = New EML_StaffingDataSet
End Sub

Public Shared Sub loadskills()
Dim ta As New EML_StaffingDataSetTableAdapters.TSTAFFSKILLTableAdapter
ta.Fill(EML_StaffingDataSet.TSTAFFSKILL)
End Sub

End Class


I run this on a background worker when my application is starting up. So it loads the dataset table. On fill, I can see the datatable has data in it. When I open a form, i want to use the dataset table, but it seems to clear the data out. Not sure if my approach is correct or where my error is.



Edit2:
I have also tried this per comments, but not sure I am doing it correctly. If I am doing it correctly, then how do I use that as a datasource at design time, can i only do that programmatically?



Public Module lookupdata
Private EML_StaffingDataSet As EML_StaffingDataSet

Private skillvalues As List(Of skill)

Public ReadOnly Property skill As List(Of skill)
Get
If skillvalues Is Nothing Then
getskillvalues()
End If
Return skillvalues
End Get
End Property

Private Sub getskillvalues()
skillvalues = New List(Of skill)
EML_StaffingDataSet = New EML_StaffingDataSet
Dim ta As New EML_StaffingDataSetTableAdapters.TSTAFFSKILLTableAdapter
ta.Fill(EML_StaffingDataSet.TSTAFFSKILL)

For Each row As DataRow In EML_StaffingDataSet.TSTAFFSKILL
Dim skill As New skill
skill.skill_id = row("skill_id")
skill.skill_desc = row("skill_desc")
skill.skill_open_ind = row("skill_open_ind")
skillvalues.Add(skill)
Next
End Sub

End Module

Public Class skill
Public Property skill_id As Integer
Public Property skill_desc As String
Public Property skill_open_ind As Boolean
End Class









share|improve this question
















I have a winforms applications that has an ms sql server backend. In my database i have lookup tables for like status, and other tables where the data rarely changes. In my application, several forms might use the same lookup tables (Some have a lot of data in them). Instead of loading/filling the data each time the form is open, is there a way to cache the data from the database that can be accessed from multiple forms. I did some searching, but couldnt find the best solution. There is caching, dictionaries, etc. What is the best solution and can you point me to the documentation that discusses it and may even have an example.



Edit:
In my original post I failed to mention that I have a strongly typed dataset and use tableadapters. I want to preload my lookup tables when my application starts, and then have these dataset tables be used throughout the application, on multiple forms without having to fill them on every form.



I have tried creating a class:



Public Class dsglobal

Public Shared EML_StaffingDataSet As EML_StaffingDataSet

Public Shared Sub populateDS()
EML_StaffingDataSet = New EML_StaffingDataSet
End Sub

Public Shared Sub loadskills()
Dim ta As New EML_StaffingDataSetTableAdapters.TSTAFFSKILLTableAdapter
ta.Fill(EML_StaffingDataSet.TSTAFFSKILL)
End Sub

End Class


I run this on a background worker when my application is starting up. So it loads the dataset table. On fill, I can see the datatable has data in it. When I open a form, i want to use the dataset table, but it seems to clear the data out. Not sure if my approach is correct or where my error is.



Edit2:
I have also tried this per comments, but not sure I am doing it correctly. If I am doing it correctly, then how do I use that as a datasource at design time, can i only do that programmatically?



Public Module lookupdata
Private EML_StaffingDataSet As EML_StaffingDataSet

Private skillvalues As List(Of skill)

Public ReadOnly Property skill As List(Of skill)
Get
If skillvalues Is Nothing Then
getskillvalues()
End If
Return skillvalues
End Get
End Property

Private Sub getskillvalues()
skillvalues = New List(Of skill)
EML_StaffingDataSet = New EML_StaffingDataSet
Dim ta As New EML_StaffingDataSetTableAdapters.TSTAFFSKILLTableAdapter
ta.Fill(EML_StaffingDataSet.TSTAFFSKILL)

For Each row As DataRow In EML_StaffingDataSet.TSTAFFSKILL
Dim skill As New skill
skill.skill_id = row("skill_id")
skill.skill_desc = row("skill_desc")
skill.skill_open_ind = row("skill_open_ind")
skillvalues.Add(skill)
Next
End Sub

End Module

Public Class skill
Public Property skill_id As Integer
Public Property skill_desc As String
Public Property skill_open_ind As Boolean
End Class






database vb.net winforms tableadapter strongly-typed-dataset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago







dk96m

















asked Mar 6 at 18:41









dk96mdk96m

558




558







  • 1





    As simple as it could be, you can create a separate project where you have a static class with some property representing the cached data. When you use the property you check if the underlying property static field has been initialized. If not initialize it (loading data from the db), if yes just return the static field.

    – Steve
    Mar 6 at 18:48











  • I think what I am looking for is how to do a global dataset.

    – dk96m
    2 days ago











  • how do I use that as a datasource at design time do you mean specifically as a winforms control datasource?

    – djv
    2 days ago






  • 1





    In conjunction with the answer below, I think you have it. Add your control, click the properties arrow, Choose Data Source, Add Project Data Source, Object, Next, Expand your project containing the module down to the property "skill", Check "skill", Finish. You will see the designer has generated SkillBindingSource at the bottom. And your control should be populated with properties of class skill.

    – djv
    2 days ago






  • 1





    I would add that the public property of your module which has your data should probably be of type IEnumerable(Of Skill), rather than a List(Of Skill). The difference is a List exposes methods such as Add, Clear, Remove, etc. which you probably don't want to expose to the consumer, which IEnumerable is designed for the consumer to enumerate / read results only.

    – djv
    2 days ago













  • 1





    As simple as it could be, you can create a separate project where you have a static class with some property representing the cached data. When you use the property you check if the underlying property static field has been initialized. If not initialize it (loading data from the db), if yes just return the static field.

    – Steve
    Mar 6 at 18:48











  • I think what I am looking for is how to do a global dataset.

    – dk96m
    2 days ago











  • how do I use that as a datasource at design time do you mean specifically as a winforms control datasource?

    – djv
    2 days ago






  • 1





    In conjunction with the answer below, I think you have it. Add your control, click the properties arrow, Choose Data Source, Add Project Data Source, Object, Next, Expand your project containing the module down to the property "skill", Check "skill", Finish. You will see the designer has generated SkillBindingSource at the bottom. And your control should be populated with properties of class skill.

    – djv
    2 days ago






  • 1





    I would add that the public property of your module which has your data should probably be of type IEnumerable(Of Skill), rather than a List(Of Skill). The difference is a List exposes methods such as Add, Clear, Remove, etc. which you probably don't want to expose to the consumer, which IEnumerable is designed for the consumer to enumerate / read results only.

    – djv
    2 days ago








1




1





As simple as it could be, you can create a separate project where you have a static class with some property representing the cached data. When you use the property you check if the underlying property static field has been initialized. If not initialize it (loading data from the db), if yes just return the static field.

– Steve
Mar 6 at 18:48





As simple as it could be, you can create a separate project where you have a static class with some property representing the cached data. When you use the property you check if the underlying property static field has been initialized. If not initialize it (loading data from the db), if yes just return the static field.

– Steve
Mar 6 at 18:48













I think what I am looking for is how to do a global dataset.

– dk96m
2 days ago





I think what I am looking for is how to do a global dataset.

– dk96m
2 days ago













how do I use that as a datasource at design time do you mean specifically as a winforms control datasource?

– djv
2 days ago





how do I use that as a datasource at design time do you mean specifically as a winforms control datasource?

– djv
2 days ago




1




1





In conjunction with the answer below, I think you have it. Add your control, click the properties arrow, Choose Data Source, Add Project Data Source, Object, Next, Expand your project containing the module down to the property "skill", Check "skill", Finish. You will see the designer has generated SkillBindingSource at the bottom. And your control should be populated with properties of class skill.

– djv
2 days ago





In conjunction with the answer below, I think you have it. Add your control, click the properties arrow, Choose Data Source, Add Project Data Source, Object, Next, Expand your project containing the module down to the property "skill", Check "skill", Finish. You will see the designer has generated SkillBindingSource at the bottom. And your control should be populated with properties of class skill.

– djv
2 days ago




1




1





I would add that the public property of your module which has your data should probably be of type IEnumerable(Of Skill), rather than a List(Of Skill). The difference is a List exposes methods such as Add, Clear, Remove, etc. which you probably don't want to expose to the consumer, which IEnumerable is designed for the consumer to enumerate / read results only.

– djv
2 days ago






I would add that the public property of your module which has your data should probably be of type IEnumerable(Of Skill), rather than a List(Of Skill). The difference is a List exposes methods such as Add, Clear, Remove, etc. which you probably don't want to expose to the consumer, which IEnumerable is designed for the consumer to enumerate / read results only.

– djv
2 days ago













1 Answer
1






active

oldest

votes


















1














You might want to consider a lazy loading pattern, like this:



Public Module LookupData
Private statusValues As List(Of LookupValue)

Public Readonly Property Statuses As List(Of LookupValue)
Get
If statusValues Is Nothing Then
GetStatusValues()
End If

Return statusValues
End Get
End Property

Private Sub GetStatusValues()
statusValues = New List(Of LookupValue)

Dim sql = "select key, value from StatusTable"

'TODO: Read the items from the database here, adding them to the list.

End Sub

End Module

Public Class LookupValue
Public Property Key As String
Public Property Value As String
End Class


The idea is that you've got a single instance of LookupData (a Module in VB, there can be only one). Lookup data has a series of Properties, each of which returns a list of values from the database. If the data has already been loaded, it just returns what it has cached. If the data has not been loaded, then the first time it is referenced it retrieves it from the database.



You can consume it elsewhere in your code as follows:



Dim myStatuses = LookupData.Statuses





share|improve this answer























  • but what if the database table has more than a key field and value field. It could be made up of 2+ columns

    – dk96m
    Mar 6 at 20:02






  • 1





    You can add as many properties to the LookupValue class as you like, and you can also have multiple classes for different record structures.

    – RogerMKE
    Mar 6 at 20:03






  • 1





    Instead of returning List(Of LookupValue), create a class which encapsulates all your database state and return that. This is similar to a static factory method.

    – djv
    Mar 6 at 20:05











  • thank you for the info. Where can I read more about it?

    – dk96m
    Mar 6 at 20:08











  • say i am using datasets and tableadapters throughout my application. right now I fill each like "Me.TSTAFFSKILLTableAdapter.Fill(Me.EML_StaffingDataSet.TSTAFFSKILL)" what is the best option when i am loading data this way?

    – dk96m
    Mar 7 at 20:10











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55030097%2fwinforms-data-from-database-across-multiple-forms%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You might want to consider a lazy loading pattern, like this:



Public Module LookupData
Private statusValues As List(Of LookupValue)

Public Readonly Property Statuses As List(Of LookupValue)
Get
If statusValues Is Nothing Then
GetStatusValues()
End If

Return statusValues
End Get
End Property

Private Sub GetStatusValues()
statusValues = New List(Of LookupValue)

Dim sql = "select key, value from StatusTable"

'TODO: Read the items from the database here, adding them to the list.

End Sub

End Module

Public Class LookupValue
Public Property Key As String
Public Property Value As String
End Class


The idea is that you've got a single instance of LookupData (a Module in VB, there can be only one). Lookup data has a series of Properties, each of which returns a list of values from the database. If the data has already been loaded, it just returns what it has cached. If the data has not been loaded, then the first time it is referenced it retrieves it from the database.



You can consume it elsewhere in your code as follows:



Dim myStatuses = LookupData.Statuses





share|improve this answer























  • but what if the database table has more than a key field and value field. It could be made up of 2+ columns

    – dk96m
    Mar 6 at 20:02






  • 1





    You can add as many properties to the LookupValue class as you like, and you can also have multiple classes for different record structures.

    – RogerMKE
    Mar 6 at 20:03






  • 1





    Instead of returning List(Of LookupValue), create a class which encapsulates all your database state and return that. This is similar to a static factory method.

    – djv
    Mar 6 at 20:05











  • thank you for the info. Where can I read more about it?

    – dk96m
    Mar 6 at 20:08











  • say i am using datasets and tableadapters throughout my application. right now I fill each like "Me.TSTAFFSKILLTableAdapter.Fill(Me.EML_StaffingDataSet.TSTAFFSKILL)" what is the best option when i am loading data this way?

    – dk96m
    Mar 7 at 20:10
















1














You might want to consider a lazy loading pattern, like this:



Public Module LookupData
Private statusValues As List(Of LookupValue)

Public Readonly Property Statuses As List(Of LookupValue)
Get
If statusValues Is Nothing Then
GetStatusValues()
End If

Return statusValues
End Get
End Property

Private Sub GetStatusValues()
statusValues = New List(Of LookupValue)

Dim sql = "select key, value from StatusTable"

'TODO: Read the items from the database here, adding them to the list.

End Sub

End Module

Public Class LookupValue
Public Property Key As String
Public Property Value As String
End Class


The idea is that you've got a single instance of LookupData (a Module in VB, there can be only one). Lookup data has a series of Properties, each of which returns a list of values from the database. If the data has already been loaded, it just returns what it has cached. If the data has not been loaded, then the first time it is referenced it retrieves it from the database.



You can consume it elsewhere in your code as follows:



Dim myStatuses = LookupData.Statuses





share|improve this answer























  • but what if the database table has more than a key field and value field. It could be made up of 2+ columns

    – dk96m
    Mar 6 at 20:02






  • 1





    You can add as many properties to the LookupValue class as you like, and you can also have multiple classes for different record structures.

    – RogerMKE
    Mar 6 at 20:03






  • 1





    Instead of returning List(Of LookupValue), create a class which encapsulates all your database state and return that. This is similar to a static factory method.

    – djv
    Mar 6 at 20:05











  • thank you for the info. Where can I read more about it?

    – dk96m
    Mar 6 at 20:08











  • say i am using datasets and tableadapters throughout my application. right now I fill each like "Me.TSTAFFSKILLTableAdapter.Fill(Me.EML_StaffingDataSet.TSTAFFSKILL)" what is the best option when i am loading data this way?

    – dk96m
    Mar 7 at 20:10














1












1








1







You might want to consider a lazy loading pattern, like this:



Public Module LookupData
Private statusValues As List(Of LookupValue)

Public Readonly Property Statuses As List(Of LookupValue)
Get
If statusValues Is Nothing Then
GetStatusValues()
End If

Return statusValues
End Get
End Property

Private Sub GetStatusValues()
statusValues = New List(Of LookupValue)

Dim sql = "select key, value from StatusTable"

'TODO: Read the items from the database here, adding them to the list.

End Sub

End Module

Public Class LookupValue
Public Property Key As String
Public Property Value As String
End Class


The idea is that you've got a single instance of LookupData (a Module in VB, there can be only one). Lookup data has a series of Properties, each of which returns a list of values from the database. If the data has already been loaded, it just returns what it has cached. If the data has not been loaded, then the first time it is referenced it retrieves it from the database.



You can consume it elsewhere in your code as follows:



Dim myStatuses = LookupData.Statuses





share|improve this answer













You might want to consider a lazy loading pattern, like this:



Public Module LookupData
Private statusValues As List(Of LookupValue)

Public Readonly Property Statuses As List(Of LookupValue)
Get
If statusValues Is Nothing Then
GetStatusValues()
End If

Return statusValues
End Get
End Property

Private Sub GetStatusValues()
statusValues = New List(Of LookupValue)

Dim sql = "select key, value from StatusTable"

'TODO: Read the items from the database here, adding them to the list.

End Sub

End Module

Public Class LookupValue
Public Property Key As String
Public Property Value As String
End Class


The idea is that you've got a single instance of LookupData (a Module in VB, there can be only one). Lookup data has a series of Properties, each of which returns a list of values from the database. If the data has already been loaded, it just returns what it has cached. If the data has not been loaded, then the first time it is referenced it retrieves it from the database.



You can consume it elsewhere in your code as follows:



Dim myStatuses = LookupData.Statuses






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 6 at 19:53









RogerMKERogerMKE

1078




1078












  • but what if the database table has more than a key field and value field. It could be made up of 2+ columns

    – dk96m
    Mar 6 at 20:02






  • 1





    You can add as many properties to the LookupValue class as you like, and you can also have multiple classes for different record structures.

    – RogerMKE
    Mar 6 at 20:03






  • 1





    Instead of returning List(Of LookupValue), create a class which encapsulates all your database state and return that. This is similar to a static factory method.

    – djv
    Mar 6 at 20:05











  • thank you for the info. Where can I read more about it?

    – dk96m
    Mar 6 at 20:08











  • say i am using datasets and tableadapters throughout my application. right now I fill each like "Me.TSTAFFSKILLTableAdapter.Fill(Me.EML_StaffingDataSet.TSTAFFSKILL)" what is the best option when i am loading data this way?

    – dk96m
    Mar 7 at 20:10


















  • but what if the database table has more than a key field and value field. It could be made up of 2+ columns

    – dk96m
    Mar 6 at 20:02






  • 1





    You can add as many properties to the LookupValue class as you like, and you can also have multiple classes for different record structures.

    – RogerMKE
    Mar 6 at 20:03






  • 1





    Instead of returning List(Of LookupValue), create a class which encapsulates all your database state and return that. This is similar to a static factory method.

    – djv
    Mar 6 at 20:05











  • thank you for the info. Where can I read more about it?

    – dk96m
    Mar 6 at 20:08











  • say i am using datasets and tableadapters throughout my application. right now I fill each like "Me.TSTAFFSKILLTableAdapter.Fill(Me.EML_StaffingDataSet.TSTAFFSKILL)" what is the best option when i am loading data this way?

    – dk96m
    Mar 7 at 20:10

















but what if the database table has more than a key field and value field. It could be made up of 2+ columns

– dk96m
Mar 6 at 20:02





but what if the database table has more than a key field and value field. It could be made up of 2+ columns

– dk96m
Mar 6 at 20:02




1




1





You can add as many properties to the LookupValue class as you like, and you can also have multiple classes for different record structures.

– RogerMKE
Mar 6 at 20:03





You can add as many properties to the LookupValue class as you like, and you can also have multiple classes for different record structures.

– RogerMKE
Mar 6 at 20:03




1




1





Instead of returning List(Of LookupValue), create a class which encapsulates all your database state and return that. This is similar to a static factory method.

– djv
Mar 6 at 20:05





Instead of returning List(Of LookupValue), create a class which encapsulates all your database state and return that. This is similar to a static factory method.

– djv
Mar 6 at 20:05













thank you for the info. Where can I read more about it?

– dk96m
Mar 6 at 20:08





thank you for the info. Where can I read more about it?

– dk96m
Mar 6 at 20:08













say i am using datasets and tableadapters throughout my application. right now I fill each like "Me.TSTAFFSKILLTableAdapter.Fill(Me.EML_StaffingDataSet.TSTAFFSKILL)" what is the best option when i am loading data this way?

– dk96m
Mar 7 at 20:10






say i am using datasets and tableadapters throughout my application. right now I fill each like "Me.TSTAFFSKILLTableAdapter.Fill(Me.EML_StaffingDataSet.TSTAFFSKILL)" what is the best option when i am loading data this way?

– dk96m
Mar 7 at 20:10




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55030097%2fwinforms-data-from-database-across-multiple-forms%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

Алба-Юлія

Захаров Федір Захарович