Flooring And Truncating Decimals

April 16, 2014 | Posted in .Net
There may be legitimate circumstances where you want to store a specific decimal value and you don’t want SQL Server to round that precision off which would give you incorrect results when multiplying by large numbers.
Example 10.73085 stored in a SQL Server decimal 18,4 will round up and be stored as 10.7309. If like me you wanted to keep the 4 decimal place precision without round up there is a way.
(Math.Floor(value * 10000) / 10000)
Just specify a zero for each decimal place you wish to keep i.e. * 1000 for 3dp, * 100 for 2 dp and so on.
How To See Which Ports Are Listening

April 11, 2014 | Posted in Commant Prompt
netstat -an | find /i “listening”
Locating User In Active Directory Using System.DirectoryServices.DirectorySearcher

April 11, 2014 | Posted in .Net
DirectorySearcher.FindAll() is very slow. Instead search for the specific AD user, much faster:
Dim windowsLogin As String
Dim currentIdentity As System.Security.Principal.WindowsIdentity
Dim slashIndex As Integer = 0
Try
currentIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
slashIndex = currentIdentity.Name.IndexOf(“\”)
windowsLogin = currentIdentity.Name.Substring(slashIndex + 1)
Catch
Throw
End Try
Dim searcher As New DirectorySearcher(“”)
Dim result As SearchResult = Nothing
With searcher
.Filter = String.Format(“(&(objectCategory=person)(SAMAccountName={0}))”, windowsLogin)
result = .FindOne()
End With
If ((result IsNot Nothing) AndAlso
(result.Properties.Item(“SAMAccountName”) IsNot Nothing) AndAlso
(result.Properties.Item(“SAMAccountName”).Item(0) IsNot Nothing) AndAlso
(CStr(result.Properties.Item(“SAMAccountName”).Item(0)).ToUpper() = windowsLogin.ToUpper())) Then
MessageBox.Show(String.Format(“User {0}”, windowsLogin) + ” found”)
End If
Why Can’t I See Queued Service Broker Messages In Sys.Transmission_Queue?

April 11, 2014 | Posted in SQL Server
You need to be in the db_owner() role. The only way around this is to create a stored procedure to execute as dbo which will return this information:
CREATE VIEW sys.transmission_queue AS
SELECT conversation_handle = S.handle,
to_service_name = Q.tosvc,
to_broker_instance = Q.tobrkrinst,
from_service_name = Q.fromsvc,
service_contract_name = Q.svccontr,
enqueue_time = Q.enqtime,
message_sequence_number = Q.msgseqnum,
message_type_name = Q.msgtype,
is_conversation_error = sysconv(bit, Q.status & 2),
is_end_of_dialog = sysconv(bit, Q.status & 4),
message_body = Q.msgbody,
transmission_status = GET_TRANSMISSION_STATUS (S.handle),
priority = R.priority
FROM sys.sysxmitqueue Q
INNER JOIN sys.sysdesend S WITH (NOLOCK) on Q.dlgid = S.diagid AND Q.finitiator = S.initiator
INNER JOIN sys.sysdercv R WITH (NOLOCK) ON Q.dlgid = R.diagid AND Q.finitiator = R.initiator
WHERE is_member(‘db_owner’) = 1
Visual Studio Treat Warnings As Errors

April 9, 2014 | Posted in .Net
I spent a considerable amount of time trying to find all of the VB.Net and C# check id’s so that I could treat a select few warnings as errors. The truth of it is, that the Visual Basic compiler is a lot less picky than the C# compiler. As an example, the default behaviour of the C# compiler is to tell you that a function has no return value (as an error), whereas in the land of Visual Basic you need to specify this under VB Project -> Compile -> Warning Configurations (Condition “Unused local variable” = “Error”). This instructs the Visual Basic compiler to treat these warnings as errors and as you would expect, they then show up in your errors list.
There seem to be very few options for Visual Basic projects to achieve this for many of the desired warnings as errors, but the following should get you started:
http://msdn.microsoft.com/en-us/library/3y20cc1z.aspx
I’ve also managed to find the C# equivilent URL which lists the check id’s here:
http://msdn.microsoft.com/en-us/library/dd380629.aspx
NOTE: If you exceed the maximum number of warnings in Visual Studio and you are treating them as errors, the errors will not show if they are out of scope and will effectively be hidden!! Watch out for this.