Thursday, October 24, 2019

Change IDENTITY_INSERT to ON in SQL server

Explicit identity insert require IDENTITY_INSERT property set to ON.

SET IDENTITY_INSERT MyTable ON  -- Statement Allows explicit values to be inserted into
                                                                     -- the identity column of a table.
GO

INSERT INTO MyTable(ID, Name, Description)
VALUES (0, 'Special title', 'special item');
GO

SET IDENTITY_INSERT MyTable OFF  -- Statement revert granted permission
GO

Wednesday, October 23, 2019

Disable All the Foreign Key Constraint in Database – Enable All the Foreign Key Constraint in Database

-- Disable all the constraint in database

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

-- Enable all the constraint in database

EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"

How to drop all tables from a database with one SQL query?

Use the INFORMATION_SCHEMA.TABLES view to get the list of tables. Generate Drop scripts in the select statement and drop it using Dynamic SQL:


DECLARE @sql NVARCHAR(max)=''

SELECT @sql += ' Drop table ' + QUOTENAME(TABLE_SCHEMA) + '.'+ QUOTENAME(TABLE_NAME) + '; '
FROM   INFORMATION_SCHEMA.TABLES
WHERE  TABLE_TYPE = 'BASE TABLE'

Exec Sp_executesql @sql


Sys.Tables Version


DECLARE @sql NVARCHAR(max)=''

SELECT @sql += ' Drop table ' + QUOTENAME(s.NAME) + '.' + QUOTENAME(t.NAME) + '; '
FROM   sys.tables t
       JOIN sys.schemas s
         ON t.[schema_id] = s.[schema_id]
WHERE  t.type = 'U'

Exec sp_executesql @sql

Note: If you have any foreign Keys defined between tables then first run the below query to
 disable all foreign keys present in your database.

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

Monday, October 21, 2019

How to change the table name dynamically in MSSQL Server

use TestingDB
go
SET NOCOUNT ON
DECLARE @cnt INT
DECLARE @table VARCHAR(128)
DECLARE @schema VARCHAR(100)
DECLARE @cmd VARCHAR(500)


IF OBJECT_ID('tempdb..#LISTOFTABLES') IS NOT NULL
BEGIN
    DROP TABLE #LISTOFTABLES
END
SELECT TABLE_SCHEMA,TABLE_NAME INTO #LISTOFTABLES
FROM INFORMATION_SCHEMA.TABLES
where table_type='BASE TABLE'

DECLARE tables CURSOR FOR
SELECT * FROM #LISTOFTABLES

OPEN tables
FETCH NEXT FROM tables INTO @schema, @table
WHILE @@fetch_status = 0
BEGIN
IF (OBJECT_ID(@schema+'.'+@table) IS NOT NULL)
BEGIN
SET @cmd = 'sp_rename '''+@table+''', ''NEW_'+@table +'''' --- PROBLEM IS HERE I GUESS
EXEC(@cmd)
PRINT @cmd
END
 FETCH NEXT FROM tables INTO @schema, @table
END
CLOSE tables
DEALLOCATE tables


Syntax :
The script for renaming any column :
sp_RENAME 'TableName.[OldColumnName]' '[NewColumnName]''COLUMN'
 The script for renaming any object (table, sp etc) :
sp_RENAME '[OldTableName]' '[NewTableName]'

Tuesday, October 15, 2019

Maximum request length exceeded.

Getting the error Maximum request length exceeded when I am trying to upload a video in my site.
How do I fix this?>
If you are using IIS for hosting your application, then the default upload file size if 4MB. To increase it, please use this below section in your web.config -

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="1048576" />
    </system.web>
</configuration>

For IIS7 and above, you also need to add the lines below:

 <system.webServer>
   <security>
      <requestFiltering>
         <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
   </security>
 </system.webServer>

Note:
maxRequestLength is measured in kilobytes
maxAllowedContentLength is measured in bytes
which is why the values differ in this config example. (Both are equivalent to 1 GB)

.NET 5

.NET 5 key features include

  1. A single unified platform for everything including Windows, Mac, Web, Mobile, Cloud, IoT, Gaming, Machine Learning and Data Science.
  2. Managed by open source community and supported by Microsoft.
  3. Cross-platform with any device anywhere.
  4. Supports all major platform capabilities for .NET Framework, .NET Core and Xamarin including Windows Forms, WPF, UWP, ASP.NET MVC, Entity Framework, LINQ and so on.
  5. Scalable, fast, and high performance.
  6. Smaller deployment and packages.
  7. Support of the most productive IDEs and tools including Visual Studio, VS Code, VS for Mac, and Command Line Interface (CLI) 






.NET Languages and Runtimes


Mono is the runtime used as a part of Xamarin to build cross-platform applications. CoreCLR is the runtime used as a part of .NET Core.  
In .NET 5, both runtimes will be supported, evolve, and will continue to work together.
.NET 5 will support major compilers, languages, and runtime that are currently supported by .NET Framework and .NET Core. C#, F#, and VB.NET are expected to be a part of .NET. XAML is also a major component as a part of .NET supported languages.













What’s Replaced 
EF Core - Entity Framework Core is replacing EF 6 in .Net Core Framework.

Asp.net Core - Asp.net is replaced by Asp.net Core.

ASP.NET Core MVC - ASP.NET Core MVC unified ASP.NET MVC and Web API. Check the migration guide for porting asp.net mvc app into asp.net core mvc.

MSIX - Microsoft new packaging tool which replaces the old MSI package installer for desktop applications.

JsonDocument - New Json Document library from System.Text.Json.JsonDocument API to replace json.net. It is 2-3x times faster than old json.net.

Friday, October 11, 2019

What is the difference between declarations, providers, and import in NgModule?

Angular Concepts

  • imports makes the exported declarations of other modules available in the current module
  • declarations are to make directives (including components and pipes) from the current module available to other directives in the current module. Selectors of directives, components or pipes are only matched against the HTML if they are declared or imported.
  • providers are to make services and values known to DI (dependency injection). They are added to the root scope and they are injected to other services or directives that have them as dependency.
A special case for providers are lazy loaded modules that get their own child injector. providers of a lazy loaded module are only provided to this lazy loaded module by default (not the whole application as it is with other modules).


  1. exports makes the components, directives, and pipes available in modules that add this module to imports. exports can also be used to re-export modules such as CommonModule and FormsModule, which is often done in shared modules
  2. entryComponents registers components for offline compilation so that they can be used with ViewContainerRef.createComponent(). Components used in router configurations are added implicitly.

Angular's @NgModule() imports and TypeScript import are entirely different concepts.


Components are declared, Modules are imported, and Services are provided. An example I'm working with:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import {FormsModule} from '@angular/forms';
import { UserComponent } from './components/user/user.component';
import { StateService } from './services/state.service';   

@NgModule({
  declarations: [
    AppComponent,
    UserComponent
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  providers: [ StateService ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

Thursday, October 3, 2019

The transaction log for database 'tempdb' is full due to 'ACTIVE_TRANSACTION'

You can search all the temp objects with a simple SELECT * FROM tempdb..sysobjects WHERE name LIKE '%Alltemptab%'
To fix it just run once:

BEGIN TRANSACTION
    DROP TABLE #Alltemptab
COMMIT TRANSACTION
If you still cant delete it just check for zombie sessions with SELECT * FROM sys.dm_exec_sessions and kill it with KILL session_id.

Change url query string value using jQuery

var url="http://www.test.com/admin/userSearch.aspx?FS=oldvalue";
var ReplaceNewValue="NewValue";
 var _fs = window.location.href.match(/FS\=([a-z0-9]+)/i);

                if (_fs != null) {
                    var value = window.location.href.substring(window.location.href.lastIndexOf('FS')); 
                    window.location = window.location.href.replace(value, 'FS=' +ReplaceNewValue + '');

                }

Result :  
   "http://www.test.com/admin/userSearch.aspx?FS=NewValue";