Reports for WinForms | ComponentOne
Working with C1Report / Advanced Uses / Data Security / Building a ConnectionString with a User-Supplied Password
In This Topic
    Building a ConnectionString with a User-Supplied Password
    In This Topic

    Building a connection string with a user-supplied password is a very simple alternative to protect your data. For example, before rendering a report (or when the control reports a "failed to connect" error), you can prompt the user for a password and plug that into the connection string:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim strConn        
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _        
              "Data Source=C:\SecureData\People.mdb;" & _        
              "Jet OLEDB:Database Password={{THEPASSWORD}};"
    
    ' get password from the user        
    Dim strPwd$        
    strPwd = InputBox("Please enter your password:")        
    If Len(strPwd) = 0 Then Exit Sub
      
    ' build new connection string and assign it to the control        
    strConn = Replace(strConn, "{{THEPASSWORD}}", strPwd)        
    vsr.DataSource.ConnectionString = strConn
    

    To write code in C#

    C#
    Copy Code
    // build connection string with placeholder for password        
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +          
              "Data Source=C:\SecureData\People.mdb;" +          
              "Jet OLEDB:Database Password={{THEPASSWORD}};";
    
    // get password from the user        
    string strPwd = InputBox("Please enter your password:");        
    if (strPwd.Length == 0) return;
    
    // build new connection string and assign it to the control        
    strConn = Replace(strConn, "{{THEPASSWORD}}", strPwd);        
    c1r.DataSource.ConnectionString = strConn;