Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
i am face one problem hope you help me
I am 4 connection string in Web.Config And Switch Betweeen Them In Code Behind as per check selected in checkboxList?

please let me know how can i use it 
Web.Config


C#
<connectionStrings>
 <add name="mysql" connectionString="Server=localhost;database=arjun;user id=root;password=root;" />
    <add name="mysql1" connectionString="Server=localhost;database=bsales;user id=root;password=root;" />
    <add name="mysql2" connectionString="Server=localhost;database=hr;user id=root;password=root;" />
</connectionStrings>
 

protected void CheckBoxList3_SelectedIndexChanged(object sender, EventArgs e)
    {
 
        foreach (ConnectionStringSettings c in System.Configuration.ConfigurationManager.ConnectionStrings)
        {
            string Name = c.Name;
            string connString = c.ConnectionString;
            string provider = c.ProviderName;
        
        }
        MySqlConnection myconn = new MySqlConnection(strmysqlcon2);
        MySqlConnection myconn1 = new MySqlConnection(strmysqlcon8);
        string str = string.Empty;
        DataSet ds2 = new DataSet();
       
        foreach (ListItem li in CheckBoxList2.Items)
        {
            if (li.Selected)
            {
                myconn.Open();
                str = li.ToString();
                string query = "show COLUMNS from " + str + "";
                MySqlDataAdapter mydata = new MySqlDataAdapter(query, myconn);
 
                mydata.Fill(ds2, str);
                myconn.Close();
            }
            int p = 0;
            foreach (DataTable dt in ds2.Tables)
            {
                if (p == 0)
                {
                    TableCheckBoxList3.DataSource = ds2.Tables[0];
                    TableCheckBoxList3.DataTextField = "Field";
                    TableCheckBoxList3.DataValueField = "Field";
                    TableCheckBoxList3.DataBind();
 
                }
                if (p == 1)
                {
 
                    CheckFromTable3.DataSource = ds2.Tables[1];
                    CheckFromTable3.DataTextField = "Field";
                    CheckFromTable3.DataValueField = "Field";
                    CheckFromTable3.DataBind();
                }
                p++;
 
            }
 
        }
    }





ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownList.aspx.cs" Inherits="DropDownList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <asp:DropDownList ID="DropDownList3" runat="server" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged"
            AutoPostBack="True">
            <asp:ListItem Selected="True">FoxPro
            <asp:ListItem>MySQL
            <asp:ListItem>Data3

    </div>
    <div>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="" DataValueField=""
            OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">

    </div>
    <div>
        <table>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                    <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
                    <asp:Button ID="Button3" runat="server" Text="Button" OnClick="Button3_Click" />
                </td>

            </tr>
        </table>
    </div>
    <div>
        <table>
            <tr>
            <td>
                    <asp:CheckBoxList ID="CheckBoxList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList2_SelectedIndexChanged">

                </td>
                <td>
                    <asp:CheckBoxList ID="CheckBoxList3" runat="server"
                        onselectedindexchanged="CheckBoxList3_SelectedIndexChanged"
                        AutoPostBack="True">

                </td>
                </tr>
                <tr>
                <td>
                    <asp:CheckBoxList ID="TableCheckBoxList" runat="server">

                </td>
                <td><asp:CheckBoxList ID="CheckFromTable" runat="server">
                        </td>
                        <td><asp:CheckBoxList ID="TableCheckBoxList3" runat="server">
                        </td>
                        <td><asp:CheckBoxList ID="CheckFromTable3" runat="server">
                        </td>
            </tr>
            <tr>
                <td>
                    <asp:DropDownList ID="Dropdownlist1" runat="server">

                    </td>

            </tr>
        </table>
    </div>
    </form>
</body>
</html>
Posted
Updated 15-Jan-14 23:02pm
v2
Comments
Rajkumar_Blr 16-Jan-14 1:33am    
I think you should use *Radio Button*(single selection allowed) and not checkbox(multiple selection allowed)
Arjunwalmiki 16-Jan-14 1:41am    
Rajkumar Thank you for reply
i want to select single connectionStrings when user check in checkboxlist.
Karthik_Mahalingam 16-Jan-14 2:04am    
what issue you r facing >?
Arjunwalmiki 16-Jan-14 2:15am    
Hi Karthik

i am not get right connection from connection String when user select database table from checkBoxList
Karthik_Mahalingam 16-Jan-14 3:17am    
post your aspx code for checkbox list

1 solution

you can use something like below code,

C#
            CheckBoxList1.Items.Add("mysql1");
            CheckBoxList1.Items.Add("mysql2");
            CheckBoxList1.Items.Add("mysql3");
            .....
            .....

            string conne=CheckBoxList1.SelectedItem.Text
 
            //Connect to database
            .....

           
Note: Here your CheckBoxList1 items name should match with your connectionstring name(mysql)

1,mysql2
 
Share this answer
 
v3
Comments
Arjunwalmiki 16-Jan-14 2:05am    
Hi Rajkumar thank you for prompt reply and you valuable time

I am already code inside the CheckBoxList3_SelectedIndexChanged and in the below code i am able to get correct data but my connection string is hard-coded but i want to handle dynamically.So pls let me know.
string strmysqlcon3 = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
protected void CheckBoxList3_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlConnection myconn = new MySqlConnection(strmysqlcon3);
string str = string.Empty;
DataSet ds1 = new DataSet();
foreach (ListItem li in CheckBoxList3.Items)
{
if (li.Selected)
{
myconn.Open();
str = li.ToString();
string query = "show COLUMNS from " + str + "";
MySqlDataAdapter mydata = new MySqlDataAdapter(query, myconn);

mydata.Fill(ds2,str);
myconn.Close();
}
int p = 0;
foreach (DataTable dt in ds2.Tables)
{
if (p == 0)
{TableCheckBoxList3.DataSource = ds2.Tables[0];
TableCheckBoxList3.DataTextField = "Field";
TableCheckBoxList3.DataValueField = "Field";
TableCheckBoxList3.DataBind();

}
if (p == 1)
{

CheckFromTable3.DataSource = ds2.Tables[1];
CheckFromTable3.DataTextField = "Field";
CheckFromTable3.DataValueField = "Field";
CheckFromTable3.DataBind();
}
p++;

}

}


}
Rajkumar_Blr 16-Jan-14 2:15am    
The below is my understanding of the probelm,

strmysqlcon3 - is a connection string(You are already retrieving from the config section dynamically ) and
CheckBoxList3.Items contains list of table in the database.

So what are you expecting?
Arjunwalmiki 16-Jan-14 3:31am    
if you seen in the code i am connecting with database name "arjun"(
Arjunwalmiki 16-Jan-14 3:37am    
if you seen in the code i am connecting with database name "arjun"(<add name="mysql" connectionstring="Server=localhost;database=arjun;user id=root;) but is user select database name " bsales="(<add name=" mysql1=" connectionString=" server="localhost;database=bsales;user" id="root;)" or="" database="hr">

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