Blog

IonicCourseCode1

declare var $:any;




function onClickButton(){

        let url = 'http://transportapi.com/v3/uk/train/station/exc/live.json?'

            +'app_id=*****&'

            +'app_key=******************';
//get your own app_id and app_key from http://transportapi.com/v3/uk

        $.get(url).then(

            (data:any)=>{

                var x = data;

                let element = document.getElementById("statusText");

                if(element != null){

                    element.innerHTML = '<ul>';

    

                    for(var i=0;i<data.departures.all.length;++i){

                        let itm = data.departures.all[i];

                        let itmString = '<li>Plat ' + itm.platform

                            + ' Time ' + itm.expected_departure_time

                            + ' To ' + itm.destination_name + '</li>';

                        element.innerHTML += itmString;

                    }

                    element.innerHTML += '<ul>';

                }

                

            }

        );  

}

//(see c:\users\IonicNinja\Documents\TestTrainTimetable\index.ts)

sp_who you can filter

SELECT spid,
 sp.[status],
 loginame [Login],
 hostname, 
 blocked BlkBy,
 sd.name DBName, 
 cmd Command,
 cpu CPUTime,
 physical_io DiskIO,
 last_batch LastBatch,
 [program_name] ProgramName 
FROM master.dbo.sysprocesses sp 
JOIN master.dbo.sysdatabases sd ON sp.dbid = sd.dbid
where hostname='BOARDROOM'
ORDER BY spid 


Get size of files using xp_cmdshell

Declare @Dir VARCHAR(256)
DECLARE @CMD VARCHAR(256)
SET @Dir = 'C:\myImageFolder\'
SET @CMD = 'DIR "'+@DIR+'" /A /S'

drop table #tmp 
 
 declare @media nvarchar(2048), @fn nvarchar(2048)
 DECLARE @CMD VARCHAR(256)

set @media = 'D:\Filestore\media'

SET @CMD = 'DIR "'+@media +'" /-C /S'

CREATE TABLE #tmp 
 (returnval NVARCHAR(500), rownum INT IDENTITY(1,1))

-- Populate Temp Table with the contents of the outfiles directory
 INSERT #tmp EXEC master..xp_cmdshell @cmd

alter table #tmp add filename nvarchar(255)
 alter table #tmp add size int
 alter table #tmp add DocumentId int

select convert(int,substring(returnval, 27,9)) size, substring(returnval, 37,999) name, * from #tmp
 where returnval like '__/__/____ __:__ %'
 --e.g. '17/11/2017 11:40 2645779 2017-11-17T114021.769Z001214 Band 4 Business Administrator Job Description_SM.pdf'

update #tmp set filename = substring(returnval, 37,999)
 , size = convert(int,substring(returnval, 27,9))
 where returnval like '__/__/____ __:__ %'

update #tmp set documentid=d.documentid from
 #tmp, documents d where d.documentpath like '%' + #tmp.filename


 select 
 filename, 'D:\MyStaffFile_Files\media\' + d.documentpath, s.assignmentid, size, d.documentid, createdby, createddate
 
 from #tmp inner join documents d on #tmp.documentid = d.documentid
 inner join sections s on d.sectionid=s.sectionid
 
 where size>1000000
 order by CreatedBy, CreatedDate, Size desc --documentid desc

 

What columns are used? Quick analysis of legacy SQL table

Got a table with lots of columns? Use this SQL to identify which have values.

My table name is manTransaction and I’m only interested in those of type = 2

declare @tn nvarchar(200)
declare @whereclause nvarchar(1024)
set @tn='manTransactionDetail' --table
set @whereclause = 'TransactionID in (select TransactionID from manTransaction where TransactionTypeID=8)' --particular records
declare @col nvarchar(200)
declare @typ int
declare @sql0 nvarchar(2048)
declare @sql nvarchar(2048)

set @sql0 = 'with a(v) as (select __COLNAME__ from __TABLENAME__ where __WHERECLAUSE__ )
 , d(v) as (select v from a group by v)
 insert into #ColumnStatus(ColName, DistinctValues) select ''__COLNAME__'', COUNT(*) from d'
set @sql0 = REPLACE(@sql0, '__TABLENAME__',@tn)
set @sql0 = REPLACE(@sql0, '__WHERECLAUSE__',@whereclause)

if OBJECT_ID('tempdb..#ColumnStatus') is not null drop table #ColumnStatus
create table #ColumnStatus(ColName nvarchar(200), DistinctValues int)
declare c cursor for
select name, system_type_id from sys.columns where object_id=object_id(@tn)
open c
fetch next from c into @col,@typ
set @sql = REPLACE (@sql0, '__COLNAME__', @col)
print @sql
exec(@sql) 
while @@FETCH_STATUS=0
begin
 fetch next from c into @col,@typ
 set @sql = REPLACE (@sql0, '__COLNAME__', @col)
 exec(@sql) 
end
close c
deallocate c
select ColName + ',' from #ColumnStatus where DistinctValues > 1

--select * from #ColumnStatus





 

 

MVC / Razor doesn’t post values from disabled html elements back to controller

In a Razor MVC form, if you disable a html element, the value won’t be posted back to the controller.

@Html.EditorFor(model => model.Person.Surname, new { htmlAttributes = new { @class = "form-control variable" } })

You might do

if (...) {
 $(".variable").attr('disabled', 'disabled'); //doesn't work
 }

Better, do this…

if (...) {
 $(".disable")
 .prop("readonly", true)
 .css('color', "#c0c0c0");
 //https://stackoverflow.com/a/2724483
 }

https://stackoverflow.com/a/2724483

Build Date for MVC or Visual Studio project

Shamelessly stolen from here

Project, create new folder Resources

Project, Properties: Build Events, Pre-Build event command line

echo %date% %time% > "$(ProjectDir)\Resources\BuildDate.txt"

Build your Project

Project, Resources: Create Default Resources File

..Add Resource, Existing File, …\Resources\BuildDate.txt

To read the build date, e.g. in a controller

protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state)
 {

ViewBag.BuildDate = Properties.Resources.BuildDate;

...

 

Azure DNS / HTTPS and FastHosts email

How to configure a domain name, https and email for your Azure web app

I tried (oh how i tried) to use a LetsEncrypt HTTPS certificate for my Azure-hosted web site by following Troy Hunt’s blog post

(all right, I gave it an hour…)

Then I looked more closely at the Azure portal and it turns out you can buy your domain and HTTPS certificate from there. It originally comes from GoDaddy and my legstat.org.uk domain plus certificate cost £50 ish (and a lot of clicking around).

How to buy a Top Level Domain from Microsoft Azure?

You have to go to Basic (more expensive) hosting to support https. On the plus side, now I can run my new site thru asafaweb without feeling bad…

And I also get an “Advanced DNS” page.

Now I want to send email from legstat.org.uk so I signed up for a fasthosts email account (£3/month).

To use this I have to “prove ownership”. This means 2 things

* Add some MX records
* Add a TXT record

I had a bit of trouble with the advanced DNS settings, but what they finally looked like was..

sql isnumeric(2,000.00)=1 but isfloat(2,000.00) errors Error converting data type varchar to float.

select isnumeric('20,000.000') returns 1
select convert(float, '20,000.000') raises an error Error converting data type varchar to float.

You can’t write a isfloat() function, because try-catch is a side effect.

Here’s a snippet to walk thru your data to find duff values

declare c cursor for
select chillirecipedetailid, amount_or from tblAbRecipes
open c
declare @id int, @val nvarchar(100)
fetch next from c into @id, @val
while @@FETCH_STATUS=0
begin
 begin try
 declare @f float
 set @f = convert(float,@val)
 end try
 begin catch
 print 'duff value at ' + convert(nvarchar(20),@id) + ': ' + @val
 end catch;
 fetch next from c into @id, @val
end;
close c
deallocate c

select isnumeric('20,000.000')
select convert(float, '20,000.000')

 

How to check a file exists in transact sql using xp_FileExists

 

exec sp_configure 'show advanced options',1
go
reconfigure;
go
exec sp_configure 'xp_cmdshell', 1;
go
reconfigure;
go

drop function sp_FileExists

if object_id('sp_FileExists') is null
begin
 declare @sql nvarchar(2000)
 set @sql='
 create function dbo.sp_FileExists(@fn nvarchar(1000)) returns bit as
 begin
 declare @result int
 exec master.dbo.xp_fileexist @fn, @result output
 if @result = 1 return 1
 return 0 
 end
 '
 exec @sql
end

select DocumentId, 'd:\MyStaffFile_Files\media\' + DocumentPath 
,case when DocumentPath is null then 'no path'
 when dbo.sp_FileExists('d:\MyStaffFile_Files\media\' + DocumentPath) = 1 then 'exists'
 else 'does not exist'
 end
from Documents