Serialization and Deserialization

29. December 2009

Serialization and Deserialization

Serialization definition
serialization is the process of converting object into a sequence of bits so
that it can be stored in a file, a memory buffer, or transmitted across a network connection

Deserialization definition
Deserialization is the process of using the serialized state information
to reconstruct the object from the serialized state to its original state.


using System;
using System.Xml.Serialization;

namespace CodeGuru.Serialization
{
[XmlRoot("DataXml")]
public class XMLData
{
private int _Identity = 0;

private string _Name = "";
[XmlElement("Name")]

public string Name
{
get { return this._Name; }
set { this._Name = value; }
}

private string _Address = "";
[XmlAddress]

public string Address
{
get { return this._Address; }
set { this._Address = value; }
}

public XMLData()
{
}
}
}


Serialization


protected void XmlSerializerData()
{
XMLData obj = new XMLData();
obj.Name = "Aamir";
obj.IgnoreMe = "office";
XmlSerializer serializer = new XmlSerializer(obj.GetType());
Stream stream = new FileStream("c:\\MyFileName.xml", FileMode.Create,
FileAccess.Write, FileShare.None);
serializer.Serialize(stream, obj);
stream.Close();
}


Deserialization

public void XmlDeserialize(string XMLPath)
{
System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(typeof(XMLData));
FileStream fs = new FileStream(XMLPath, FileMode.Open);
object obj = xmlSer.Deserialize(fs);
fs.Close();
XMLData dataObj = (XMLData)obj

}




http://msdn.microsoft.com/en-us/library/ms731073.aspx



Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, CSharp , ,

single Report Viewer Control with Multiple Reports using asp.net csharp

25. December 2009

single Report Viewer Control with Multiple Reports using asp.net csharp

reportViewer1.Reset();

reportViewer2.LocalReport.ReportEmbeddedResource = "ReportViewer.AamirHasan.rdlc";

this.reportViewer2.RefreshReport();


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, asp.net 4.0, CSharp

Sending HTML Mail Using Gmail SMTP

22. December 2009
Sending HTML Mail Using Gmail SMTP

use namespace System.Net.Mail

 

    Sendmail();

       

        protected static bool Sendmail()
        {
            MailMessage MyMailMessage = new MailMessage();

            MyMailMessage.From = new MailAddress("xxx-xxx-xxx@gmail.com");

            MyMailMessage.To.Add("xxxxxx@gmail.com");

            MyMailMessage.Subject = "Feedback Form";

            MyMailMessage.IsBodyHtml = true;

            MyMailMessage.Body = "<table><tr><td>ggg</table></tr></td>";

            SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");

            SMTPServer.Port = 587;

            SMTPServer.Credentials = new System.Net.NetworkCredential("xxx-xxx-xxx@gmail.com", "zzzzzzz");

            SMTPServer.EnableSsl = true;

            try
            {

                SMTPServer.Send(MyMailMessage);


                return true;
            }

            catch
            {

                return false;


            }

 

posted by aamir Hasan



Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, CSharp, TIPS

Windows Service uisng C#

21. December 2009

Step 1. Create Skeleton of the Service

To create a new Window Service, pick Windows Service option from your Visual C# Projects, give your service a name, and click OK.

The result look like this. The Wizard adds WebService1.cs class to your project.

Set your ServiceName to your own name so it would be easier to recognize your service during testing OR you can set this property programmatically using this line this.ServiceName = "mcWinService";

This is the name you will be looking for later :).

The default code of WebService1.cs added by the Wizard looks like here

namespace mcWebService
{
using System;
using System.Collections;
using System.Core;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Web.Services;
using System.Diagnostics;
using System.ServiceProcess;
public class WinService1 : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components;
public WinService1()
{
// This call is required by the WinForms Component Designer. InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
/ create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new WinService1(), new
ySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WinService1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components =
new System.ComponentModel.Container();
this.ServiceName = "WinService1";
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}


Step 2. Add functionality to your service

As you saw WebService1.cs, there are two overridden functions OnStart and OnStop. The OnStart function executes when you start your service and the OnStop function gets execute when you stop a service. I write some text to a text file when you start and stop the service.

protected override void OnStart(string[] args)
{
FileStream fs =
new FileStream(@"c:\\Studentacad.com.txt" ,
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter =
new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" mcWindowsService: Service Started \n");
m_streamWriter.Flush();
m_streamWriter.Close();
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
FileStream fs =
new FileStream(@"c:\\Studentacad.com.txt" ,
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter =
new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" mcWindowsService: Service Stopped \n"); m_streamWriter.Flush();
m_streamWriter.Close();

 
Step 3: Install and Run the Service

Build of this application makes one exe, mcWinService.exe. You need to call installutil to
egister this service from command line.
installutil C:\mcWebService\bin\Debug\mcWebService.exe
You use /u option to uninstall the service.
installutil /u C:\mcWebService\bin\Debug\mcWebService.exe
Run the application

Step 4: Start and Stop the Service

You need to go to the Computer Management to Start to start and stop the service. You can use Manage menu item by right clicking on My Computer. 

Under Services and Applications, you will see the service mcWinService. Start and Stop menu item starts and stops the service.

Step 5: Test the Service

Go to your c:\Studentacad.com directory and see if text file is there with contents or not.if yes it mean u did


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

CSharp ,

HttpWebRequest to post data to a remote Web page

18. December 2009

 Uri uri = new Uri("http://www.studentacad.com/");
        string data = "";
        if (uri.Scheme == Uri.UriSchemeHttp)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Method = WebRequestMethods.Http.Post;
            request.ContentLength = data.Length;
            request.ContentType = "application/x-www-form-urlencoded";

            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(data);
            writer.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();
            response.Close();

            this.Panel1.GroupingText = tmp;
        }


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, asp.net 4.0, CSharp , ,



User Name: Guest

Your Ip: 38.107.191.94
Time: