i need your to help to short out my problem. i m trying to connect my web application to different database. user select the database from user interface i.e asp.net page.the application will connect with that database.

for that i tried to make initial catalog dynamic but not get success. please help.

protected void Button1_Click(object sender, EventArgs e)
{
    string db = "";
    if (ddlDropdown.SelectedIndex != 0)
    {
        if (ddlDropdown.SelectedItem.Value == "2013")
        {
            db = "2013";
        }

        if (ddlDropdown.SelectedItem.Value == "2014")
        {
            db = "2014";
        }
    }
    string abc = "Data Source=.;Initial Catalog=" + db + ";Integrated Security=True";
    Session["abc"] = abc;
    SqlConnection con = new SqlConnection(abc);
    con.Open();
    Response.Redirect("rrr.aspx");
}
share
    
And there you have an open database connection. But I don't see what error you may encounter or what you want to achieve? – Matten Dec 10 '13 at 7:43
    
Any error message or exception? – Soner Gönül Dec 10 '13 at 7:43
    
please share your design code aswell. – Sudhakar Tillapudi Dec 10 '13 at 7:45
3  
What happen when SelectedIndex is zero or -1? – Steve Dec 10 '13 at 7:47
1  
@Steve: i feel OP Should assign db value to some default Database instead of initializing to empty string. – Sudhakar Tillapudi Dec 10 '13 at 7:49

If you are using sqlserver database system then just connect with one database in connection string, and while accessing tables of another database just use following syntax:

select * from databasename..tablename

databasename..tablename do works.

you can also use ti as follows:

select * from databasename..tablename t
where t.columnname=value

Try it.

share
    
no i don't want this.. i want to connect seprate database.. suppose user select 2013 than the application connect with 2013 database if user select 2014 than the application connect with 2014 database.same thing for other years... – user3085836 Dec 10 '13 at 7:55
    
@user3085836 things you are mentioning will increase overhead of your application – C Sharper Dec 10 '13 at 7:55
    
@user3085836 in that case ur above code(with some modifications) will also work, what is problem in it? – C Sharper Dec 10 '13 at 7:57
    
Though it is a technically correct answer, it is really a pain to add the name of the database and the two points at every table, view, or stored procedure – Steve Dec 10 '13 at 7:57
    
@Steve hahaha...as if we writes so many points to have join query...then why to feel pain in adding extra point? – C Sharper Dec 10 '13 at 7:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.