Navigation

Search

Categories

On this page

Getting a Record Count from a SqlDataSource
Getting a record count from a Dataset
Find Maximum Value in Each Row Using UNPIVOT
Drop and Recreate Stored Procedures in T-SQL
Formatting Dates in T-SQL

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 378
This Year: 6
This Month: 1
This Week: 0
Comments: 17

Sign In
Pick a theme:

# Wednesday, August 18, 2010
Wednesday, August 18, 2010 3:11:07 PM (GMT Daylight Time, UTC+01:00) ( ASP.NET | SQL )

 

Private Sub OnSelectedHandler(ByVal source As Object, ByVal e As SqlDataSourceStatusEventArgs)
        Dim cmd As IDbCommand
        cmd = e.Command
        Dim recordCount As Integer = e.AffectedRows()
        lblRecordCount.Text = "Showing " & recordCount & " IMFs"
End Sub


<asp:SqlDataSource ID="dsMyIMFs" runat="server" ConnectionString="<%$ ConnectionStrings:Tearsheet_ConString %>" 
SelectCommand="p_MyIMFs" SelectCommandType="StoredProcedure" OnSelected="OnSelectedHandler">
</asp:SqlDataSource>

<asp:Label ID="lblRecordCount" runat="server" />
Comments [0] | | # 
Wednesday, August 18, 2010 2:59:34 PM (GMT Daylight Time, UTC+01:00) ( )


Dim MyConnection As SqlConnection
        
 MyConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Tearsheet_ConString").ConnectionString)
Dim MyAdapter As New SqlDataAdapter(SqlText, MyConnection)
 
 Dim DS As DataSet
Dim cmd As SqlCommand
        
 SqlText += " SELECT * FROM Table A"
       
              
 cmd = New SqlCommand(SqlText)
MyConnection.Open()
cmd.Connection = MyConnection

DS = New DataSet()
MyAdapter.Fill(DS, "SearchResults")
                           
GridView1.DataSource = DS.Tables("SearchResults").DefaultView
lblRecordCount.Text = "Your search returned " & DS.Tables("SearchResults").Rows.Count & " records"
Or
Dim intRecordCount As Integer = ds.Tables(0).Rows.Count()
lblRecordCount.Text = "Your search returned " & intRecordCount & " records"

GridView1.DataBind()
Comments [0] | | # 
# Monday, August 02, 2010
Monday, August 02, 2010 4:31:29 PM (GMT Daylight Time, UTC+01:00) ( SQL )

 

DECLARE @t table (id int PRIMARY KEY, col1 int, col2 int, col3 int)

-- SAMPLE DATA
INSERT INTO @t SELECT 1, 45, 2, 14
INSERT INTO @t SELECT 2, 8, 1, 12
INSERT INTO @t SELECT 3, 21, 20, 8
INSERT INTO @t SELECT 4, 8, 5, 2
INSERT INTO @t SELECT 5, 23, 49, 7
INSERT INTO @t SELECT 6, 19, 7, 5
INSERT INTO @t SELECT 7, 7, 7, 2
INSERT INTO @t SELECT 8, 14, 17, 2
-- QUERY
SELECT id, MAX(col) AS maxValue
FROM
(  
    SELECT id, col FROM @t  
    UNPIVOT  
        (col FOR ListofColumns IN (col1,col2,col3))   
    AS unpivott) AS p
    GROUP BY id 
Comments [0] | | # 
Monday, August 02, 2010 4:30:08 PM (GMT Daylight Time, UTC+01:00) ( SQL )


The syntax shown below will drop a stored procedure if it exists and recreate it.

IF OBJECTPROPERTY(object_id('dbo.YourStoredProcName'), N'IsProcedure') = 1
DROP PROCEDURE [dbo].[YourStoredProcName]
GO
CREATE PROCEDURE dbo.YourStoredProcName
AS
-- Logic Comes Here
GO
Comments [0] | | # 
Monday, August 02, 2010 4:28:12 PM (GMT Daylight Time, UTC+01:00) ( SQL )

 

DECLARE @CurrentDate DateTime
SET @CurrentDate = '2010-03-27 14:15:15.390'

--Print Date in MM/YYYY format

SELECT DATENAME(m, @CurrentDate) 
+ ' ' + CONVERT(varchar(4), DATEPART(year, @CurrentDate))
as 'MM/YYYY'

--Print Date in MM-DD-YY format

SELECT CONVERT(varchar(10), @CurrentDate, 110)
as 'MM-DD-YY'
Comments [0] | | #