Wednesday, October 30, 2019

Drop everything in a MS SQL server database

 The SQL script must be execute on the database level. Optionally you can add an
  USE ‘your Database Name’     statement on the top.

/* 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
                                                                                                                         

Easiest way to generate MachineKey

Use IIS 7.5 user interface to generate the MachineKey section and save it in the web.config of your application / root web.config file. Steps are quite easy:
1) Open IIS manager.
2) If you need to generate and save the MachineKey for all your applications select the server name in the left pane, in that case you will be modifying the root web.config file (which is placed in the .NET framework folder). If your intention is to create MachineKey for a specific web site/application then select the web site / application from the left pane. In that case you will be modifying the web.config file of your application.
3) Double click the Machine Key icon in ASP.NET settings in the middle pane:




4) MachineKey section will be read from your configuration file and be shown in the UI. If you did not configure a specific MachineKey and it is generated automatically you will see the following options:

5) Now you can click Generate Keys on the right pane to generate random MachineKeys. When you click Apply, all settings will be saved in the web.config file.






Tuesday, October 22, 2019

How to alter column to identity(1,1)

u have 2 options,
1. Create a new table with identity & drop the existing table
2. Create a new column with identity & drop the existing column
Create table testtable(id int,name varchar(10))
insert into testtable values(1,'AAAAAA')
insert into testtable values(2,'BBBBB')
go
select * from testtable
go
Alter Table testtable Add Id_new Int Identity(1,1)

Go



Alter Table testtable Drop Column ID

Go



Exec sp_rename 'testtable.Id_new', 'ID','Column'

insert into testtable values('CCCC')


select * from testtable

================================================
connect to your db

show the table/column names in object explorer window

right click on column name

select modify

in column properties tab

   alter "identity specification"/"is identity" to YES

   set increment/seed properties as desired

Thursday, October 25, 2018

Change Fonts and Colors in Visual Studio

Change the background color of your code editor

Change the Color Theme of the IDE

On the menu bar, choose Tools, Options.



In the options list, choose Environment, General.



In the Color theme list, choose either the default Blue theme, Dark or Light.

To change the font and size of all text in the IDE

1.On the menu bar, choose Tools, Options.

2.In the options list, choose Environment, Fonts and Colors.

3.In the Show settings for list, choose Environment Font.

4.In the Font list, choose a font.

5.In the Size list, choose a text size, and then choose the OK button.

Tuesday, October 23, 2018

JQuery: how to replace all between certain characters?

Syntax:
string.replace(/abc.+xyz/,"abc"+newString+"xyz");


Input value :
 var inputString="(10.00%)(20.00%)(10.00%)Testing Value";

 var   Output =inputString.replace(/\(.*%\)/ig,"");

Result value :
    Testing Value


SQL replace all characters between two strings

How can I use replace statement in SQL query to replace/remove all the text inside the square bracket and the square bracket itself. I have many similar text like that with square bracket in my database, can I use regular expression or something similar?

Solution :
declare @str varchar(100)
select @str='This is Sql testing Text [23/10/2018 12:23 - admin]'
select stuff(@str,patindex('%(%',replace(@str,'[','(')),LEN(@str),'')

Result :  This is testing Text 

Tuesday, January 23, 2018

Redirect to login page if Idle Timeout is 10mins

MVC

The Following javascript file put in Layout Page(Views/Shared/_layout.cshtml ) on MVC page.

<script type="text/javascript">
        rootUrl = '@Url.Content("~/")';
        var t;
        window.onload = resetTimer;
        document.onkeypress = resetTimer;

        function logout() {
           // alert("You are now logged out.")
            // location.href = 'Logoff.php'
            window.location.href = '@Url.Action("Login", "Register")';

        }

        function resetTimer() {

            clearTimeout(t);
           // t = setTimeout(logout, 60000) //logs out in 1 min
         t = setTimeout(logout, 600000) //logs out in 10 min
        }


    </script>
================
Web Page(ASP.NET)

 The Following javascript file put on Master Page.(MasterPage.master) on ASP.NET Site

 <script type="text/javascript">
 var t;
        window.onload = resetTimer;
        document.onkeypress = resetTimer;

        function logout() {
            // alert("You are now logged out.")
            // location.href = 'Logoff.php'
            //window.location.origin : you'll get http://sub.domain.com *****
            window.location.href = window.location.origin+"/Account/login";

        }

        function resetTimer() {
            clearTimeout(t);
         //   t = setTimeout(logout, 60000) //logs out in 1 min
             t = setTimeout(logout, 600000) //logs out in 10 min

        }
    </script>

Mixed Content: The page at xxx was loaded over HTTPS, but requested an insecure

 Mixed Content: The page at ' https ://www.test.com/signup.aspx' was loaded over HTTPS, but requested an insecure script ' http ...