Friday, July 24, 2009
Tuesday, October 14, 2008
Converting Byte Array to/from Hexadecimal String in C#
.NET framework provides methods to convert a byte array into a Hexadecimal string ( byte.To String(“X”) ), it is not so easy to convert a hexadecimal string back into a byte array.This Example Shows u how to convert byte array into a Hexadecimal string and Hexadecimal string to byte array. Let us See
This is for Byte array to String:-
Here U can pass Sql Rowversion also.
public static string ByteArrayToString(byte[] rowVersion)
{
string retVal = "0x";
string toAdd = "";
foreach (byte b in rowVersion)
{
toAdd = b.ToString("X");
if (toAdd.Length == 1)
{
retVal += "0" + toAdd;
}
else
{
retVal += toAdd;
}
}
return retVal;
}
This for Hexadecimal string to byte array:-
public static byte[] StringToByteArray(string rowVersion)
{
if (rowVersion.Length != 18)
{
throw new Exception();
}
byte[] retVal = new byte[8];
for (int index = 2; index < 18; index += 2)
{
retVal[(index / 2) - 1] =
(byte)int.Parse(
rowVersion.Substring(index, 2),
System.Globalization.NumberStyles.HexNumber);
}
return retVal;
}
I hope this two methods will help u
This is for Byte array to String:-
Here U can pass Sql Rowversion also.
public static string ByteArrayToString(byte[] rowVersion)
{
string retVal = "0x";
string toAdd = "";
foreach (byte b in rowVersion)
{
toAdd = b.ToString("X");
if (toAdd.Length == 1)
{
retVal += "0" + toAdd;
}
else
{
retVal += toAdd;
}
}
return retVal;
}
This for Hexadecimal string to byte array:-
public static byte[] StringToByteArray(string rowVersion)
{
if (rowVersion.Length != 18)
{
throw new Exception();
}
byte[] retVal = new byte[8];
for (int index = 2; index < 18; index += 2)
{
retVal[(index / 2) - 1] =
(byte)int.Parse(
rowVersion.Substring(index, 2),
System.Globalization.NumberStyles.HexNumber);
}
return retVal;
}
I hope this two methods will help u
Wednesday, September 24, 2008
Serialize and DeSerialize an object into XML
Required namespace to Serialize and DeSerialize an object in this artilce are:
1. System.IO
2. System.Xml
3. System.Xml.Serialization
In order to show how to Serialize and DeSerialize an object, I will create a class called MyClass that will have two public variable named name and address. My code for this class is as follows
public class MyClass
{
public string name = string.Empty;
public string address = string.Empty;
}
I have set this class value as following:
MyClass myClass = new MyClass();
myClass.name = "Sheo Narayan";
myClass.address = "Washington DC, US";
How to Serialize an Object:
private string SerializeAnObject(object obj)
{
System.Xml.XmlDocument doc = new XmlDocument();
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
System.IO.MemoryStream stream = new System.IO.MemoryStream();
try
{
serializer.Serialize(stream, obj);
stream.Position = 0;
doc.Load(stream);
return doc.InnerXml;
}
catch
{
throw;
}
finally
{
stream.Close();
stream.Dispose();
}
}
This is a generic function and you can use this function to pass any type of object that can be serialized, even you can serialize a collection object. This method will return a string that is nothing but our serialized object in XML format. I will call my above function like this
string xmlObject = SerializeAnObject(myClass);
How to DeSerialize an Object
private object DeSerializeAnObject(string xmlOfAnObject)
{
MyClass myObject = new MyClass();
System.IO.StringReader read = new StringReader(xmlOfAnObject);
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(myObject.GetType());
System.Xml.XmlReader reader = new XmlTextReader(read);
try
{
myObject = (MyClass)serializer.Deserialize(reader);
return myObject;
}
catch
{
throw;
}
finally
{
reader.Close();
read.Close();
read.Dispose();
}
}
This function return an object so to covert that object into my class I will have to unbox it. In order to avoid boxing and unboxing, you can simply specify your class name as a parameter to these functions. To get my class from serialized xml I will call above function like following and extract its properties or use in the way i want.
MyClass deSerializedClass = (MyClass) DeSerializeAnObject(xmlObject);
string name = deSerializedClass.name;
string address = deSerializedClass.address;
Thats all
1. System.IO
2. System.Xml
3. System.Xml.Serialization
In order to show how to Serialize and DeSerialize an object, I will create a class called MyClass that will have two public variable named name and address. My code for this class is as follows
public class MyClass
{
public string name = string.Empty;
public string address = string.Empty;
}
I have set this class value as following:
MyClass myClass = new MyClass();
myClass.name = "Sheo Narayan";
myClass.address = "Washington DC, US";
How to Serialize an Object:
private string SerializeAnObject(object obj)
{
System.Xml.XmlDocument doc = new XmlDocument();
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
System.IO.MemoryStream stream = new System.IO.MemoryStream();
try
{
serializer.Serialize(stream, obj);
stream.Position = 0;
doc.Load(stream);
return doc.InnerXml;
}
catch
{
throw;
}
finally
{
stream.Close();
stream.Dispose();
}
}
This is a generic function and you can use this function to pass any type of object that can be serialized, even you can serialize a collection object. This method will return a string that is nothing but our serialized object in XML format. I will call my above function like this
string xmlObject = SerializeAnObject(myClass);
How to DeSerialize an Object
private object DeSerializeAnObject(string xmlOfAnObject)
{
MyClass myObject = new MyClass();
System.IO.StringReader read = new StringReader(xmlOfAnObject);
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(myObject.GetType());
System.Xml.XmlReader reader = new XmlTextReader(read);
try
{
myObject = (MyClass)serializer.Deserialize(reader);
return myObject;
}
catch
{
throw;
}
finally
{
reader.Close();
read.Close();
read.Dispose();
}
}
This function return an object so to covert that object into my class I will have to unbox it. In order to avoid boxing and unboxing, you can simply specify your class name as a parameter to these functions. To get my class from serialized xml I will call above function like following and extract its properties or use in the way i want.
MyClass deSerializedClass = (MyClass) DeSerializeAnObject(xmlObject);
string name = deSerializedClass.name;
string address = deSerializedClass.address;
Thats all
How to show a report of reporting services from .net applications
Show Reporting Service Report from Asp .Net Application And Pass Report Parameters
After deploying your reports to report server, you need to access these reports and show them in your custom .net applications. This post will help you to do this task. Please see the clip that shows you how to deploy your report and how to access them.
Let's say you have a web application that needs to show reports. Firstly, you need add the ReportViewer control from toolbox (which is in data tab) to your page. Then you need to write some code to load report inside the ReportViewer probably you write this code either in page load or in a button click. Take a look to this code:
reportViewer1.ServerReport.ReportServerUrl = new Uri("http://ServerNam/ServiceName");
reportViewer1.ServerReport.ReportPath = "/Report Project State(ApplicationFolder)/Report1(report Name)";
List parameters = new List();
parameters.Add(new ReportParameter("RefTable", comboBox1.SelectedItem.ToString()));
reportViewer1.ServerReport.SetParameters(parameters);
reportViewer1.ProcessingMode = ProcessingMode.Remote;
reportViewer1.ShowParameterPrompts = false;
reportViewer1.ShowPromptAreaButton = false;
reportViewer1.ServerReport.Refresh();
this.reportViewer1.RefreshReport();
After deploying your reports to report server, you need to access these reports and show them in your custom .net applications. This post will help you to do this task. Please see the clip that shows you how to deploy your report and how to access them.
Let's say you have a web application that needs to show reports. Firstly, you need add the ReportViewer control from toolbox (which is in data tab) to your page. Then you need to write some code to load report inside the ReportViewer probably you write this code either in page load or in a button click. Take a look to this code:
reportViewer1.ServerReport.ReportServerUrl = new Uri("http://ServerNam/ServiceName");
reportViewer1.ServerReport.ReportPath = "/Report Project State(ApplicationFolder)/Report1(report Name)";
List parameters = new List();
parameters.Add(new ReportParameter("RefTable", comboBox1.SelectedItem.ToString()));
reportViewer1.ServerReport.SetParameters(parameters);
reportViewer1.ProcessingMode = ProcessingMode.Remote;
reportViewer1.ShowParameterPrompts = false;
reportViewer1.ShowPromptAreaButton = false;
reportViewer1.ServerReport.Refresh();
this.reportViewer1.RefreshReport();
Thursday, August 28, 2008
View Reporting Service in ReportViewer
ConnectionStringSettings lSetting;
lSetting = ConfigurationManager.ConnectionStrings["Billing.Connection"];
//string cnString = @"Data Source= HTS-003\SQL2005;Initial Catalog=Billing;" +
// "User Id=sa;Password=hts";
//use following if you use standard security
//string cnString = @"Data Source=(local);Initial Catalog=northwind;
// Integrated Security=SSPI";
//declare Connection, command and other related objects
SqlConnection conReport = new SqlConnection();
SqlCommand cmdReport = new SqlCommand();
SqlDataReader drReport;
DataSet dsReport = new PatientDataSet();//Datasource Name
try
{
//open connection
conReport.ConnectionString = lSetting.ConnectionString;
conReport.Open();
//prepare connection object to get the data through reader and populate into dataset
cmdReport.CommandType = CommandType.Text;
cmdReport.Connection = conReport;
cmdReport.CommandText = "Select * FROM BA_Patients ";
//read data from command object
drReport = cmdReport.ExecuteReader();
//new cool thing with ADO.NET... load data directly from reader to dataset
dsReport.Tables[0].Load(drReport);
//close reader and connection
drReport.Close();
conReport.Close();
//provide local report information to viewer
reportViewer1.LocalReport.ReportEmbeddedResource = "BillingAndAccounts.PatientList.rdlc";
//prepare report data source
ReportDataSource rds = new ReportDataSource();
rds.Name = "PatientDataSet_BA_Patients";
rds.Value = dsReport.Tables[0];
reportViewer1.LocalReport.DataSources.Add(rds);
//add report parameters
ReportParameter[] Param = new ReportParameter[2];
switch (reportType)
{
case "N":
Param[0] = new ReportParameter("parReportTitle", "Orders by Nationality");
Param[1] = new ReportParameter("parReportType", "N");
break;
case "T":
Param[0] = new ReportParameter("parReportTitle", "Orders List");
Param[1] = new ReportParameter("parReportType", "T");
break;
case "S":
Param[0] = new ReportParameter("parReportTitle", "Orders by Sex");
Param[1] = new ReportParameter("parReportType", "S");
break;
case "O":
Param[0] = new ReportParameter("parReportTitle", "Orders by Occupation");
Param[1] = new ReportParameter("parReportType", "O");
break;
case "P":
Param[0] = new ReportParameter("parReportTitle", "Orders by PatientName");
Param[1] = new ReportParameter("parReportType", "P");
break;
}
reportViewer1.LocalReport.SetParameters(Param);
//load report viewer
reportViewer1.RefreshReport();
}
catch (Exception ex)
{
//display generic error message back to user
MessageBox.Show(ex.Message);
}
finally
{
//check if connection is still open then attempt to close it
if (conReport.State == ConnectionState.Open)
{
conReport.Close();
}
}
lSetting = ConfigurationManager.ConnectionStrings["Billing.Connection"];
//string cnString = @"Data Source= HTS-003\SQL2005;Initial Catalog=Billing;" +
// "User Id=sa;Password=hts";
//use following if you use standard security
//string cnString = @"Data Source=(local);Initial Catalog=northwind;
// Integrated Security=SSPI";
//declare Connection, command and other related objects
SqlConnection conReport = new SqlConnection();
SqlCommand cmdReport = new SqlCommand();
SqlDataReader drReport;
DataSet dsReport = new PatientDataSet();//Datasource Name
try
{
//open connection
conReport.ConnectionString = lSetting.ConnectionString;
conReport.Open();
//prepare connection object to get the data through reader and populate into dataset
cmdReport.CommandType = CommandType.Text;
cmdReport.Connection = conReport;
cmdReport.CommandText = "Select * FROM BA_Patients ";
//read data from command object
drReport = cmdReport.ExecuteReader();
//new cool thing with ADO.NET... load data directly from reader to dataset
dsReport.Tables[0].Load(drReport);
//close reader and connection
drReport.Close();
conReport.Close();
//provide local report information to viewer
reportViewer1.LocalReport.ReportEmbeddedResource = "BillingAndAccounts.PatientList.rdlc";
//prepare report data source
ReportDataSource rds = new ReportDataSource();
rds.Name = "PatientDataSet_BA_Patients";
rds.Value = dsReport.Tables[0];
reportViewer1.LocalReport.DataSources.Add(rds);
//add report parameters
ReportParameter[] Param = new ReportParameter[2];
switch (reportType)
{
case "N":
Param[0] = new ReportParameter("parReportTitle", "Orders by Nationality");
Param[1] = new ReportParameter("parReportType", "N");
break;
case "T":
Param[0] = new ReportParameter("parReportTitle", "Orders List");
Param[1] = new ReportParameter("parReportType", "T");
break;
case "S":
Param[0] = new ReportParameter("parReportTitle", "Orders by Sex");
Param[1] = new ReportParameter("parReportType", "S");
break;
case "O":
Param[0] = new ReportParameter("parReportTitle", "Orders by Occupation");
Param[1] = new ReportParameter("parReportType", "O");
break;
case "P":
Param[0] = new ReportParameter("parReportTitle", "Orders by PatientName");
Param[1] = new ReportParameter("parReportType", "P");
break;
}
reportViewer1.LocalReport.SetParameters(Param);
//load report viewer
reportViewer1.RefreshReport();
}
catch (Exception ex)
{
//display generic error message back to user
MessageBox.Show(ex.Message);
}
finally
{
//check if connection is still open then attempt to close it
if (conReport.State == ConnectionState.Open)
{
conReport.Close();
}
}
Tuesday, August 26, 2008
Radwindow modal Popup
function closePopup()
{
GetRadWindow().Close();
}
function GetRadWindow()
{
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;//IE (and Moz az well)
return oWindow;
}
function RefreshParentPage()
{
GetRadWindow().BrowserWindow.location.reload();
}
function openRadWindow(ClientId)
{
var oWnd = radopen("Client.aspx?ClientId=" +ClientId, "RadWindow1" );
oWnd.center();
}
function OpenWindow()
{
var oWnd = radopen("Client.aspx?", "RadWindow1" );
oWnd.center();
}
{
GetRadWindow().Close();
}
function GetRadWindow()
{
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;//IE (and Moz az well)
return oWindow;
}
function RefreshParentPage()
{
GetRadWindow().BrowserWindow.location.reload();
}
function openRadWindow(ClientId)
{
var oWnd = radopen("Client.aspx?ClientId=" +ClientId, "RadWindow1" );
oWnd.center();
}
function OpenWindow()
{
var oWnd = radopen("Client.aspx?", "RadWindow1" );
oWnd.center();
}
Subscribe to:
Comments (Atom)