1

I am using "Bltoolkit" to access sql server data.It works fine as per my requirement.Now I am trying to change connection string in runtime.I have tried below code but the connection is not changed that means its shows the default connection string.

protected void LogIn_Clicked(object sender, EventArgs e)
    {
        DataTable Login = new DataTable();

        string username = Request.Form["username"];
        string password = Request.Form["password"];
        bool remember = RememberMe.Checked;

        DbManager.AddConnectionString(ConfigurationManager.ConnectionStrings["Default"].ConnectionString); // Default Connection String


        using (DbManager Db = new DbManager())
        {
            Login = Db.SetCommand("Select * from UserList Where UserName = '" + username + "' And Password = '" + password + "' ").ExecuteDataTable();

            if (Login.Rows.Count > 0)
            {
                Session["UserId"] = Login.Rows[0]["UserId"];
                Session["UserName"] = Login.Rows[0]["UserName"];
                Session["Division"] = Login.Rows[0]["Division"];
                Db.SetCommand("UpDate UserList Set LoginTime = '" + Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd HH:mm") + "' Where UserId = " + Convert.ToInt16(Login.Rows[0]["UserId"]) + " ").ExecuteScalar();
                DbManager.AddConnectionString(ConfigurationManager.AppSettings["GLD"]); // Changed Connection string from Web.Config File
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                ErrorMessage.Text = "Invalid UserName or Password";
                ErrorMessage.Visible = true;
            }
            Db.Close();
            Db.Dispose();
        }
    }
KH IT
  • 19
  • 1
  • 7
  • 1
    why you want to change connection string? do you have multiple databases? If you wan't to change base connection string check this link: https://stackoverflow.com/questions/12521390/how-to-connect-sqlitedb-using-bltoolkit-on-asp-net might have same issue. P.s. not familia with BLToolkit but it should be simple as it seems it have webconfig where do you store your connection string. – Davit Mikuchadze Jun 02 '20 at 12:41
  • Yes I have multiple database.I would like to change connection based on User Login.. – KH IT Jun 02 '20 at 12:47
  • you don't need to change base connection string. Just add another one in Webconfig/Appconfig like: Database1 and Database2 and set different connection strings. which will give you probably multiple DbManager class. I suggest searching BLToolkit multiple database handling guide. – Davit Mikuchadze Jun 02 '20 at 13:07

0 Answers0