RSS

Monthly Archives: June 2012

Access Skype Functionality in Web Site

The Skype protocol allows users to deal with Skype related activities. The only condition is to have installed Skype  in client side. Getting a call, sending a IM, sending a voicemail and having a conference call  are still possible when the Skype username is known.

The following examples use ‘echo123’ demo user provided by Skype. Replace it with your friend’s Skype username.

Call a Skype user

call Skype demo user

<a href="skype:echo123?call">call Skype demo user</a>

Send a Skype IM message

Chat with Skype demo user

<a href="skype:echo123?chat">Chat with Skype demo user</a>

Send a voicemail

Send voicemail to demo user

<a href="skype:echo123?voicemail">Send voicemail to demo user</a>

Create a Skype conference call

conference call

<a href="skype:echo123;ANOTHER_USER?call">conference call</a>
Advertisements
 
Leave a comment

Posted by on June 29, 2012 in HTML, Miscellaneous

 

Tags:

How to Display Current Date in your program

Find it difficult to get the current date and time? Then read on, I this tutorial I demonstrate getting current date and time in different programming languages.

Java

Using Calendar

output: 28/6/2012

  Calendar cal = new GregorianCalendar();
  int month = cal.get(Calendar.MONTH);
  int year = cal.get(Calendar.YEAR);
  int day = cal.get(Calendar.DAY_OF_MONTH);
  System.out.println("Current date : " + day + "/" + (month + 1) + "/" + year);

Using SimpleDateFormat

output: 28/06/2012

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String sDate= sdf.format(date);

C#
Using DateTime format

output: Jun Fri 27 11:41 2012

DateTime time = DateTime.Now;              // Use current time
string format = "MMM ddd d HH:mm yyyy";    // Use this format
Console.WriteLine(time.ToString(format));

Some other patterns for DateTime
MMM : display three-letter month
ddd : display three-letter day of the WEEK
d : display day of the MONTH
HH : display two-digit hours on 24-hour scale
mm : display two-digit minutes
yyyy : display four-digit year

Single-letter format

output :
6/28/2012
Thursday, June 28, 2012
Thursday, June 28, 2012 11:36 AM
Thursday, June 28, 2012 11:36:21 AM
6/28/2012 11:36 AM
6/28/2012 11:36:21 AM
June 28
June 28
2012-06-28T11:36:21.8863219+05:30
2012-06-28T11:36:21.8863219+05:30
2012-06-28T11:36:21
11:36 AM
11:36:21 AM
2012-06-28 11:36:21Z
Thursday, June 28, 2012 6:06:21 AM
June, 2012
June, 2012

DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("d"));
Console.WriteLine(now.ToString("D"));
Console.WriteLine(now.ToString("f"));
Console.WriteLine(now.ToString("F"));
Console.WriteLine(now.ToString("g"));
Console.WriteLine(now.ToString("G"));
Console.WriteLine(now.ToString("m"));
Console.WriteLine(now.ToString("M"));
Console.WriteLine(now.ToString("o"));
Console.WriteLine(now.ToString("O"));
Console.WriteLine(now.ToString("s"));
Console.WriteLine(now.ToString("t"));
Console.WriteLine(now.ToString("T"));
Console.WriteLine(now.ToString("u"));
Console.WriteLine(now.ToString("U"));
Console.WriteLine(now.ToString("y"));
Console.WriteLine(now.ToString("Y"));

Using Date strings to control the output

output:
Thursday, June 28, 2012
12:03:53 PM
6/28/2012
12:03 PM
6/28/2012 12:03:53 PM

DateTime now = DateTime.Now;
Console.WriteLine(now.ToLongDateString());
Console.WriteLine(now.ToLongTimeString());
Console.WriteLine(now.ToShortDateString());
Console.WriteLine(now.ToShortTimeString());
Console.WriteLine(now.ToString());

PHP

output : 2012-06-28 17:33:07

$date = date('Y-m-d H:i:s');
echo $date;

output : 2012/06/28 17:33:07

$date = date('Y/m/d H:i:s');
echo $date;

This time is based on the default server time zone. To get the time in a different time zone it should be set first.

date_default_timezone_set('Australia/Sydney');
$date = date('Y/m/d H:i:s');
echo $date;

JavaScript

output : Thursday, June 28, 2012

<script type="text/javascript"><!--
var now = new Date();
var Weekday = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var Month = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
document.write(Weekday[now.getDay()]+", "+Month[now.getMonth()]+" "+now.getDate()+", "+now.getFullYear());
//--></script>

output : 6/28/2012

<script type="text/javascript"><!--
var now = new Date();
document.write((now.getMonth()+1)+"/"+now.getDate()+"/"+now.getFullYear());
//--></script>

output : 06/28/2012

<script type="text/javascript"><!--
var now = new Date();
var month = now.getMonth()+1;
if( month < 9 ) { month = "0"+month; }
var day = now.getDate();
if( day < 9 ) { day = "0"+day; }
document.write(month+"/"+day+"/"+now.getFullYear());
//--></script>

ios

 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormatter setDateFormat:@"hh:mm a"];   
 NSString *str_date = [dateFormatter stringFromDate:[NSDate date]];
 NSLog(@"str_date:%@",str_date);
 
 

Tags:

Send Location To Mobile From Your Web Site

This is a SMS widget offered by zhiing to send SMS free anywhere in the world. With zhiing, you not limited to plain text message but directions and maps are also possible. Static or dynamic widgets can be generated according to your needs. A great widget obviously.

Get the widget

 
Leave a comment

Posted by on June 25, 2012 in Widgets

 

Tags:

Save and Retrieve images in WPF From Database

Download Sourcecode

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.Drawing.Imaging;

namespace imageDBWPF
{

public partial class MainWindow : Window
{
DataSet ds;
string imageName;

public MainWindow()
{
InitializeComponent();
}

private void insertData()
{
if (imageName == "")
{
return;
}

try
{
//Initialize a file stream to read the image file
FileStream fs = new FileStream(@imageName, FileMode.Open, FileAccess.Read);
//Initialize a byte array with size of stream
byte[] imgByteArr = new byte[fs.Length];
//Read data from the file stream and put into the byte array
fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
fs.Close();

//Save binary data in database
string constr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\imageDB.mdf;Integrated Security=True;User Instance=True";
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
string sql = "insert into tbl_Image(id,img) values('" + textBox1.Text + "',@img)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add(new SqlParameter("img", imgByteArr));
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
MessageBox.Show("Image Added");
refreshImageList();
}
}
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}

private void refreshImageList()
{
try
{
string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\imageDB.mdf;Integrated Security=True;User Instance=True";

using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();

using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM tbl_Image", conn))
{
ds = new DataSet("myDataSet");
adapter.Fill(ds);
DataTable dt = ds.Tables[0];

//Alternatively can fill data into a data table directly
//DataTable dt2 = new DataTable();
//adapter.Fill(dt2);

comboBox1.Items.Clear();

foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["id"].ToString());
}
comboBox1.SelectedIndex = 0;

}
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button1_Click(object sender, RoutedEventArgs e)
{
try
{

FileDialog fldlg = new OpenFileDialog();
fldlg.InitialDirectory = Environment.SpecialFolder.MyPictures.ToString();
fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
fldlg.ShowDialog();
{

imageName = fldlg.FileName;
ImageSourceConverter isc = new ImageSourceConverter();
image1.SetValue(Image.SourceProperty,isc.ConvertFromString(imageName));

}

fldlg = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

private void button2_Click(object sender, RoutedEventArgs e)
{
insertData();
}

private void button3_Click(object sender, RoutedEventArgs e)
{
DataTable dataTable = ds.Tables[0];

foreach (DataRow row in dataTable.Rows)
{
if (row[0].ToString() == comboBox1.SelectedItem.ToString())
{
//Store binary data read from the database in a byte array
byte[] blob = (byte[])row[1];
MemoryStream stream = new MemoryStream();
stream.Write(blob, 0, blob.Length);
stream.Position = 0;

System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
BitmapImage bi = new BitmapImage();
bi.BeginInit();

MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
image2.Source = bi;

}
}
}

}
}

 
14 Comments

Posted by on June 24, 2012 in WPF, XAML

 

Tags: ,

Save and Retrieve images in C# from SQL Database

Download Sourcecode

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace imageDB
{
public partial class Form1 : Form
{
DataSet ds;
string imageName;

public Form1()
{
InitializeComponent();
}

//Without using statements
/*private void insertData()
{
try
{

if (imageName != "")
{
//Initialize a file stream to read the image file
FileStream fs = new FileStream(@imageName, FileMode.Open, FileAccess.Read);
//Initialize a byte array with size of stream
byte[] imgByteArr = new byte[fs.Length];
//Read data from the file stream and put into the byte array
fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
fs.Close();

//Save binary data in database
string constr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\imageDB.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(constr);
conn.Open();
string sql = "insert into tbl_Image(id,img) values('" + textBox1.Text + "',@img)";

SqlParameter imageParameter = new SqlParameter();
imageParameter.SqlDbType = SqlDbType.Image;
imageParameter.ParameterName = "img";
imageParameter.Value = imgByteArr;
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add(imageParameter);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
MessageBox.Show("Image Added");
}

cmd.Dispose();
conn.Dispose();

}
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}*/

private void insertData()
{
if (imageName == "")
{
return;
}

try
{
//Initialize a file stream to read the image file
FileStream fs = new FileStream(@imageName, FileMode.Open, FileAccess.Read);
//Initialize a byte array with size of stream
byte[] imgByteArr = new byte[fs.Length];
//Read data from the file stream and put into the byte array
fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
fs.Close();

//Save binary data in database
string constr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\imageDB.mdf;Integrated Security=True;User Instance=True";
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
string sql = "insert into tbl_Image(id,img) values('" + textBox1.Text + "',@img)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add(new SqlParameter("img", imgByteArr));
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
MessageBox.Show("Image Added");
refreshImageList();
}
}
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}

private void refreshImageList()
{
try
{
string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\imageDB.mdf;Integrated Security=True;User Instance=True";

using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();

using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM tbl_Image", conn))
{
ds = new DataSet("myDataSet");
adapter.Fill(ds);
DataTable dt = ds.Tables[0];

//Alternatively can fill data into a data table directly
//DataTable dt2 = new DataTable();
//adapter.Fill(dt2);

comboBox1.Items.Clear();

foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["id"].ToString());
}
comboBox1.SelectedIndex = 0;

//Display queried data in a grid view
dataGridView1.DataSource = dt;
}
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button1_Click(object sender, EventArgs e)
{
try
{

FileDialog fldlg = new OpenFileDialog();
fldlg.InitialDirectory = Environment.SpecialFolder.MyPictures.ToString();
fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
if (fldlg.ShowDialog() == DialogResult.OK)
{
imageName = fldlg.FileName;
Bitmap bmp = new Bitmap(imageName);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = (Image)bmp;
}

fldlg = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

private void button2_Click(object sender, EventArgs e)
{
insertData();
}

private void button3_Click(object sender, EventArgs e)
{
DataTable dataTable = ds.Tables[0];
//If there is an already an image in picturebox, then delete it
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
}

//Initialize a file stream to write image data
FileStream fs = new FileStream("image.jpg", FileMode.Create);

foreach (DataRow row in dataTable.Rows)
{
if (row[0].ToString() == comboBox1.SelectedItem.ToString())
{
//Store binary data read from the database in a byte array
byte[] blob = (byte[])row[1];

//Write data in image file using file stream
fs.Write(blob, 0, blob.Length);
fs.Close();
fs = null;

pictureBox2.Image = Image.FromFile("image.jpg");
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox2.Refresh();

}
}
}
}

}

 
3 Comments

Posted by on June 24, 2012 in C#.Net, SQL

 

Tags: ,

Open a file with default file extension in C#

var fileToOpen = "PATH_TO_FILE";
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = fileToOpen
};

process.Start();
process.WaitForExit();

UseShellExecute parameter tells Windows to use the default program for the type of file.

WaitForExit will cause your application to wait until the application luanched has been closed.

 
Leave a comment

Posted by on June 22, 2012 in C#.Net

 

Tags:

c# database access class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace IDStore.db
{
class DBAccess
{
SqlConnection conn;
string table = "tbl_Accounts";
AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection();

public DBAccess()
{
conn = ConnectionManager.GetConnection();
}

public void Openconn()
{
if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
{
conn.Open();
}

}

public void Closeconn()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}

}

public int ExecuteNonQuery(string query)
{
int result = 0;
conn.Open();

try
{
SqlCommand cmd = new SqlCommand();
cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
result = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return result;
}

public DataSet ExecuteQuery(string query)
{
conn.Open();
try
{
SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
DataSet ds = new DataSet();
adapter.Fill(ds, "Table");
return ds;

}
finally
{
conn.Close();
}
}

public List<Account> getAllAccountsASList()
{
List<Account> list = new List<Account>();
conn.Open();

SqlCommand cmd = new SqlCommand();
cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM " + table;
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
int accountID = dr.GetInt32(0);
string type = dr.GetString(1);
string username = dr.GetString(2);
string password = dr.GetString(3);
string description = dr.GetString(4);
list.Add(new Account { AccountID = accountID, Type = type, Username = username, Password = password, Description = description });
}
return list;
}

public bool HasRows(string query)
{
conn.Open();
try
{
SqlDataReader dr;
SqlCommand cmd = new SqlCommand();
cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
dr = cmd.ExecuteReader();

if (dr.HasRows)
{
return true;
}

}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

return false;
}
}
}

Download the complete sample project

 
Leave a comment

Posted by on June 22, 2012 in C#.Net

 

Tags: ,