C# .NET Tips

1. Getting application running system’s domain name and host name

System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
System.Net.Dns.GetHostName();

 

 

Leave a Comment

log function in C using variable arguments…

void Log(TCHAR *szFmter, …)
{
if(NULL !=  pfLog)
{
wchar_t szBuff[MAX_LOG_BUFF_SIZE];

SYSTEMTIME  t;
GetLocalTime( &t);

_stprintf_s(szTime, _countof(szTime), _T(“%02d.%02d.%hd.%02d.%02d.%02d.%003d”), t.wDay, t.wMonth, t.wYear, t.wHour, t.wMinute, t.wSecond, t.wMilliseconds);
va_list args;
va_start( args, szFmter);
vswprintf_s(szBuff, szFmter, args );
va_end(args);

//writing the values in the log file
_fwprintf_p(pfLog, _T(“%s#%s\n”), szTime, szBuff);
fflush(pfLog);
}
}

Leave a Comment

ZSocket Library – a thin wrapper around BSD socket.

Leave a Comment

PUGI – A simple XML parser api from google labs…

usage example:
=============

#include “pugixml.hpp”

int _tmain(int argc, _TCHAR* argv[])
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(“temp.xml”);

for (pugi::xml_node user = doc.child(L”logins”).child(L”user”); user; user = user.next_sibling(L”user”))
{
//std::cout << “Tool ” << tool.attribute(“Filename”).value();
//std::cout << “: AllowRemote ” << tool.attribute(“AllowRemote”).as_bool();
//std::cout << “, Timeout ” << tool.attribute(“Timeout”).as_int();
//std::cout << “, Description ‘” << tool.child_value(“Description”) << “‘\n”;
std::wcout << L”\n” << user.child_value(L”name”) << L” [” << user.child_value(L”pwd”) << L”]”;
}

return 0;
}

————————————————————————————————————————————————

Following code will create xml nodes within node <logins></logins>

<user>

    <uname>username</uname>

    <pwd>48729038787AB789DE3FF</pwd>

    <display>First Name Last Name</display>

</user>

pugi::xml_node node_user = doc.child(L”logins”).append_child(L”user”);

if(is_admin == true){
node_user.append_attribute(L”is_admin”) = L”1″;
}

pugi::xml_node node_uname = node_user.append_child(L”uname”).append_child(pugi::node_pcdata);
node_uname.set_value(s_user_name);

pugi::xml_node node_pwd = node_user.append_child(L”pwd”).append_child(pugi::node_pcdata);
node_pwd.set_value(szHashPasswdHex);

pugi::xml_node node_display = node_user.append_child(L”display”).append_child(pugi::node_pcdata);
node_display.set_value(s_display_name);

————————————————————————————————————————————————

Leave a Comment

JavaScript Tips

Validating number (supports both integer and float type)
=============================================

value = jQuery.trim($(“#max_value”).val());

if(value * 1 != value ){
alert(“Maximum value should be a number.”);
}

Leave a Comment

PHP Tips

Log function
===========

function log_entry($log_str){
    $file = fopen("email_trigger.log", "a+");
    if($file)
    {
        fprintf($file, "\n[".date("Y-m-d H:i:s")."]".$log_str);
        fclose($file);
    }
}

SimpleXML – Removing node
==========================

$data='<data>
    <seg id="A1"/>
    <seg id="A5"/>
    <seg id="A12"/>
    <seg id="A29"/>
    <seg id="A30"/>
</data>';
$doc=new SimpleXMLElement($data);
foreach($doc->seg as $seg)
{
    if($seg['id'] == 'A12') {
        $dom=dom_import_simplexml($seg);
        $dom->parentNode->removeChild($dom);
    }
}
echo $doc->asXml();

Leave a Comment

MSSQL Tips

Listing all user created table names
============================

select name from dbname..sysobjects where xtype = ‘U’;

—————————————————————

USE YourDBName
SELECT * FROM sys.Tables

—————————————————————

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ‘BASE TABLE’

Listing index details of a table
======================

EXEC sp_helpindex ‘tablename’

Listing columns of a table
===================

1. select * from information_schema.columns where TABLE_NAME = ‘tablename’
2. exec sp_columns ‘tablename’


Grouping by week
=============

select DATEPART (wk, date_column) as WeekNum, count(*) as total
from table_name group by DATEPART (wk, date_column)

Above query gives results based on week numbers (1,2,3… 52)

Sample output
===========
1 – 30293
2 – 343423
3 – 897902



52 – 379829

select date_column – (DATEPART(DW, date_column) – 1) as week_start_date, count(*) as total
from table_name group by date_column – (DATEPART(DW, date_column) – 1)

Sample output
===========
2010-01-02 – 30293
2010-01-09 – 343423
2010-01-16 – 897902



2010-12-25 – 379829

NOTE:

=====

Below condition can be added to where clause if we want to omit results of current week, because current week has not yet completed.

Declare @todaydate smalldatetime = GetDate();

date_column – (DATEPART(DW,  date_column) – 1)  !=  convert(date, @todaydate- (DATEPART(DW,  @todaydate)- 1))

 

Leave a Comment

[PHP] ODBC db functions

if($type == “mysql”){
$dsn = “Driver={MySQL ODBC 3.51 Driver};Server={$host};Database={$dbname};Port={$port};”;
}
else if($type == “mssql”){
$dsn = “Driver={SQL Server};Server={$host};Database={$dbname};Port={$port};”;
}
else if($type == “oracle”){
$dsn = “Driver={Microsoft ODBC for Oracle};Server={$host};Database={$dbname};Port={$port};”;
}
else if($type == “postgresql”){
$dsn = “Driver={PostgreSQL};Server={$host};Database={$dbname};Port={$port};”;
}

$odbc_connection=odbc_connect($dsn,$user,$pw,SQL_CUR_USE_ODBC);
odbc_exec($odbc_connection, “SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED”);

$sql = ‘INSERT INTO table (col1, col2, col3…) values (?,?,?… ) ‘;

$query = odbc_prepare($odbc_connection, $sql);

$result = odbc_execute($query, $insert_col_values); //$insert_col_values is array of string for each ?,?,?… in previopus statement.

$sql = “select from table_name where condition”;

while ($row = odbc_fetch_array($rs)) {
……
……
……
}

Leave a Comment

[PHP] Convertin String to Time and incrementing date by one day…

Converting a date string to time object
===============================
$sUnixDateTime = strtotime($sDate)

Incrementing date by one day, result will be returned as a date object
========================================================
$sUnixDateTime = mktime(0, 0, 0, date(“m”, $sUnixDateTime), date(“d”, $sUnixDateTime)+1, date(“Y”, $sUnixDateTime));

Leave a Comment

Getting formatted date and time string in C Programming

wchar_t wszTime[101];
time_t t = time(NULL);
wcsftime(wszTime, sizeof(wszTime), _T(“%Y/%m/%d : %H:%M:%S”), localtime(&t));
wprintf(_T(“%s”), wszTime);

Leave a Comment

Older Posts »