Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All i want how to Create GlobalVariable and save my database name in this variable for use reference anywhere in page.
My code are as below in this code every thing is perfect but not define GlobalVariable if you people know pls help me it is very urgent
C#
protected void Button3_Click(object sender, EventArgs e)
    {
        string str = string.Empty;
        DataSet ds = new DataSet();
        string db = "";

        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected)
            {
                if (CheckBoxList1.SelectedIndex != 0)
                {
                    if (CheckBoxList1.SelectedItem.Value == "arjun")
                    {
                        db = "arjun";

                    }
                    if (CheckBoxList1.SelectedItem.Value == "bsales")
                    {
                        db = "bsales";
                    }
                    if (CheckBoxList1.SelectedItem.Value == "hr")
                    {
                        db = "hr";
                    }
                    if (CheckBoxList1.SelectedItem.Value == "information_schema")
                    {
                        db = "information_schema";
                    }
                    if (CheckBoxList1.SelectedItem.Value == "intall")
                    {
                        db = "intall";
                    }
                    if (CheckBoxList1.SelectedItem.Value == "messages")
                    {
                        db = "messages";
                    }
                    if(CheckBoxList1.SelectedItem.Value=="mysql")
                    {
                        db = "mysql";
                    }
                    if (CheckBoxList1.SelectedItem.Value == "sales")
                    {
                        db = "sales";
                    }
                
                }
                string abc = "Data Source=localhost;Initial Catalog=" + db + ";user id=root;password=root";
               
                MySqlConnection myconn = new MySqlConnection(abc);
                myconn.Open();
                str = li.ToString();
                string query = "show tables from " + str + "";
                MySqlDataAdapter mydata = new MySqlDataAdapter(query, myconn);             
                mydata.Fill(ds, str);              
                myconn.Close();
                
                
            }

            int k = 0;
            foreach (DataTable dt in ds.Tables)
            {
                if (k == 0)
                {
                    CheckBoxList2.DataSource = ds.Tables[0];
                    CheckBoxList2.DataTextField = "tables_in_" + ds.Tables[0].TableName + "";
                    CheckBoxList2.DataValueField = "tables_in_" + ds.Tables[0].TableName + "";
                    CheckBoxList2.DataBind();
                }
                if (k == 1)
                {
                    CheckBoxList3.DataSource = ds.Tables[1];
                    CheckBoxList3.DataTextField = "tables_in_" + ds.Tables[1].TableName + "";
                    CheckBoxList3.DataValueField = "tables_in_" + ds.Tables[1].TableName + "";
                    CheckBoxList3.DataBind();
                }
                k++;
            }
        }   
    }
Posted
Updated 23-Jan-14 0:59am
v2
Comments
BillWoodruff 23-Jan-14 11:30am    
Your code puzzles me: If there can only be one string stored in the 'db variable, why are you even using a CheckedListBox which ... even if SelectionMode is set to 'One ... allows multiple CheckBoxes to be checked ?

If SelectionMode is 'One, then SelectedIndex will always have a value of one of the CheckedListBox Items, unless there's no selection, in which case its value is -1.

Why are you iterating over CheckedListBox1, and testing every Item, when to get the SelectedIndex you only need to write: CheckBoxList1.SelectedIndex ?

try thiss. :)

HTML
If you create a variable inside the event of the session_start() method, it will only be available inside that method.

If you store a value in application or session state, those are accessible throughout your whole application. For example:


C#
string myAppTitle = (string)Application["appTitle"]'


reference.. :)

Application Variables[^]
 
Share this answer
 
C#
public partial class ClassName : System.Web.UI.Page
{
  string str="";//global  You can assign a value to this variable anywhere on the page

  //other Events.....
}
 
Share this answer
 
Comments
Arjunwalmiki 23-Jan-14 7:06am    
Salman can you seen my code how can i do batter way please let me know
Salman622 23-Jan-14 7:10am    
do you want to retrive that value on the same page or some other page
Salman622 23-Jan-14 7:14am    
below karthik has given a nice solution
to use class
Arjunwalmiki 24-Jan-14 0:51am    
yes salman thank you i am follow the karthik
Create a class and have a static Read only property like

C#
public class GlobalClass
{    public static string ConnectionString { get { return "Your connection string" // you can read form config also..
 } }
}


and in the all pages you can access like this
C#
string conn = GlobalClass.ConnectionString;


updated based on comments:

C#
public class GlobalClass
{

    public static Dictionary<string, string> ConnectionString
    {
        get
        { 
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Con1", "Your connection string 1");
            dict.Add("Con2", "Your connection string 2");
            dict.Add("Con3", "Your connection string 3");
            dict.Add("Con4", "Your connection string 4");
            return dict;
        }
    }
}


and you can access it as

C#
string con1 = GlobalClass.ConnectionString["Con1"];
       string con2 = GlobalClass.ConnectionString["Con2"];
       string con3 = GlobalClass.ConnectionString["Con3"];
       string con4 = GlobalClass.ConnectionString["Con4"];
 
Share this answer
 
v2
Comments
Arjunwalmiki 23-Jan-14 7:20am    
public class GlobalClass
{ public static string ConnectionString { get
{ return "Data Source=localhost;Initial Catalog=arjun;user id=root;password=root";
"Data Source=localhost;Initial Catalog=hr;user id=root;password=root";
"Data Source=localhost;Initial Catalog=bsales;user id=root;password=root";
} }
}

Karthik it is working fine but if i am using 5 database how can handle it
Karthik_Mahalingam 23-Jan-14 8:04am    
you can use list or dictionary then...
Karthik_Mahalingam 23-Jan-14 8:10am    
check my updated solution..
Arjunwalmiki 24-Jan-14 0:48am    
Hi Karthik gm

This is my code :-

protected void Button1_Click(object sender, EventArgs e)
{
string str = string.Empty;
DataSet ds1 = new DataSet();
string con1 = GlobalClass.ConnectionString["Con1"];
string con2 = GlobalClass.ConnectionString["Con2"];
string con3 = GlobalClass.ConnectionString["Con3"];
string con4 = GlobalClass.ConnectionString["Con4"];
foreach (ListItem li in CheckBoxList2.Items)
{
if (li.Selected)
{
MySqlConnection myconn3 = new MySqlConnection(con1);

myconn3.Open();
str = li.ToString();
string query = "show COLUMNS from " + str + "";
MySqlDataAdapter mydata = new MySqlDataAdapter(query, myconn3);
mydata.Fill(ds1,str);
myconn3.Close();
}
int a = 0;
foreach (DataTable dt in ds1.Tables)
{
if (a == 0)
{
TableCheckBoxList.DataSource = ds1.Tables[0];
TableCheckBoxList.DataTextField = "Field" ;
TableCheckBoxList.DataValueField = "Field";
TableCheckBoxList.DataBind();
}
if (a == 1)
{

CheckFromTable.DataSource = ds1.Tables[1];
CheckFromTable.DataTextField = "Field";
CheckFromTable.DataValueField = "Field";
CheckFromTable.DataBind();
}
a++;
}

}
}
my code only get one database table i want both database table in new checkboxlist
Karthik_Mahalingam 24-Jan-14 1:01am    
you want to merge it ?

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