Image Varification (Random Image Generation for Varification)

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Drawing;
using System.Security.Cryptography;

///
/// Summary description for VarificationImage
///

public class VarificationImage
{
public VarificationImage()
{
//
// TODO: Add constructor logic here
//
}
public string CreateImage(string path, int height, int width)
{
Random r = new Random();
string salt = CreateSalt(4);
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmp);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.Clear(Color.Black);
g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3);
g.DrawRectangle(Pens.Brown, 0, 0, width, height);
System.Drawing.Drawing2D.Matrix mymat = new System.Drawing.Drawing2D.Matrix();

for (int i = 0; i < salt.Length - 1; i++)
{
mymat.Reset();
mymat.RotateAt(r.Next(-30, 0), new PointF((float)(width * (0.12 * i)), (float)(height * 0.5)));
g.Transform = mymat;
g.DrawString(salt[i].ToString(), new Font("Comic Sans MS", 18, FontStyle.Italic), SystemBrushes.ActiveCaptionText, (float)(width * (0.12 * i)), (float)(height * 0.5));
g.ResetTransform(); //Monotype Corsiva
}

bmp.Save(path, ImageFormat.Gif);
g.Dispose();
bmp.Dispose();
return salt.Replace("=","");
}
public string CreateSalt(int size)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[size + 1];
rng.GetBytes(buff);
return Convert.ToBase64String(buff);
}

}

Doubleclick to open file

What you need is a place in your MyProgram.cs class to store the command line arguments received in the constructor (Main).
Something like public string SelectedFile;

In the Main constructor, do this:

public static void Main(string[] args)
{ string SelectedFile;
if (args.Length > 0)
{
SelectedFile = Convert.ToString(args[0]);
MessageBox.Show(SelectedFile); // Just to confirm it.
}

MyProgram app = new MyProgram();
app.SelectedFile = SelectedFile;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(app);
}

Yahoo Contact Importer (Make changes According to your need)

using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Gnilly.Syndication.Mail
{
public class YahooExtract
{
private const string _addressBookUrl = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&.rand=1671497644&A=H&Yahoo_ab.csv";
private const string _authUrl = "https://login.yahoo.com/config/login?";
private const string _loginPage = "https://login.yahoo.com/config/login";
private const string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";

public bool Extract( NetworkCredential credential, out MailContactList list )
{
bool result = false;

list = new MailContactList();

try
{
WebClient webClient = new WebClient();
webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
webClient.Encoding = Encoding.UTF8;

byte[] firstResponse = webClient.DownloadData( _loginPage );
string firstRes = Encoding.UTF8.GetString( firstResponse );


NameValueCollection postToLogin = new NameValueCollection();
Regex regex = new Regex( "type=\"hidden\" name=\"(.*?)\" value=\"(.*?)\"", RegexOptions.IgnoreCase );
Match match = regex.Match( firstRes );
while ( match.Success )
{
if ( match.Groups[ 0 ].Value.Length > 0 )
{
postToLogin.Add( match.Groups[ 1 ].Value, match.Groups[ 2 ].Value );
}
match = regex.Match( firstRes, match.Index + match.Length );
}


postToLogin.Add( ".save", "Sign In" );
postToLogin.Add( ".persistent", "y" );

string login = credential.UserName.Split( '@' )[ 0 ];
postToLogin.Add( "login", login );
postToLogin.Add( "passwd", credential.Password );

webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
webClient.Headers[ HttpRequestHeader.Referer ] = _loginPage;
webClient.Encoding = Encoding.UTF8;
webClient.Headers[ HttpRequestHeader.Cookie ] = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];

webClient.UploadValues( _authUrl, postToLogin );
string cookie = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];

if ( string.IsNullOrEmpty( cookie ) )
{
return false;
}

string newCookie = string.Empty;
string[] tmp1 = cookie.Split( ',' );
foreach ( string var in tmp1 )
{
string[] tmp2 = var.Split( ';' );
newCookie = String.IsNullOrEmpty( newCookie ) ? tmp2[ 0 ] : newCookie + ";" + tmp2[ 0 ];
}

// set login cookie
webClient.Headers[ HttpRequestHeader.Cookie ] = newCookie;
byte[] thirdResponse = webClient.DownloadData( _addressBookUrl );
string thirdRes = Encoding.UTF8.GetString( thirdResponse );

string crumb = string.Empty;
Regex regexCrumb = new Regex( "type=\"hidden\" name=\"\\.crumb\" id=\"crumb1\" value=\"(.*?)\"", RegexOptions.IgnoreCase );
match = regexCrumb.Match( thirdRes );
if ( match.Success && match.Groups[ 0 ].Value.Length > 0 )
{
crumb = match.Groups[ 1 ].Value;
}


NameValueCollection postDataAB = new NameValueCollection();
postDataAB.Add( ".crumb", crumb );
postDataAB.Add( "vcp", "import_export" );
postDataAB.Add( "submit[action_export_yahoo]", "Export Now" );

webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
webClient.Headers[ HttpRequestHeader.Referer ] = _addressBookUrl;

byte[] FourResponse = webClient.UploadValues( _addressBookUrl, postDataAB );
string csvData = Encoding.UTF8.GetString( FourResponse );

string[] lines = csvData.Split( '\n' );
foreach ( string line in lines )
{
string[] items = line.Split( ',' );
if ( items.Length < 5 )
{
continue;
}
string email = items[ 4 ];
string name = items[ 3 ];
if ( !string.IsNullOrEmpty( email ) && !string.IsNullOrEmpty( name ) )
{
email = email.Trim( '\"' );
name = name.Trim( '\"' );
if ( !email.Equals( "Email" ) && !name.Equals( "Nickname" ) )
{
MailContact mailContact = new MailContact();
mailContact.Name = name;
mailContact.Email = email;
list.Add( mailContact );
}
}
}

result = true;
}
catch
{
}
return result;
}
}
}

Convert Database data to XML

The data existing in the relational database can be easily converted into an xml document in the program. The following code illustrates this.

I have used SQLServer database table employee ( with columns empid, first_name,last_name,deptcd,salary and resig_status)

The connection is established using appropriate connection string. The data from the RDBMS table is stored in the dataset dsEmp. The xml class XmlDataDocument is used to convert the data into an xml data file. It takes the dataset as input and converts to xml data.

{
DataSet dsEmp = new DataSet();
string strCon;
string strSelect;

strCon = "data source=SQLEXPRESS;initial catalog=dbemp;persist security info" +
"=False;user id=sa;password=jadoogar;workstation id=HCL;packet size =4096";
strSelect = "Select empid,first_name,deptcd from employee";

try
{
SqlConnection sqlCon = new SqlConnection(strCon);
SqlDataAdapter empAdapter = new SqlDataAdapter(strSelect,sqlCon);
empAdapter.Fill(dsEmp,"employee");
}
catch
{
}
XmlDataDocument empDoc = new XmlDataDocument(dsEmp);
empDoc.Save(MapPath("xmldata/newemp.xml"));

}

How to Convert CSV to XML using C#

public void ConvertCSVToXML()

{

String[] FileContent = File.ReadAllLines(@"C:\Temp\pan.csv");

String XMLNS = "";

XElement Inv = new XElement("Invoice",

from items in FileContent

let fields = items.Split(',')

select new XElement("Item",

new XElement("ID", fields[0]),

new XElement("Name", fields[1]),

new XElement("Price", fields[2]),

new XElement("Availability", fields[3]),

new XElement("TotalPrice", fields[4])

)

);

File.WriteAllText(@"C:\Temp\pan.xml", XMLNS + Inv.ToString() );

}

Server-side state management

This kind of mechanism retains state in the server.
Application State
The data stored in an application object can be shared by all the sessions of the application. The application object stores data in the key value pair.
Session State
Session state stores session-specific information and the information is visible within the session only. ASP.NET creates unique sessionId for each session of the application. SessionIDs are maintained either by an HTTP cookie or a modified URL, as set in the application's configuration settings. By default, SessionID values are stored in a cookie.
Database
Database can be used to store large state information. Database support is used in combination with cookies or session state

Client-side state management

This maintains information on the client's machine using Cookies, View State, and Query Strings.
Cookies.
A cookie is a small text file on the client machine either in the client's file system or memory of client browser session. Cookies are not good for sensitive data. Moreover, Cookies can be disabled on the browser. Thus, you can't rely on cookies for state management.
View State
Each page and each control on the page has View State property. This property allows automatic retention of page and controls state between each trip to server. This means control value is maintained between page postbacks. Viewstate is implemented using _VIEWSTATE, a hidden form field which gets created automatically on each page. You can't transmit data to other page using view state.
Querystring
Querystring can maintain limited state information. Data can be passed from one page to another with the URL but you can send limited size of data with the URL. Most browsers allow a limit of 255 characters on URL length.