Monday, December 26, 2011

Generate five digit Random Number

public string GetRandomString(int seed)
    {
        //use the following string to control your set of alphabetic characters to choose from
        //for example, you could include uppercase too
        const string alphabet = "abcdefghijklmnopqrstuvwxyz";

        // Random is not truly random,
        // so we try to encourage better randomness by always changing the seed value
        Random rnd = new Random((seed + DateTime.Now.Millisecond));

        // basic 5 digit random number
        string result = rnd.Next(10000, 99999).ToString();

        // single random character in ascii range a-z
        string alphaChar = alphabet.Substring(rnd.Next(0, alphabet.Length-1),1);

        // random position to put the alpha character
        int replacementIndex = rnd.Next(0, (result.Length - 1));
        result = result.Remove(replacementIndex, 1).Insert(replacementIndex, alphaChar);

        return result;
    }

Import Excel Data Into An ASP.NET GridView using OLEDB

using System.Data.OleDb;
using System.Data;
public partial class UploadD : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
string cnstr = “Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\a.xls;”+ “Extended Properties=Excel 8.0″;
OleDbConnection oledbConn = new OleDbConnection(cnstr);
string strSQL = “SELECT * FROM [Sheet$]“;
OleDbCommand cmd = new OleDbCommand(strSQL, oledbConn);
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}

Payment gateway implementation C# in asp.Net

Copy paste following code to implement payment gateway using c#.net:
String post_url = “https://test.authorize.net/gateway/transact.dll“;
Hashtable post_values = new Hashtable();
//the API Login ID and Transaction Key must be replaced with valid values
post_values.Add(“x_login”, “6zz6m5N4Et”);
post_values.Add(“x_tran_key”, “9V9wUv6Yd92t27t5″);
post_values.Add(“x_delim_data”, “TRUE”);
post_values.Add(“x_delim_char”, ‘|’);
post_values.Add(“x_relay_response”, “FALSE”);
post_values.Add(“x_type”, “AUTH_CAPTURE”);
post_values.Add(“x_method”, “CC”);
post_values.Add(“x_card_num”, “378282246310005″);
post_values.Add(“x_exp_date”, “0809″);
post_values.Add(“x_amount”, “99999.00″);
post_values.Add(“x_description”, “Sample Transaction”);
post_values.Add(“x_first_name”, “John”);
post_values.Add(“x_last_name”, “Doe”);
post_values.Add(“x_address”, “1234 Street”);
post_values.Add(“x_state”, “WA”);
post_values.Add(“x_zip”, “98004″);
// Additional fields can be added here as outlined in the AIM integration
// guide at: http://developer.authorize.net
// This section takes the input fields and converts them to the proper format
// for an http post. For example: “x_login=username&x_tran_key=a1B2c3D4″
String post_string = “”;
foreach(DictionaryEntry field in post_values)
{
post_string += field.Key + “=” + field.Value + “&”;
}
post_string = post_string.TrimEnd(‘&’);

// create an HttpWebRequest object to communicate with Authorize.net
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(post_url);
objRequest.Method = “POST”;
objRequest.ContentLength = post_string.Length;
objRequest.ContentType = “application/x-www-form-urlencoded”;
// post data is sent as a stream
StreamWriter myWriter = null;
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(post_string);
myWriter.Close();
// returned values are returned as a stream, then read into a string
String post_response;
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()) )
{
post_response = responseStream.ReadToEnd();
responseStream.Close();
}
// the response string is broken into an array
// The split character specified here must match the delimiting character specified above
Array response_array = post_response.Split(‘|’);
// the results are output to the screen in the form of an html numbered list.
resultSpan.InnerHtml += “<OL> \n”;
foreach (string value in response_array)
{
resultSpan.InnerHtml += “<LI>” + value + “&nbsp;</LI> \n”;
}
resultSpan.InnerHtml += “</OL> \n”;
// individual elements of the array could be accessed to read certain response
// fields. For example, response_array[0] would return the Response Code,
// response_array[2] would return the Response Reason Code.
// for a list of response fields, please review the AIM Implementation Guide

Implementing Service Locator (To Resolve Dependency)

using System; /// <summary> /// Summary description for Class1 /// </summary> public class serviceLocator {     public s...