Tuesday, 29 January 2013

sql concatenate string group by

Concatenate many rows into a single text string:


CREATE TABLE #YourTable ([ID] INT, [Name] CHAR(1), [Value] INT)

INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (1,'A',4)
INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (1,'B',8)
INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (2,'C',9)

SELECT
  [ID],
  STUFF((
    SELECT ', ' + [Name] + ':' + CAST([Value] AS VARCHAR(MAX))
    FROM #YourTable
    WHERE (ID = Results.ID)
    FOR XML PATH (''))
  ,1,2,'') AS NameValues
FROM #YourTable Results
GROUP BY ID

DROP TABLE #YourTable




Turn textbox into label


1. Change the readonly property of textbox to TRUE

2. Change the back color of textbox to transparent.

3. Change the border of the text box to none.

 This will feel TextBox as a Label

Friday, 25 January 2013

Create Linked Server SQL Server 2008


Creating a linked server in SQL Server 2008 is a great way to run CRUD statements against a completely different remote server. This method relies on Windows Authentication.
This is only one way to do it. There are others. And, if you are going to run SQL statements against a linked server from a web app, you need to have the permissions set up properly. From the article “How do I… Query foreign data using SQL Server’s linked servers?” by Susan Harkins:








The biggest catch to all this simplicity is, as always, security. If you’re working with Windows Authentication, the system accommodates well. If the user has the appropriate permissions for the servers and data sources, linked queries will work.
If you have users outside the Windows Authentication environment, you can use a linked login. Create a standard login and assign the appropriate permissions on the remote server.
Setting It Up
1.    Go to “Server Objects” of the database server where the linked server will be added and right click on “Linked Servers”. Select “Add New Linked Server”.
2.    In the “General” tab, add a name for the new linked server in the “Linked Server” field.
3.    Select “Other data source”and for “Provider” select “Microsoft OLE DB for SQL Server”
4.    For “Product Name” type in “SQLOLEDB”
5.    In the “Data Source” field, enter the IP address of the server to be linked
6.    “Catalog” is the name of the database on the linked server and is optional
7.    Go to the “Security” tab and select “Be made using this security context”. Type in the remote login and credentials
Running Queries
To query against this server user the syntax:
SELECT * FROM [MY_LINKED_SERVER].[database_name].[dbo].[table_name]
Or, for a join, use linked table with an alias (example):

SELECT * FROM Table
    RIGHT OUTER JOIN

    [MY_LINKED_SERVER].[database_name].[dbo].[table_name] LinkedTable
    ON Table.column = LinkedTable.column


Reference:

Thursday, 24 January 2013

ContextSwitchDeadlock was detected


ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context 0x1b7370 to COM context 0x1b74e0 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

Solution:
 Debug -> Exceptions -> Managed Debug Assistants
 untick ContextSwitchDeadlock

Tuesday, 8 January 2013

Compare comma separated values with comma separated values in sql


CREATE FUNCTION [dbo].[Split](@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 or @String is null  return
   
        while @idx!= 0
        begin
                set @idx = charindex(@Delimiter,@String)
                if @idx!=0
                        set @slice = left(@String,@idx - 1)
                else
                        set @slice = @String
               
                if(len(@slice)>0)
                        insert into @temptable(Items) values(@slice)

                set @String = right(@String,len(@String) - @idx)
                if len(@String) = 0 break
        end
return
end
GO
--function end
GO

--now check below sample
--table variable to hold data
declare @table table
(
        id int,[key] varchar(100)
)
insert into @table
select 1,'session, state' union all
select 2,'a,b,c' union all
select 3,'hi, hello'

--parameter value
declare @data varchar(1000)
set @data='b,c'

--select query to find data
select distinct id
from @table
CROSS APPLY dbo.Split(strkeywords,',') AS AA
CROSS APPLY dbo.Split(@data,',') AS BB
WHERE AA.items=BB.items

Thursday, 3 January 2013

Export Data from Excel files with special characters


Dim grd1 As New GridView()
grd1.DataSource = dt ' dt is Data Table
grd1.DataBind()

Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment;filename=products.xls")
Response.Charset = ""

Response.ContentEncoding = Encoding.Unicode
Response.BinaryWrite(Encoding.Unicode.GetPreamble())

Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
grd1.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()