Web Code Blog

A web repository of coding tips and knowledge base articles

Archive for November, 2010

Click Once Application Error – The signer’s certificate is not valid for signing

Posted by Stefan Zvonar on November 17, 2010

Problem:

When trying to publish a Click Once application, you recieve the following error:
“The signer’s certificate is not valid for signing.”
Reason:
This is most likely due to the fact that the certificate used to publish the application originally has expired.  Most certificates have a lifespan of about a year, so it needs updating now and then, especially when publishing new versions of the application.

Solution:
To get around this problem, you need to create a new certificate.
Open your project in Visual Studio and go to the Project menu -> Project Properties.  Click on the Signing tab and click on the “Create Test Certificate” button (as shown in the image below).

Create Test Certificate

Create Test Certificate

I left the password blank, but if you do use a password, be sure to remember it a year later, or whenever you need to regenerate another certificate :)
This will place a ????.pfx file in your project.  This file contains the private and public key informaiton.  I simply removed the old file (backed it up first) and renamed my new generated file to the old file name.
You should now be able to publish your Click Once application.

Hope this helps,

Stefan.

For more Web Code, ASP.NET, SQL Server and other development tips, please check back here at webcodeblog.com often.

Posted in .NET, Visual Studio | Leave a Comment »

Replacing all instances of a piece of text inside a SQL Server Database

Posted by Stefan Zvonar on November 12, 2010

So, have you ever had to replace a piece of text wherever it occured inside a SQL Server Database (without knowing where to even look)?  This can be a time consuming task, especially if you have a database with hundreds of tables and columns.

I had to perform this recently when a server moved to a new domain (changing all usernames etc).

Here is how you can make a quick start.  What this article will explain is how to generate the script which you can then use to perform the alteration (although of course you will need to review the resultant script, change it, test it and run at your own risk!)

First, open SQL Server Management Studio and connect to your database, creating a blank query window.  You will need to also click on the “Results to Text” button, like shown below.

Results to text

Results to text

Now copy and paste the below script into your new query window.  This script will perform the script generation, replacing all instances of ‘ABC’ with ‘XYZ’.  Here is where you change what string you are wanting to replace with its new value.

    SET NOCOUNT ON
    DECLARE @SearchStr varchar(100)
    DECLARE @ReplaceStr varchar(100)
    SET @SearchStr = 'ABC'
    SET @ReplaceStr = 'XYZ'

    DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @Counter int, @Exists int

    SET @TableName = ''
    SET @Counter = 0

    CREATE TABLE #StringFound
    (
     occurances varchar(1000),
     tableName varchar(1000),
     columnName varchar(1000)
    )

    WHILE @TableName IS NOT NULL
    BEGIN
        SET @ColumnName = ''
        SET @TableName =
        (
            SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
            FROM     INFORMATION_SCHEMA.TABLES
            WHERE         TABLE_TYPE = 'BASE TABLE'
                AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
                AND    OBJECTPROPERTY(
                        OBJECT_ID(
                            QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                             ), 'IsMSShipped'
                               ) = 0
        )

        WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
        BEGIN
            SET @ColumnName =
            (
                SELECT MIN(QUOTENAME(COLUMN_NAME))
                FROM     INFORMATION_SCHEMA.COLUMNS
                WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                    AND    TABLE_NAME    = PARSENAME(@TableName, 1)
                    AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
                    AND    QUOTENAME(COLUMN_NAME) > @ColumnName
            )

            IF @ColumnName IS NOT NULL
            BEGIN

                INSERT INTO #StringFound EXEC ('SELECT COUNT(*), ''' + @TableName + ''', ''' + @ColumnName + '''  FROM ' + @TableName + ' WHERE ' + @ColumnName + ' LIKE ''%' + @SearchStr + '%'' HAVING COUNT(*) > 0')
                SET @Counter = @Counter + @@ROWCOUNT

            END
        END
    END

    SELECT 'EXEC sp_MSforeachtable ''ALTER TABLE ? DISABLE TRIGGER ALL'''

    SELECT 'UPDATE [' + DB_NAME() + '].' + tableName + CHAR(13) + CHAR(10) +
           'SET ' + columnName + ' = REPLACE(' + columnName + ', ''' + @SearchStr + ''', ''' + @ReplaceStr + ''')' + CHAR(13) + CHAR(10) +
           'GO' + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
    FROM #StringFound
    ORDER BY tableName, columnName

    SELECT 'EXEC sp_MSforeachtable ''ALTER TABLE ? ENABLE TRIGGER ALL'''

    DROP TABLE #StringFound

    --PRINT CAST(@Counter AS varchar) + ' occurence(s)'

    SET NOCOUNT OFF

Simply execute the query and view the results.  Here is an example of the output script:

----------------------------------------------------------
EXEC sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE [TEMP_DATABASE].[dbo].[SIMPLE_NAME]
SET [FIRST_NAME] = REPLACE([FIRST_NAME], 'ABC', 'XYZ')
GO

UPDATE [TEMP_DATABASE].[dbo].[SIMPLE_NAME]
SET [LAST_NAME] = REPLACE([LAST_NAME], 'ABC', 'XYZ')
GO

UPDATE [TEMP_DATABASE].[dbo].[SIMPLE_NAME]
SET [USERNAME] = REPLACE([USERNAME], 'ABC', 'XYZ')
GO

---------------------------------------------------------
EXEC sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL'

As stated previously, never run this straight onto your database without testing and verifying the results.  You will also probably want to fine tune the script so that its updates are more precise (adding in WHERE clauses to keep the changes more precise).  However, this should give you a good place to start and see what tables and columns you need to look for!

Hope this helps,

Stefan.

For more Web Code, ASP.NET, SQL Server and other development tips, please check back here at webcodeblog.com often.

Posted in SQL Server | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.