Friday, May 15, 2009

Distributed Transaction(DTC) using .Net

Add the below reference to the disired Project
System.Transactions

(Using is key word in C# it'll release to garbage collection after usage)


private void button1_Click(object sender, EventArgs e)
{
using (TransactionScope objScope = new TransactionScope())
{
string conString1 = "Data Source=sfa_dev;Persist Security Info=True;User ID=ez;Password=ez;Unicode=True";
string conString2 = "Data Source=CHANDIMA\\SQLEXPRESS;Initial Catalog=Exam;Integrated Security=True";
using (OracleConnection Con1 = new OracleConnection(conString1))
{
OracleCommand ocmd = new OracleCommand();
ocmd.CommandText ="Insert into dtctest values (1,'Test Adddress')";
Con1.Open();
ocmd.ExecuteNonQuery();
Con1.Close();
}
using(SqlConnection con2=new SqlConnection(conString2))
{
SqlCommand scmd=new SqlCommand();
scmd.CommandText ="Insert into tbl_Student values (3,'chandima W','Test Add1',1,'~/Test')";
con2.Open();
scmd.ExecuteNonQuery();
con2.Close();
}
objScope.Complete();
}
}

Wednesday, May 6, 2009

Pass Data in Compression and Decompression Mode

Compression

private byte[] ConvertToStram(DataSet ds)
{
MemoryStream memStream = new MemoryStream();
GZipStream zipStream = new GZipStream(memStream, CompressionMode.Compress);
ds.WriteXml(zipStream, XmlWriteMode.WriteSchema);
zipStream.Close();
byte[] data = memStream.ToArray();
memStream.Close();
ds.Dispose();
return data;
}

Decmpression

public DataSet ConvertStreamToDS(byte[] DataInfo)
{
MemoryStream memStream = new MemoryStream(DataInfo);
GZipStream unzipStream = new GZipStream(memStream, CompressionMode.Decompress);
DataSet ds = new DataSet();
ds.ReadXml(unzipStream);
memStream.Close();
unzipStream.Close();
return ds;
}

XML Type In Oracle

Table Creation

Create Table CallInfo (msg SYS.XMLTYPE,msisdn Varcjar2(10));

Insert values to Table

DECLARE
v_xml SYS.XMLTYPE;
v_doc CLOB;
BEGIN

CLOB v_doc := '' Chr(10) ' MY_TABLE'; v_xml := sys.xmltype.createXML(v_doc);

INSERT INTO tab1 (col1) VALUES (v_xml);
SELECT SYS_XMLGen(table_name) INTO v_xml FROM
user_tables WHERE rownum = 1;

INSERT INTO tab1 (col1) VALUES (v_xml);
COMMIT;
END;

Extract Value From Table

SET LONG 1000
SELECT a.col1.getStringVal() FROM tab1 a;

Extract Values from Selected Tags

SELECT a.col1.extract('//TABLE_NAME/text()').getStringVal() AS "Table Name"
FROM tab1 aWHERE a.col1.existsNode('/TABLE_NAME') = 1;

WebService Async invoke

Method 1

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.OracleClient;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
private DataSet _ds;
private DataSet _ds1;
AS.AsyncSer _ws = new AS.AsyncSer();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Response.Write("OtherGridStart" + DateTime.Now.Millisecond.ToString());
//SetOther();
// Hook PreRenderComplete event for data binding
this.PreRenderComplete +=
new EventHandler(Page_PreRenderComplete);
Response.Write("Begin" + DateTime.Now.Millisecond.ToString());
// Register async methods
AddOnPreRenderCompleteAsync(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation)
);
AddOnPreRenderCompleteAsync(
new BeginEventHandler(BeginAsyncOperationOther),
new EndEventHandler(EndAsyncOperationOther)
);
Response.Write("End" + DateTime.Now.Millisecond.ToString());
Response.Flush();
}
}

IAsyncResult BeginAsyncOperationOther(object sender, EventArgs e,
AsyncCallback cb, object state)
{
Response.Write("OtherGridStart" + DateTime.Now.Millisecond.ToString());
_ws.Url = "http://localhost:2012/AsynService/AsyncSer.asmx"; //new Uri(Request.Url, "AsyncSer.asmx").ToString();
_ws.UseDefaultCredentials = true;
return _ws.BeginGetOther(cb, state);
}
void EndAsyncOperationOther(IAsyncResult ar)
{
Response.Write("OtherGridStart" + DateTime.Now.Millisecond.ToString());
_ds1 = _ws.EndGetOther(ar);
}

//private void SetOther()
//{
// DataSet dd;
// _ws.Url = "http://localhost:2012/AsynService/AsyncSer.asmx"; //new Uri(Request.Url, "AsyncSer.asmx").ToString();
// _ws.UseDefaultCredentials = true;
// dd = _ws.GetOther();
// GridView1.DataSource = dd;
// GridView1.DataBind();
// Response.Write("OtherGridEnd" + DateTime.Now.Millisecond.ToString());
//}

IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
AsyncCallback cb, object state)
{
_ws.Url = "http://localhost:2012/AsynService/AsyncSer.asmx"; //new Uri(Request.Url, "AsyncSer.asmx").ToString();
_ws.UseDefaultCredentials = true;
return _ws.BeginGetInfo(cb, state);
}

void EndAsyncOperation(IAsyncResult ar)
{
Response.Write("Async Com" + DateTime.Now.Millisecond.ToString());
_ds = _ws.EndGetInfo(ar);
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
dg.DataSource = _ds;
dg.DataBind();
GridView1.DataSource = _ds1;
GridView1.DataBind();
}
public override void Dispose()
{
if (_ws != null) _ws.Dispose();
base.Dispose();
}


}

Method 2

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
public partial class Default2 : System.Web.UI.Page
{
//private WebRequest _ws;
AS.AsyncSer _ws = new AS.AsyncSer();
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask task = new PageAsyncTask(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation),
new EndEventHandler(TimeoutAsyncOperation),
null
);
RegisterAsyncTask(task);
}
IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
AsyncCallback cb, object state)
{
_ws.Url = "http://localhost:2012/AsynService/AsyncSer.asmx"; //new Uri(Request.Url, "AsyncSer.asmx").ToString();
_ws.UseDefaultCredentials = true;
//_ws = WebRequest.Create("http://localhost:2012/AsynService/AsyncSer.asmx");
return _ws.BeginGetOther(cb, state);
}
void EndAsyncOperation(IAsyncResult ar)
{
DataSet ds;
ds = _ws.EndGetOther(ar);
dg.DataSource = ds;
dg.DataBind();
}
void TimeoutAsyncOperation(IAsyncResult ar)
{
Response.Write("Data temporarily unavailable");
}
}