0

I am a newbie. Sorry!

My Windows Form App has 3 layer. Presentation has Form_Login with textEdit_Name and textEdit_Pass.

My Connection class:

public class _Connection
{
    public OleDbConnection GetConn(string _name, string _pass)
    {
        OleDbConnection _Conn = new OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID={1};Password={2};", @"C:\Test\Test.mdb", _name, _pass));
        return _Conn;
    }
}

My Data Access Layer:

public class getDAL : IDisposable
{
    private _Connection getConn = new _Connection();
    OleDbConnection _Conn = new OleDbConnection();
    public DataTable getDatatable()
    {
        _Conn = getConn.GetConn();
        //Do something
    }
}

How can I get _Conn with:

_name = textEdit_Name.Text and _pass = textEdit_Pass.Text

when user login through Form_Login

4
  • Do you create instance of DAL in Business Layer or Presentation Layer? Commented Jun 25, 2014 at 4:30
  • I did it in Business Layer. Commented Jun 25, 2014 at 4:32
  • That means in Form_Login you make instance of Business Layer and then in Business Layer you are creating instance of DAL? If yes then have to pass your name and pass to other layers. Commented Jun 25, 2014 at 4:33
  • Have a look at the OleDbConnectionStringBuilder class - this can help you create valid connection strings very easily Commented Jun 25, 2014 at 5:04

3 Answers 3

1

Perhaps the simplest method would be to set up the ConnectionString details in a static class in your DAL. Something like this:

public static class ConnectionDetails
{
    public static string UserName;
    public static string Password;

    public static string GetAccessConnectionString(string filepath)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", filepath);
        if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
            sb.Append("User Id=admin;Password=;");
        else
            sb.AppendFormat("User Id={0};Password={1};", UserName, Password);

        return sb.ToString();
    }
}

Your business layer needs to set the UserName and Password fields of the ConnectionDetails static class whenever they change in your presentation layer, and your DAL classes that need connection strings should get them by calling ConnectionDetails.GetAccessConnectionString with the appropriate file path. Or add FilePath as one of the static fields in that class so that you can set all three from wherever.

If you're intending to use more than one database file then a dictionary of connection data keyed off the file name would probably be a good idea.

Sign up to request clarification or add additional context in comments.

2 Comments

ConnectionStringBuilder is what you want here.
@HackSlash Yeah, true. But I did write that 4 years ago. I've moved on since then.
1

You can use the ConnectionStringBuilder Class

public static class BuildConnection()
{    
    public static GetConnectionString(var userName, var Password)
    {
          OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
          builder.Driver = "Microsoft Access Driver (*.mdb)";        
          builder.Add("Dbq", @"C:\Test\Test.mdb");
          builder.Add("User Id", userName);
          builder.Add("Password", Password);
          return builder.ConnectionString; // Here is your connection String 
    }
}

Comments

0

Here is example how you can pass LoginForm TextBox .Text to Business Layer and Data Access Layer.

Data Access Layer:

class DALClass : IDisposable
{
    private _Connection getConn;
    OleDbConnection _Conn;

    public DALClass()
    {
         getConn = new _Connection();
    }

    public DataTable getDatatable(string sUserName, string sUserPass)
    {
        //pass on user name and password
        _Conn = getConn.GetConn(sUserName, sUserPass);
        //Do something

        return default(DataTable);  //return datatable 
    }
}

public class _Connection
{
    public OleDbConnection GetConn(string _name, string _pass)
    {
        OleDbConnection _Conn = new OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID={1};Password={2};", @"C:\Test\Test.mdb", _name, _pass));
        return _Conn;
    }
}

Business Layer:

class BLClass
{
    DALClass _dal;       

    public BLClass()
    {
        _dal = new DALClass();            
    }

    public DataTable GetDataTable(string sUserName, string sUserPass)
    {
        return _dal.getDatatable(sUserName, sUserPass);
    }
}

Login Form:

 public partial class LoginForm : Form
 {
    BLClass _bl;

    public LoginForm()
    {
        InitializeComponent();
        //object of business layer
        _bl = new BLClass();
    }

    private void button1_Click(object sender, EventArgs e)
    {    
        //pass text boxes values to Business Layer      
        DataTable dt = _bl.GetDataTable(textEdit_Name.Text, textEdit_Pass.Text);            
    }
 }

9 Comments

No, it does not work... bl.sUserName and _bl.sUserPass is null when run to get connection string
@Camambeou. Sorry, I missed something obvious. Can you check my updated code?
Thanks for your idear. But it still doesn't work correctly. I don't know why. I think when we call the class is call a new one so the parameter is new too (get default value <null>)
Instances of business class and dal access are called once in the respective constructors. Notice in LoginForm() constructor BLClass instance _bl is instantiated.
I'm stuck, string _name and _pass always get null and application throw error "Invalid user name or password"... Do you have any idea?!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.