Thursday, December 20, 2012

How to change entity framework connection string dynamically

Use constructor with connection string

var db = new dbContext(connectionString);

How to change ASP.NET membership database connection string dynamically?

By reflection:
        private void SetProviderConnectionString(string connectionString)
            // Set private property of Membership, Role and Profile providers.
            FieldInfo connectionStringField = Membership.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic); 
            if (connectionStringField != null)
                connectionStringField.SetValue(Membership.Provider, connectionString);

            FieldInfo roleField = Roles.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
            if (roleField != null)
                roleField.SetValue(Roles.Provider, connectionString);

            FieldInfo profileField = ProfileManager.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
            if (profileField != null) 
                profileField.SetValue(ProfileManager.Provider, connectionString);

How to call WCF service in ASP.NET ?

Add following setting in web.config:


How to solve "CustomError for 403 Forbidden not working"?

In IIS 7 add following settings into web.config

How to use QR code in ASP.NET MVC?

The lib is: QRCODENET

public ActionResult QRImage(int id)
            string url = string.Format("{0}", id);
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = qrEncoder.Encode(url);

            GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);
            MemoryStream ms = new MemoryStream();
            renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
            byte[] image = ms.ToArray(); ;
            return File(image, "image/png");