Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have declare one GLobal class for Dictionary

C#
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

/// <summary>
/// Summary description for GeneralClass
/// </summary>
public class Global
{

    public static Dictionary<string, object> Dictionary_common_setting = new Dictionary<string, object>();
    public Global()
    {

        //
        // TODO: Add constructor logic here
        //
    }
}

Another class to store value of dictionary

C#
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for common_setting
/// </summary>
public class dict_common_setting
{
   public string Name, Value ;
    public dict_common_setting()
    {

    }
}


Storing value in dictionary
C#
public bool UpdCommonSetting()
    {
        try
        {

            mycon.Open();

            MySqlCommand com = new MySqlCommand("select name,value from common_setting", mycon);
            dr = com.ExecuteReader();

            while (dr.Read())
            {                             

                string name = dr.GetString(0);
                string value = dr.GetString(1);

                dict_common_setting commsett = new dict_common_setting();
                commsett.Value  = value;
                Global.Dictionary_common_setting.Add(name, commsett);
            }
            dr.Close();

            mycon.Close();
        }
        catch (Exception ex)
        {
            mycon.Close();
        }
        return true;
    }



Here is the problem
C#
if (!IsPostBack)
{
    object ObjGetImgPath;

    function_class func = new function_class();
    func.UpdCommonSetting();
    if (Global.Dictionary_common_setting.ContainsKey("imgpath"))
    {
        ObjGetImgPath = Global.Dictionary_common_setting["imgpath"];

    }
    ObjGetImgPath. //I get the key and value in Object ObjGetImPath but unable to access it from that object.
   //How to get in string ImgPath = ObjGetImgPath.valuee;
    if (Session["id"] == null)
    {
        Response.Redirect("~/AdminPanel/frmLogout.aspx");
    }
}
Posted
Updated 20-Jan-15 17:42pm
v2
Comments
PIEBALDconsult 20-Jan-15 23:54pm    
Well, there's a lot of strange code there, but you don't seem to store the name in commsett, and why have a dictionary of objects rather than dict_common_settings?
Sergey Alexandrovich Kryukov 21-Jan-15 1:48am    
Using typeless collection (meaning: value type is System.Object, which means any type at all) is a really big abuse.
You named your class "Global", but it doesn't mean there are "global classes"; they are just not needed; there is no such concept in .NET. Your biggest problem is hard-coding keys (or any other immediate constants, like "imgpath").
—SA
BillWoodruff 21-Jan-15 5:42am    
I'd like to suggest you focus back on the "big-picture" here and try and state clearly what it is you want to implement.

You have a web-app/web-site; you want to store settings in a DataBase. So, this is all happening in code on the Server ?

1 solution

I suspect you need to cast the object to the proper type, but a better solution is to change the dictionary.

If you can't do that, then try

C#
object dict_common_setting ObjGetImgPath;
... 
        ObjGetImgPath = (dict_common_setting) Global.Dictionary_common_setting["imgpath"];
or
        ObjGetImgPath = Global.Dictionary_common_setting["imgpath"] as dict_common_setting;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900