Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7803165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:29:10+00:00 2026-06-02T01:29:10+00:00

I would like to have a dictionary object contains string keys and values, which

  • 0

I would like to have a dictionary object contains string keys and values, which are generated from the database. But, I only need to run once when page loads. (C# .NET)

In which way can I do that? I tried the following:

public static class GlobalVar
{
    static Dictionary<string, string> GenerateLoginStatus(Dictionary<string, string> List)
    {
        string Query = "SELECT * FROM LoginStatus";
        DataTable Types = MyAdoHelper.ExecuteDataTable(GlobalVar.dbName, Query);
        foreach (DataRow Row in Types.Rows)
        {
            List.Add(Row["Status_Title"].ToString(), Row["Status_Info"].ToString());
        }
        return List;
    }

    public const string GlobalString = "ProjectDatabase.mdf";

    public static Dictionary<string, string> LoginTypes = GenerateLoginStatus(LoginTypes);

}

Table structure:

Status_Title        Status_Info

AlreadyLoggedIn     User is already logged in.

A                   B

C                   D

And I use it in another page: GlobalVar.LoginStatus["AlreadyLoggedIn"]

The “AlreadyLoggedIn” is created in the database for sure, but when I call any of the keys, it returns an exception (doesn’t tell which exception).

EDIT: I changed the code a bit, and it now gives “The type initializer for ‘GlobalVar’ threw an exception.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:29:12+00:00Added an answer on June 2, 2026 at 1:29 am

    I suppose you might be getting null reference exception which is caused because you call your static initialization method with instance you want to initialize (LoginTypes):

    public static Dictionary<string, string> LoginTypes =
        GenerateLoginStatus(LoginTypes);
    

    When GenerateLoginStatus executes, LoginTypes is null. Yet you attempt to add items to it:

    static Dictionary<string, string> GenerateLoginStatus(
        Dictionary<string, string> List)
    {
        // ...
        List.Add(Row["Status_Title"].ToString(), Row["Status_Info"].ToString());
    

    This causes null reference exception. You should use type constructor and create that dictionary there:

    static GlobalVar()
    {
        LoginTypes = new Dictionary<string, string>();
        // proceed with initialization
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary object which i would like to encrypt, then put it
I have a NSMutableArray which contains NSMutableDictionaries .I would like to display one string
I would like to have an explicit cast between from a string to an
I have a Dictionary object where the Keys are of type string and the
I have created a dictionary in Class A and would like to return the
I have a generic dictonary which is templated in the following manner: Dictionary<object, IList<ISomeInterface>>
I have a dictionary that I would like to iterate through and copy the
I have a collection of stuffs in a generic Dictionary<string, object>() . The key
Basically I would like to have some dictionary that is an abstaction over legacy
I would like to find a way to have a Dictionary of various Mock

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.