Tuesday, February 23, 2010

C# - example of using Web Service

Assume a web service (with .ASMX file) have been created and registered properly.

Below please find steps to utilize the Web Serice;

1) Use "Add Reference --> Add Web reference" to add the web service by using "Visual Studio" (to specify the URL of the ASMX file, e.g.: http://xxx/sample.asmx). It will create a name space of the service.

2) Use code as below to call function(s) provided by the service:

try
{
//身份验证
cn.xxx.MySoapHeader soapHeader = new cn.xxx.MySoapHeader();
soapHeader.UserName = "test";
soapHeader.PassWord = "test";
string FileName = "";
byte[] AccessoriesByte =null;

cn.xxx.SendEmail service = new cn.xxx.SendEmail();
service.MySoapHeaderValue = soapHeader;

int result=service.SendXXXEmail("111", "222");
if (result==1) MessageBox.Show("Success"); }
else MessageBox.Show("Failure");+ "]");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
...
}

Monday, February 22, 2010

C# - example of HTTP call

1. Sample to use HTTP GET protocol

using system.net;

{
....
string strURL;
strURL = "http://xxxx/default.aspx";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);

request.Method = "GET";
try
{
using (WebResponse wr = request.GetResponse())
{
Stream strm = wr.GetResponseStream();
StreamReader sr = new StreamReader(strm);
string line;

while ((line = sr.ReadLine()) != null)
{
MessageBox.Show(line.ToString());
}
strm.Close();
wr.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
....
}

2. Example of reading content of a file through URL

string strURL = "http://xxx/test.html";
string rl = "";
string strContent = "";

try
{
WebRequest myReq = WebRequest.Create(strURL);
WebResponse myRes = myReq.GetResponse();
Stream resStream = myRes.GetResponseStream();
StreamReader sr = new StreamReader(resStream, Encoding.Default);
StringBuilder sb = new StringBuilder();

while ((rl = sr.ReadLine()) != null)
{
sb.Append(rl);
}
strContent = sb.ToString();
myRes.Close();
}

Thursday, February 18, 2010

SQL - Simple example (Display Date, truncate string, etc)

Below is a simple example to only display "date" information from a field of DateTime:

CONVERT(varchar,create_timestamp,101) as create_date

Below is an example to truncate string:

left(reason,25) as reason

Monday, February 8, 2010

Below is an example to display image file (or folder content) by using a web browser:

1) Define region of a "webbrowser" component in a windows form

2) Call below command to activate the browser:

webBrowser1.Navigate(g_strPath); // System.Windows.Forms.WebBrowser()

Example of g_strPath: http://myclick.cn/mcs/MIU/testmmsii.htm");

Example of activate a window form:

Form2 webObj = new Form2(strURL);
webObj.Show();

Thursday, February 4, 2010

C# - display content in table

Below is a simple example to display content in table:

{
// create table for display
DataTable table = new DataTable();
table.Columns.Add("TimeStamp", typeof(string));
table.Columns.Add("UserID", typeof(string));
table.Columns.Add("TID", typeof(string));

// loop to add records
while (...)
{
...
table.Rows.Add(strMess1, strMess2, strMess3);
...
}

// display content to GridView
// (Assume GridView has been defined in designer of Visual Studio)
dataGridView1.DataSource = table;
}