Tag Archives: Programming

MVC 5 Signout Fails

Recently I came across with a problem in the LogOff method that is generated with a new MVC 5 solution.

mvc

The problem was:

  • User1  logged in  and Login method is called in the Account controller.
  • User1 logs off and the LogOff method is called in the Account controller and the following code is executed:
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        AuthenticationManager.SignOut();
        return RedirectToAction("Login", "Account");
    }
    
  • User2 logged in  and Login method is called in the Account controller. After this the User1 is logged in instead of User2.

It seems this is a known issue where the explicit sign-in is preferred over a generic sign-out with no params. The solution is change the  call to SignOut and pass the AuthenticationType of the cookie, and this will override the explicit sign-in.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie);
    return RedirectToAction("Login", "Account");
}
Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

MVC bundle EnableOptimizations

Bundling and minification are two techniques you can use to improve request load time.

Bundling is a ASP.NET 4.5 feature to combine multiples files into a single file. This is used in CSS and JavaScript files.
Minification performs code optimizations (ex: remove comments) to CSS and JavaScript files to reduce size.

Bundling and minification are enabled or disabled by default by setting to true or false the Compilation element.

mvc

To change this behavior use the BundleTable.EnableOptimizations property and set to true to enable bundling and minification.

Many times in a production environment you need something else to control the bundling and minification especially when you need to diagnose a problem in your JavaScript.

You can use any logic you want or, for a simple scenario, use a app settings key.

ex:

BundleTable.EnableOptimizations = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["EnableOptimizations"]);

Place this code in your BundleConfig class inside method RegisterBundles.

Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

Drop all tables, stored procedures, views, functions

Recently I had a need to drop all tables, stored procedures, views, functions in a Sql Server database.

In Stackoverflow I’ve found the solution 🙂

And here it is:

/* Drop all non-system stored procs */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 ORDER BY [name])

WHILE @name is not null
BEGIN
    SELECT @SQL = 'DROP PROCEDURE [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Procedure: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all views */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP VIEW [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped View: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all functions */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP FUNCTION [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Function: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 AND [name] > @name ORDER BY [name])
END
GO

/* Drop all Foreign Key constraints */
DECLARE @name VARCHAR(128)
DECLARE @constraint VARCHAR(254)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)

WHILE @name is not null
BEGIN
    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    WHILE @constraint IS NOT NULL
    BEGIN
        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint) +']'
        EXEC (@SQL)
        PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name
        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    END
SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)
END
GO

/* Drop all Primary Key constraints */
DECLARE @name VARCHAR(128)
DECLARE @constraint VARCHAR(254)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)

WHILE @name IS NOT NULL
BEGIN
    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    WHILE @constraint is not null
    BEGIN
        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint)+']'
        EXEC (@SQL)
        PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name
        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)
    END
SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)
END
GO

/* Drop all tables */
DECLARE @name VARCHAR(128)
DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL
BEGIN
    SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@name) +']'
    EXEC (@SQL)
    PRINT 'Dropped Table: ' + @name
    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name])
END
GO
Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

Errors running builder ‘Android Pre Compiler’ on project

If you have this error

Errors running builder ‘Android Pre Compiler’ on project ‘XXX’

Than you might be running ADT 21
This is one possible workaround until the fix
  • Open properties of project in Eclipse
  • Then Resources -> Resource filters. Click the “Add…” button -> Check “Exclude all”, “Files and folders”, “All children”.
  • In the text entry box input “.svn” (without quotes).
  • Restart Eclipse.
Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

Alpha numeric input filter

In Android you should know that it’s possible to specify input filters in EditText.
You should also know that it’s possible to create custom input filters. In this post I wil show how to create an Alpha numeric input filter.

First create a class that implements InputFilter

In this class you will have to implement the filter method.

In this method you will have to see if chars in source argument are valid. Every time a char is not valid you will have to return “” and ever time a char is valid you will have to return null.

this is a sample of what should be done:

public class AlphaNumericFilter implements InputFilter {
private Context _context;

public AlphaNumericFilter(Context context) {
this._context = context;
}

@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {

for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return “”;
}
}
return null;
}
}

To use it it’s simple.

For a EditText just do this:
this._editText = (EditText) findViewById(R.id.edit_text);
this._editText.setFilters(new InputFilter[]{ new AlphaNumericFilter(MyActivity.this)});

For a EditTextPreference just do this:
this._editText = (EditTextPreference) prefs.findPreference(“edit_text”);
this._editText.getEditText().setFilters(new InputFilter[]{new AlphaNumericFilter(MyPreferenceActivity.this) });;

Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

E/OpenGLRenderer(2268): Cannot generate texture from bitmap

After my update to Jelly Bean I’ve stated having this error in my apps:

E/OpenGLRenderer(2268): Cannot generate texture from bitmap
The bad news is that I don’t have any solution
The good news is that I’ve reported this error and it will be addressed in future releases.
UPDATE:
A possible workaround consists in using PNG files instead of GIF files. I’ve tested and it works
In the attachment there is a sample that show the problem.
Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

Android Volume Adjust

I’m in a process of restructure one off my apps and one think that I didn’t know until recently is that I can specify in my activity what kind of volume My activity will play. This is useful so that when press key volume up and volume down, I can in fact increase or decrease volume of the media i’m playing.

This is done using the method setVolumeControlStream.
This method accepts one of the following stream types.
AudioManager.STREAM_MUSIC: For media files
AudioManager.STREAM_RING: For ringer volume
AudioManager.STREAM_ALARM: For alarms
AudioManager.STREAM_NOTIFICATION: For notifications
AudioManager.STREAM_SYSTEM: For system volume
AudioManager.STREAM_VOICECALL: for in call volume
In my case my app will reproduce mp3 so I want AudioManager.STREAM_MUSIC.
Additional to setting my stream to music I can also set volume.
This can be done either using setStreamVolume or adjustStreamVolume
Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

Align RadioButtons in left and right with RadioGroup

Tricky question how to align on RadioButton to the left and another RadioButton to the right using a RadioGroup?

And this is one solution:
    <RadioGroup
    android:layout_width=”fill_parent”
    android:layout_height=”wrap_content”
    android:orientation=”horizontal”>
    <RadioButton
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_weight=”1″
        android:layout_gravity=”center|left”/>
    <RadioButton
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_gravity=”center|right” />
    </RadioGroup>
Basiclly you will need to add to the first RadioButton this:
android:layout_weight=”1″
android:layout_gravity=”center|left”
and add to the second RadioButton this:
android:layout_gravity=”center|right”
Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

Changes in ADT 14

With the release of Android 4 came ADT 14.
In ADT 14 it’s finally possible to create jar when project is configured as a library. This jar can then be used in other projects.
With ADT 14 code must also change especially if you use resource ids in switch. If you use resource ids in switch check this page and follow instructions.
Today I’ve while I was migrating to ADT 14 I’ve came to this error:
Dx UNEXPECTED TOP-LEVEL EXCEPTION:
“Conversion to Dalvik format failed with error 1”
It seems it’s a known Issue
The solution is to remove those <libraryname>_src source folders from your projects. Just right click them and choose Build Path > Remove from Build Path. You should also be prompted to remove the linked folder which you should do. If you’re not prompted, remove it manually.
Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter