0

So, I'm writing a program that imports excel files to a database, finds matches, non matches, shows the tables, etc.

In the beginning I added manually just my database connection string, but I have to make my program let the connection string change.

In case none is inserted, it will use the last one inserted.

Behind code:

protected void BtnFazerTudo_Click(object sender, EventArgs e)
    {
        var connectionString = ConfigurationManager.ConnectionStrings["Db"].ConnectionString;
        connectionString = connectionString.Replace("{username}", TxtUtilizador.Text);
        connectionString = connectionString.Replace("{pwd}",TxtPalavraPasse.Text);
        connectionString = connectionString.Replace("{DataSource}", TxtHost.Text);
        connectionString = connectionString.Replace("{Initial}", TxtBaseDeDados.Text);

        Debug.Write(con);
    }

Web Config:

<add name="Db" connectionString="Data Source ={DataSource} ;Initial Catalog= 
{Initial};Persist Security  
Info=True;User ID={username};Password={pwd}"/>

I already tried the String builder. I was able to output it in the debug window but I have no idea in how I transfer that connection made by string builder to other web forms.

Grigoris Loukidis
  • 383
  • 2
  • 7
  • 23
J.Doe
  • 1
  • 1
  • I think what you need it dynamic datasource? which component or datasource you will use with dynamic connection string? – Fedri Qrueger May 31 '19 at 08:43
  • @FedriQrueger Sorry for being ignorant but I'm not understanding... Anyway, I dont think that is what I'm asking, I have like a webform where the user insert his `Data Source: xxxx/SQlExpress` `Initial Catalog: mydb` `User id = somename` `password = blahblah` all that with textboxes... and after that I would like to all my web forms have that same connection string that was created with the user data... – J.Doe May 31 '19 at 11:36

1 Answers1

0

The connection string you're creating needs to be stored somewhere so as all pages of your web application can access it. While there are some choices that let you pass data among pages none of them suits you mainly because (a) the connection string contains security sensitive information such as the password!!! and (b) they do not offer the level of persistence required by your scenario (unless you want the user to fill the textboxes every time he connects to your application).

Having said that, one viable option is to store the connection string in a database and retrieve it every time you need it. You can either store it as a whole string or each user data separately (i.e catalog, user id, password etc). In the latter case, you'd have to re-create the connection string every time you need it.

How you create the connection string has nothing to do with your original question. It is just about string concatenation in C# for which you can use the + or += operators, string interpolation or the String.Format, String.Concat, String.Join or StringBuilder.Append methods.

dpant
  • 1,622
  • 19
  • 30