Wednesday, August 23, 2017

How to check which query take time on server

This is the following query which tell how much time it will take on server the status field, if it show running then its fine and if it show suspended its means :

The session is waiting for an event, such as I/O, to complete.
A query gets suspended when it is requesting access to a resource that is currently not available. This can be a logical resource like a locked row or a physical resource like a memory data page. The query starts running again, once the resource becomes available.
High disk IO means that a lot of data pages need to be accessed to fulfill the request

SELECT sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext

Thursday, August 10, 2017

Merge two rows into one row SQL

I have a requirement in which i need to show two column in one row .The simplest way to merge rows is with an aggregate function such as MIN/MAX. These functions will ignore nulls.

Before
EmployeeNo  Column1   Column2
================================
1           |  NULL       | Column2
1           |  Column1    |   NULL 

Sql
SELECT
  EmployeeNo,
  MAX(Column1) AS Column1,
  MAX(Column2) AS Column2
FROM MyTable
GROUP BY EmployeeNo

After Result
EmployeeNo  Column1   Column2
================================
1          | Column1  |   Column2
PS :
If you have a sub query to get a columns from other table it will not work in this you need to make a Derived Tables or you can use left join 
Reference:
https://dba.stackexchange.com/questions/67831/want-to-combine-two-rows-into-one-row

Thursday, August 3, 2017

selected column from DataTable and Rename the columns

If you want to show a selected column for DataTable not the whole dataset you can use this

VB.net
Dim selectedColumns() As String = {"Region_Name", "Country_Name"}
 Dim dt As DataTable = New DataView(dt_country).ToTable(False, selectedColumns) 'dt_country is a DataTable which I fetched from database


C#

string[] selectedColumns = {
"Region_Name",
"Country_Name"
};
DataTable dt = new DataView(dt_country).ToTable(false, selectedColumns); / dt_country is a DataTable which I fetched from database
     
   
I also want to update the column name

 dt.Columns("Region_Name").ColumnName = "Region Name"
 dt.Columns("Country_Name").ColumnName = "Country Name"

or if you want change the order of the column you can use do this
            dt.Columns("SMS API").SetOrdinal(0)
            dt.Columns("Region Name").SetOrdinal(1)
            dt.Columns("Country Name").SetOrdinal(2)


 Then pass in the grid this DataTable

Wednesday, August 2, 2017

Export Excel option in Update Panel.(Throwing Excpetion)


I am using the same code as I discuss earlier here http://engineerhacks.blogspot.ae/2017/07/converting-gridview-in-to-excel-or.html
 but this code is throwing exception every time "Unable to evaluate expression". I wondering
how it is possible that this code is running on the other pages and in this page is throwing exception when I cross check with other page I saw
that in my current page some one put Update Panel....
So if you are in same condition just put after the ContentTemplate end tag

<Triggers>
        <asp:PostBackTrigger ControlID="btnExcel" />
    </Triggers>

btnExcel is my button id when i click to generate the excel