Friday, August 19, 2011

Getting distinct column with multiple columns

if we have table id, name, age


the requirements is id has different and redundatnt data


We want the distinct column with corresponding rows. this is highly useful


SELECT id,
role_id_fk,
privilege_id_fk
FROM (SELECT id,
role_id_fk,
privilege_id_fk,
Row_number() OVER(PARTITION BY role_id_fk ORDER BY id) rn
FROM tbl_roles_privileges) t
WHERE rn = 1

Thursday, July 14, 2011

ASP.NET

how to access the usercontrol's property or method()


SearchUsercontrol is a usercontrol. The requirement is to access a usercontrol’s method or property from the parent aspx page.

For normal aspx pages (with out master pages)

Usercontrol1.Method() or userconrol.GetID

For normal aspx pages (with master page)

SearchUsercontrol _displayResultsUserControl = _contentPlaceHolder.FindControl("SearchResults") as SearchUsercontrol;

_displayResultsUserControl.methpdname()
_displayResultsUserControl.propertyName;



Similarly to access the a ASPX page’s method or property from the usercontrol


SearchResults myparentPage = (SearchResults)this.Page;
myparentPage.BindRadGrid();


Here SearchResults is the Usercontrool

Wednesday, March 25, 2009

asp.net

1. Difference between gridview, datalist, repeator and details view and form view
GridView - Read/Edit

DataList - Read/Edit

Repeater - Read Only

DetailsView - Read/Edit/Create

FormView - Read/Edit/Create











 RepeaterDataListGridView
Table layoutnny
Flow layoutyyn
Column layoutnyn
Style propertiesnyy
TemplatesyyColumns/optional
Select/Edit/Deletenyy
Sortnny
Pagingnny


2. Error: A name was started with an invalid character. Error processing resource 'http://localhost/IssueTracker/Default.aspx'. Line...
<%@ Page Language="vb" AutoEventWireup="false" Inherits="ASPNET.StarterKit.IssueTracker._Default" CodeFile="Default.aspx....


SolutionC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis -i
Start installing ASP.NET (2.0.50727).

Tuesday, March 10, 2009

DotNet Web Info

1. Install Templates in Visual studio 2005 or 2009

copy the zip (for e.g Login.zip) file to the

D:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\Web\CSharp\1033
[if the project type is web and language is c#]

D:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\Web\VisualBasic\1033
[if the project type is web and language is VB]

2. close all the instances of VS 2005 or 2008
3. type the command devenv /installvstemplates in the run command
4. Wait for auto close of the devenv.exe in process windows task manager
5. Invoke the VS 2008 and the see your template in the my templates


1. DateTime Format

DateTime t1 = DateTime.Today;
MessageBox.Show(String.Format("{0:dd}", t) + " th" + String.Format("{0:MMM}", t) + ", " + String.Format("{0:yyyy}", t));
MessageBox.Show(String.Format("{0:MM/dd/yyyy}", t1));

Tuesday, October 21, 2008

Sql Tips

1. Getting the table and column information through information_schema

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE Column_NAME = 'AddressId'

2. Creating a table with primary key and foreign key using sql commands


create Table License
(
LicenseId bigint,
LicenseTypeId bigint,
PaymentId bigint

primary key(LicenseId),
foreign key (LicenseTypeId) references [License Types](LicenseTypeId),
foreign key (PaymentId) references [Payment](PaymentId)
);

3. Difference between IsNulll and COALESCE
a. ISNULL and COALESCE though equivalent, can behave differently.
b. An expression involving ISNULL with non-null parameters is considered to be NOT NULL,
c. while expressions involving COALESCE with non-null parameters is considered to be NULL.
1.Examples

SELECT ISNULL(@foo, '123456789')
outputs to -
-----
12345 [truncates some values in Isnull]

SELECT COALESCE(@foo, '123456789')

outputs to
---------
123456789 [no truncation]


2nd Exaample
coalesce checks the three columns for null but Isnull checks only one column
e.g coalesce(col1, col2, col3)
Isnull(col1) is only allowed



4. to find duplicate values in sql

select max(id), max(name) from testing group by name having count(*) >1


5. Primary Key and Unique Key difference
Primary key does not allow null values but unique allows null values



6. Deleting the Duplicate values in Sql Tables

Create table #temp
(
id int,
[name] varchar(50)
)

insert into #temp
select max(id), max(name) from testing group by name having count(*) >1
select * from #temp

delete testing where id in (select Id from #temp)


7. Passing array values to SP
=============================
alter proc Array2sp
(
@names varchar(100)
)
as
declare @namesxml xml
DECLARE @XMLDocPointer INT

set @namesxml = '' + replace(@names, ',', '') + '';
--select namesxml.item.value('valuess', 'int') as value1 from @namesxml.nodes('xml/root')
--as namesxml(item)

EXEC sp_xml_preparedocument @XMLDocPointer OUTPUT, @namesxml
--drop table temp
--select @namesxml
--create table temp(valuess varchar(20))
--insert into temp(valuess)

SELECT *
FROM OPENXML(@XMLDocPointer,'/xml/root',2)
with (valuess varchar(50) 'valuess')

--select * from temp

EXEC sp_xml_removedocument @XMLDocPointer


execution
--exec Array2sp '1,2,3,4,5'

8. Get the number of rows in a single column and single row
use
"for xml path('')"

e.g. SELECT DocumentTypeDescription + ' / '
FROM OutPatientDocumentTypes
ORDER BY OutpatientDocumentId
FOR XML PATH('')


9. using xquery to get the results from the xml column of the table
use event
declare @docHandle xml
declare @handle int
set @docHandle=(select logxml from Import_Log where importid=142)
SELECT xref.value('@Importerror', 'varchar(100)') as ImportError, xref.value('@email', 'varchar(100)') as email FROM @docHandle.nodes('//logging/emaillistitems/listitem') R(xref)

where R is the Table and xref is the column Name

10. Comparing two xml columns in sql table - this is for multiple comparison
DECLARE @doc xml
DECLARE @doc1 xml
set @doc=(select [xmlCol1] from [xml1])
set @doc1=(select [xmlCol2] from [xml1])

Select
case when T1.c1.value('Id[1]', 'varchar(50)') = T2.c2.value('Id[1]', 'varchar(50)') then 1 else 0 end as matched,
case when T1.c1.value('Id[1]', 'varchar(50)') <> T2.c2.value('Id[1]', 'varchar(50)') then 1 else 0 end as nonmatched
From @doc.nodes('Categories/Category') T1(c1), @doc1.nodes('CategoryLevel/Category') T2(c2)

below sample is for single comparison [two xml columns in the sql table]

Select
-- case when xmlCol1.value('Categories[1]/Category[1]/CategoryName[1]', 'varchar(50)') = xmlCol2.value('CategoryLevel[1]/Category[1]/CategoryName[1]', 'varchar(50)') then 1 else 0 end as counts
-- From xml1

11. Newer way for comparing two xml columns in a sql table

DECLARE @doc xml
DECLARE @doc1 xml
set @doc=(select [xmlCol1] from [xml1])
set @doc1=(select [xmlCol2] from [xml1])

select (select count(*) as matchedwhere from @doc.nodes('Categories/Category') T1(c1) where T1.c1.value('Id[1]', 'varchar(20)') in
(select T2.c2.value('Id[1]', 'varchar(20)') from @doc1.nodes('CategoryLevel/Category') T2(c2))) as matchRow, count(*) as nomatched from @doc.nodes('Categories/Category') T1(c1)
where T1.c1.value('Id[1]', 'varchar(20)') not in (select T2.c2.value('Id[1]', 'varchar(20)') from @doc1.nodes('CategoryLevel/Category') T2(c2))

11. Encrypt text in sql



declare @hash varbinary(255)
set @hash=pwdencrypt('Dnn2O!!')
select @hash

SELECT pwdcompare ('Dnn2O!!', @hash)
If the value is one[1] then password are same


12. Get the values both null and non values for all columns



USE [mip_2.0.2]
GO
/****** Object: StoredProcedure [dbo].[usp_GetSearchUsers] Script Date: 07/26/2011 12:35:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_GetSearchUsers](@userName varchar(50), @firstName varchar(50), @lastName varchar(50), @city varchar(50), @ddlEmployementStatus varchar(50), @empType varchar(50), @doj varchar(50), @dor varchar(50), @attroneyLawFirm varchar(50))
as
SET ANSI_NULLS OFF
select * from tbl_user where
coalesce(user_name, 'null')=isnull(@userName, coalesce(user_name, 'null'))
and coalesce(first_name, 'null')=isnull(@firstName, coalesce(first_name, 'null'))
and coalesce(last_name, 'null')=isnull(@lastName, coalesce(last_name, 'null'))
and coalesce(city, 'null')=isnull(@city, coalesce(city, 'null'))
and coalesce(employeestatus_id_fk, 0)=isnull(@ddlEmployementStatus, coalesce(employeestatus_id_fk, 0))
and coalesce(employeetype_id_fk, 0)=isnull(@empType, coalesce(employeetype_id_fk, 0))
and coalesce(date_of_join, '1900-01-01')=isnull(@doj, coalesce(date_of_join, '1900-01-01'))
and coalesce(date_of_relieve, '1900-01-01')=isnull(@dor, coalesce(date_of_relieve, '1900-01-01'))
and coalesce(attorney_lawfirm_id_fk, 0)=isnull(@attroneyLawFirm, coalesce(attorney_lawfirm_id_fk, 0))