/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Thursday, May 29, 2014

Full path URL for images for ASP.NET

C# SOLUTION imgLogo.Src = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + System.Web.HttpContext.Current.Request.ApplicationPath + "/images/logo.png"; How do I turn a relative URL into a full URL? public string ConvertRelativeUrlToAbsoluteUrl(string relativeUrl){ return string.Format("http{0}://{1}{2}",(Request.IsSecureConnection) ? "s" : "", Request.Url.Host, Page.ResolveUrl(relativeUrl));} Reference:- http://ckeditor.com/forums/FCKeditor-2.x/C-SOLUTION-Full-path-URL-for-images-for-.NET-connector Get...
Read More »

Friday, May 23, 2014

How to get the cell value by column name not by index in GridView in asp.net

GridView does not act as column names, as that's it's `datasource` property to know those things. If you still need to know the index given a column name, then you can create a helper method to do this as the `gridview` Header normally contains this information.     int GetColumnIndexByName(GridViewRow row, string columnName)     {         int columnIndex = 0;         foreach (DataControlFieldCell cell in row.Cells)  ...
Read More »

Thursday, May 22, 2014

identity increment and identity seed in sql server

Syntax IDENTITY [ ( seed , increment ) ] Arguments seed Is the value that is used for the very first row loaded into the table. increment Is the incremental value that is added to the identity value of the previous row that was loaded. You must specify both the seed and increment or neither. If neither is specified, the default is (1,1). References: http://technet.microsoft.com/en-us/library/aa933196(v=sql.80).asp...
Read More »

Wednesday, May 21, 2014

Using Merge query in sql server

Using Merge query in sql server Use [example] Target Table CREATE TABLE dbo.Customers (   custid      INT         NOT NULL,   companyname VARCHAR(25) NOT NULL,   phone       VARCHAR(20) NOT NULL,   [address]     VARCHAR(50) NOT NULL,   CONSTRAINT PK_Customers PRIMARY KEY(custid) ); INSERT INTO dbo.Customers(custid, companyname, phone, address)   VALUES   (1, 'cust 1', '(111) 111-1111', 'address...
Read More »

Merge query in sql server 2008

Create a target table CREATE TABLE Products ( ProductID INT PRIMARY KEY, ProductName VARCHAR(100), Rate MONEY ) GO Insert records into target table INSERT INTO Products VALUES (1, 'Tea', 10.00), (2, 'Coffee', 20.00), (3, 'Muffin', 30.00), (4, 'Biscuit', 40.00) GO Create source table CREATE TABLE UpdatedProducts ( ProductID INT PRIMARY KEY, ProductName VARCHAR(100), Rate MONEY ) GO Insert records into source table INSERT INTO UpdatedProducts VALUES (1, 'Tea', 10.00), (2, 'Coffee',...
Read More »

Select Top nth salary from table in sql server salary query

Top 3rd Amt Query SELECT MIN(Amt) AS Amt FROM Salary WHERE Amt IN (SELECT TOP 3 Amt FROM Salary ORDER BY Amt DESC) Second Highest Amount SELECT MAX(Amt) FROM Salary WHERE Amt NOT IN (SELECT MAX(Amt) FROM Salary) Top Nth Highest Amt SELECT TOP 1 Amt FROM (SELECT DISTINCT TOP N Amt FROM B ORDER BY Amt DESC)PARA ORDER BY Amt SELECT TOP 1 Amt FROM (SELECT DISTINCT TOP 5 Amt FROM B ORDER BY Amt DESC)PARA ORDER BY Amt Using Row_Number() Method SELECT * FROM (SELECT *, ROW_NUMBER() OVER...
Read More »

Part-2 Grouping Sets in sql server 2008 GROUPING SETS, CUBE, and ROLLUP GROUPING_ID function

Part-2 Grouping Sets in sql server 2008 GROUPING SETS, CUBE, and ROLLUP GROUPING_ID function CREATE TABLE tbl_Employee  (        Employee_Name varchar(25),        Region varchar(50),        Department varchar(40),        sal int  ) truncate table tbl_Employee INSERT into tbl_Employee(                               ...
Read More »

Grouping sets in sql server 2008 GROUPING SETS, CUBE, and ROLLUP GROUPING_ID function

Grouping Sets in sql server 2008 GROUPING SETS, CUBE, and ROLLUP GROUPING_ID function CREATE TABLE dbo.Orders (   orderid   INT        NOT NULL,   orderdate DATETIME   NOT NULL,   empid     INT        NOT NULL,   custid    VARCHAR(5) NOT NULL,   qty       INT        NOT NULL,   CONSTRAINT PK_Orders PRIMARY KEY(orderid) ); INSERT INTO dbo.Orders (orderid,...
Read More »

Using sql server generating random number

Using sql server generating random number SELECT randomNumber FROM (SELECT ABS(CAST(NEWID() AS binary(6)) %50) + 1 randomNumber FROM sysobjects) sample GROUP BY randomNumber --ORDER BY randomNumber Declare @Random varchar(10) set @Random = CONVERT(varchar(10), right(newid(),10)) SELECT TOP 50 randomNumber FROM (SELECT CAST(ABS(CAST(NEWID() AS binary(6)) %50) + 1 AS VARCHAR(10)) + CONVERT(varchar(10), right(newid(),10)) randomNumber FROM sysobjects) sample GROUP BY randomNumber SELECT...
Read More »

Some important user defined function in sql server

Creating user defined function for splitting string values with comma separated CREATE FUNCTION [fn_SplitComma](@String varchar(8000), @Delimiter char(1)) returns @temptable TABLE (items varchar(8000)) as begin         declare @idx int         declare @slice varchar(8000)              select @idx = 1                 if len(@String)<1 font="" is="" nbsp="" null="" or=""...
Read More »

Using sql server search column name in all tables within a database

Search column name in all tables in complete database SELECT  t.name AS table_name, SCHEMA_NAME(schema_id) AS schema_name, c.name AS column_name FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID WHERE c.name LIKE 'ID%' ORDER BY schema_name, table_nam...
Read More »

Using sql server deleting duplicate rows from a table

--Deleting duplicates rows from a table in different way-- select * from t1 insert into t1 values (1, 1, 'data value one') insert into t1 values (1, 1, 'data value one') insert into t1 values (1, 2, 'data value two') -- Query 1 using Group By and Having -- SELECT col1, col2, count(*) FROM t1 GROUP BY col1, col2 HAVING count(*) > 1 -- Query 2 using common type expression CTE With temptable as    (   Select ROW_NUMBER() over (PARTITION BY col1, col2 ORDER BY col1)...
Read More »

Deleting whole data from database using sql server query

--***************  DELETE ALL DATA FROM DATABASE  ***************-- EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' GO EXEC sp_MSForEachTable '  IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1   DELETE FROM ?  else    TRUNCATE TABLE ? ' GO EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL' GO EXEC sp_MSForEachTable '  IF OBJECTPROPERTY(object_id(''?''), ''TableHasIdentity'') = 1  DBCC CHECKIDENT (''?'',...
Read More »

Handling deadlock conditions in sql server

First create database structure: Run the following complete query: --- db script start from here --- utm_source=dailynewsletter&utm_medium=email&utm_content=headline&utm_campaign=20140325 USE [master] GO CREATE DATABASE TestDB  ON PRIMARY  ( NAME = N'TestDB', FILENAME = N'E:\MSSQL\TestDB.mdf' , SIZE = 10240KB , MAXSIZE = UNLIMITED,  FILEGROWTH = 1024KB )  LOG ON  ( NAME = N'TestDB_log', FILENAME = N'E:\MSSQL\TestDB_log.ldf' , SIZE = 1024KB , MAXSIZE...
Read More »

Using cursor in sql server

This cursor will return all the stored procedures within a database DECLARE @procName VARCHAR(100) DECLARE @getprocName CURSOR SET @getprocName = CURSOR FOR SELECT s.name FROM sysobjects s WHERE type = 'P' OPEN @getprocName FETCH NEXT FROM @getprocName INTO @procName WHILE @@FETCH_STATUS = 0 BEGIN EXEC sp_HelpText @procName FETCH NEXT FROM @getprocName INTO @procName END CLOSE @getprocName DEALLOCATE @getprocNa...
Read More »

Creating temporary table in sql server

Here is the use of temporary table inside a stored procedure:  create procedure [dbo].[usp_CreatingTemporaryTable] as  begin --creating temporary table here create table #temp ( name varchar(50), address varchar(150) ) --select * from yourtablename insert into #temp select column1, column2 from yourtablename  select distinct column1 from #temp select * from #temp end --Finally drop temporary table drop table #te...
Read More »

Using char varchar in sql server

Char Vs Varchar Few example showing the difference between char and varchar: DECLARE @CharName Char(30) = 'Yogesh Chandra Upreti', @VarCharName VarChar(30) = 'Yogesh Chandra Upreti' SELECT DATALENGTH(@CharName) CharSpaceUsed,DATALENGTH(@VarCharName) VarCharSpaceUsed DECLARE @FirstName Char(20) = 'YogeshChandra', @LastName Char(20) = 'Upreti'  IF @FirstName + ' ' + @LastName = 'YogeshChandra Upreti'   PRINT 'I was Expecting' ELSE   PRINT...
Read More »

SQL SERVER Add common column to each tables in sql server

JUST WRITE THIS QUERY : print 'Add  Column for each user table' EXEC sp_msforeachtable  ' ---- add column exec(''  declare @tableName as nvarchar(max)  set @tableName = ''''_?_''''  IF (CHARINDEX(''''aspnet_'''',@tableName) = 0) -- no aspnet membership  BEGIN  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''''?'''') AND type in (N''''U'''')  AND name = (''''YOUR_TABLE_NAME_HERE'''')  )  BEGIN  IF...
Read More »

Tuesday, May 20, 2014

SQL Server Transaction Retry Logic for failed transactions

Implementing SQL Server Transaction Retry Logic for failed transactions Problem Under heavy contention your transactions could be the victim of a deadlock and therefore be rolled back. In this tip I will show you how to implement retry logic to re-run the failed transaction. Solution We all know that every RDBMS system has to guarantee the ACID principle (Atomicity,...
Read More »

My Blog List