views:

156

answers:

2

I have an asp.net nTier application. The data access layer is a strongly typed DataSet consisting of multiple DataTables with DataAdapters. When the user logs in, they choose which database to connect to (from a table in the membership database). I need to pass the selected connection string into the DataSet object which will remain the same for that users until they log in again.

I'm thinking that the answer might be to create a partial class of the DataSet object where I can pass the connection string into the constructor. Im not sure how to go about this though.

Cheers

+1  A: 

You could do this with a partial class.

Assuming your typed dataset is called HurrDurr:

public partial class HurrDurr
{
  public HurrDurr(string connex)
  {    
    this._connection = new global::System.Data.SqlClient.SqlConnection();
    this._connection.ConnectionString = connex;
  }
}

_connection is only initialized if it is null the first time the Connection internal property is accessed.

Will
Will, I tried your suggestion in vb.net but im getting an error: "Error 33 _connection' is not a member of DAL.ds1". Any ideas where im going wrong?
Fly_Trap
@fly I have no idea what a .ds1 file is. You can peek at the code created by the typed dataset tool (expand the xsd and look at the code files). The VB version may save the connection in a different variable name.
Will
Sorry I should have been clearer. DAL.ds1 refers to the project name "DAL" and dataset name "ds1". "ds1" is the class generated from the xsd, I assume this is what you refer to as HurrDurr.After looking at the code, I do see a _connection member variable in the table adapter? Is this what I need to create a partial class against?
Fly_Trap
You need to create a `public partial class ds1` and in the constructor create a SqlConnection with your new connection string and assign it to _connection.
Will
+1  A: 

Finally got to the bottom of this. In a new module I created a partial class to the table adapter which is where I needed to change the connection string, one mistake I was making originally was not specifying the correct namespace.

Below is the partial class I created which allowed me to dynamically change the connection string of one of my table adapters for a table called tblOptions:

Namespace ds1TableAdapters
    Partial Public Class tblOptionsTableAdapter
        Sub ChangeConnString(ByVal newConn As String)
            Me._connection.ConnectionString = newConn
        End Sub
    End Class
End Namespace

Thanks for the help Will, it got me going in the right direction.

Fly_Trap