Thursday, 6 September 2018

Windows Service - Add Dependency - After Service is Installed

I have a Windows service, that has a dependency on MSMQ (Message Queuing). So I want my service to start only after MSMQ service is started completely. Else my service will run into some anomalies.

A way to do it is to add the dependency in the service install, but this dependency came later on. So now I need to add the dependency post install.

You can do it via an elevated comand prompt using sc command. Follow the below syntax:

sc config [service name] depend= <dependencies by="" forward="" separated="" slash="">

Note 1 : Retain the space after the depend=. This is important.
Note 2 : depend= will overwrite and not append to existing dependencies list. So if you have multiple dependencies, remember to add all the required dependencies while trying to add new one as well.

Example 1:
Below command will add a dependency of service Service2 to service Service1. That means Service1 will start only after Service2 starts. Also Service1 will stop if Service2 is stopped.
sc config Service1 depend= Service2
Example 2:
Command for dependency on multiple services is added below. Here Service1 will not start until Service2, Service3, Service4 are started. Also if any of the dependent services are stopped Service1 will stop.
sc config Service1 depend= Service2/Service3/Service4
Example 3:
Remove all dependencies:
sc config Service1 depend= /
Example 4:
List all dependencies:
sc qc Service1

Sunday, 15 January 2017

SQL Server - List all Triggers in a Database

You can use the below query to list ass Triggers in your database.
It lists the Trigger name, table name and the type of trigger (update / delete / insert etc).

SELECT AS trigger_name 
    ,USER_NAME(sysobjects.uid) AS trigger_owner 
    , AS table_schema 
    ,OBJECT_NAME(parent_obj) AS table_name 
    ,OBJECTPROPERTY( id, 'ExecIsUpdateTrigger') AS isupdate 
    ,OBJECTPROPERTY( id, 'ExecIsDeleteTrigger') AS isdelete 
    ,OBJECTPROPERTY( id, 'ExecIsInsertTrigger') AS isinsert 
    ,OBJECTPROPERTY( id, 'ExecIsAfterTrigger') AS isafter 
    ,OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger') AS isinsteadof 
    ,OBJECTPROPERTY(id, 'ExecIsTriggerDisabled') AS [disabled] 
FROM sysobjects 

INNER JOIN sysusers 
    ON sysobjects.uid = sysusers.uid 

INNER JOIN sys.tables t 
    ON sysobjects.parent_obj = t.object_id 

INNER JOIN sys.schemas s 
    ON t.schema_id = s.schema_id 

WHERE sysobjects.type = 'TR' 

Sunday, 11 December 2016

C# - Update Querystring Values in a Url

We will look into a solution to update querystring values in a Url.

using System.Web;

// url has the complete url with querystring 
// ex:
var uriBuilder = new UriBuilder(url);

var query = HttpUtility.ParseQueryString(uriBuilder.Query);

// Existing Querystring parameter xyz value will be updated from 20 to 10
query["xyz"] = "10";

// As QS Parameter hello doesn't existed, it will be added.
query["hello"] = "world";

// Set updated querystring to the Url.
uriBuilder.Query = query.ToString();

// New Url:

Remember to import namespace "System.Web"

Thursday, 8 December 2016

ASP.NET MVC C# - Redirect to External Website from Controller Action Method

Refer the below code snippet for redirection from ASP.NET MVC Controller to an external website.
The MVC Controller method "Redirect()", is used for redirection from MVC Controller.

public ActionResult Index()
    return Redirect("");

Redirect() method can be used for redirecting to an internal url as well.
Find the sample code below:
public ActionResult Index()
    return Redirect("/Customer/List");

Monday, 24 October 2016

SQL - While Loop Syntax

Below is the WHILE LOOP Syntax in SQL

DECLARE @cnt INT = 1;


PRINT @cnt;
SET @cnt = @cnt+1;


SQL Doesn't have For Loop, instead you have to use While loop.

SQL - For Loop Syntax

SQL doesn't have For Loop, instead you have to use While loop.
The syntax is as below.

DECLARE @cnt INT = 1;


PRINT @cnt;
SET @cnt = @cnt+1;


Thursday, 18 August 2016

SQL Server - Get DB Object Names, Count

FROM sysobjects sobjects
WHERE sobjects.xtype = 'U'

The above example lists all the tables in the database.
You can modify the query to access other details about the db objects.
Also the same can be used to count the number of objects (tables / stored procedures) in the database.

Below are the reference to the other XTYPE values corresponding to each objects.

-- AF: Aggregate function (CLR)
-- C: CHECK constraint
-- D: Default or DEFAULT constraint
-- F: FOREIGN KEY constraint
-- L: Log
-- FN: Scalar function
-- FS: Assembly (CLR) scalar-function
-- FT: Assembly (CLR) table-valued function
-- IF: In-lined table-function
-- IT: Internal table
-- P: Stored procedure
-- PC: Assembly (CLR) stored-procedure
-- PK: PRIMARY KEY constraint (type is K)
-- RF: Replication filter stored procedure
-- S: System table
-- SN: Synonym
-- SQ: Service queue
-- TA: Assembly (CLR) DML trigger
-- TF: Table function
-- TR: SQL DML Trigger
-- TT: Table type
-- U: User table
-- UQ: UNIQUE constraint (type is K)
-- V: View
-- X: Extended stored procedure