Archive for Novembre 21st, 2007

Random Password Generator

CodeKeep C# Feed Novembre 21st, 2007

Description: This method generates random password based on the length specified as input

Link: http://www.codekeep.net/snippets/18730b64-1448-4a78-85b7-f2b87da77db6.aspx

public static string GenerateRandomPassword(int pwdLength)
{
    byte[] data = new byte[pwdLength];

    new RNGCryptoServiceProvider().GetBytes(data);
    
    StringBuilder sb = new StringBuilder();

    foreach (byte oneByte in data)
    {
        sb.Append(oneByte.ToString("X2")); // The number is converted to a string of hexadecimal digits
    }
    return sb.ToString();
}

DataReader - DataTable - GridView

CodeKeep C# Feed Novembre 21st, 2007

Description: Create a Datareader, create a Datatable and insert in a Gridview.

Link: http://www.codekeep.net/snippets/1138eb6a-9916-484c-9324-43faf16f6b01.aspx

        protected void Button1_Click(object sender, EventArgs e)
        {
            string Server = "SAPHIRA";
            string Catalog = "MyDatabase";
            string UserID = "hz10452";
            string Password = "password";

            string ConnectionString = "Provider=SQLNCLI;Server=" + Server + ";Database=" + Catalog + ";Uid=" + UserID + ";Pwd=" + Password + ";";

            string sSQL = "SELECT ID, Text, description from tblTEST";
            OleDbDataReader dr;
            OleDbConnection oCnn = new OleDbConnection(ConnectionString);
            OleDbCommand oCmd = oCnn.CreateCommand();

            DataTable mTable = new DataTable();
            DataRow mLine;
            mTable.Columns.Add(new DataColumn("ID", typeof(string)));
            mTable.Columns.Add(new DataColumn("Text", typeof(string)));
            mTable.Columns.Add(new DataColumn("Description", typeof(string)));

            oCnn.Open();
            oCmd.CommandText = sSQL;
            dr = oCmd.ExecuteReader();
            while (dr.Read())
            {
                mLine = mTable.NewRow();
                mLine["ID"] = dr["ID"].ToString();
                mLine["Text"] = dr["Text"].ToString();
                mLine["Description"] = dr["Description"].ToString();
                mTable.Rows.Add(mLine);
            }
            dr.Close();
            oCnn.Close();
            
            this.GridView1.DataSource = mTable;
            this.GridView1.DataBind();
        }

FetachAll Paged SubSonic Controller Method

CodeKeep C# Feed Novembre 21st, 2007

Description: Performs a pages fetch all using SubSonic

Link: http://www.codekeep.net/snippets/f6650e8e-447b-4c37-9140-97da15a2edd0.aspx

[DataObjectMethod( DataObjectMethodType.Select, false )]
public MyCollection FetchAllPaged(int start, int pageLength)
{
	int startIndex;

	if(start <= 1)
	{
		startIndex = 1;
	}
	else
	{
		startIndex = start / pageLength + 1;
	}
	Query qry = new Query( My.Schema );
	qry.PageSize = pageLength;
	qry.PageIndex = startIndex;
	return FetchByQuery(qry);
 }
public int FetchAllCount()
{
	Query qry = new Query( [Schema] );
	return qry.GetCount( [Columns] );
}

Cookie Parameter For DataSource

CodeKeep C# Feed Novembre 21st, 2007

Description: This is a custom parameter for a cookie value

Link: http://www.codekeep.net/snippets/1d8e6e8a-97a8-4bd2-a0b8-68e8fdfb523e.aspx

[ToolboxData("<{0}:CookieParameter ></{0}:CookieParameter>")]

    [System.Drawing.ToolboxBitmap(typeof(Parameter))]

    public class CookieParameter : Parameter

    {

        private string _cookieKey;

        public string Key

        {

            get { return _cookieKey; }

            set { _cookieKey = value; }

        }

        private string _cookie;

        public string Cookie

        {

            get { return _cookie; }

            set { _cookie = value; }

        }   

        public CookieParameter() : base()

        {

        }

        public CookieParameter(string name, object value) : base(name)

        {

        }

        public CookieParameter(string name, TypeCode type, object value) : base(name, type)

        {

        }

        protected CookieParameter(CookieParameter original)

            : base(original)

        {

        }

        protected override object Evaluate(HttpContext context, Control control)

        {

            if ((context != null) && (context.Session != null))

            {

                return context .Request.Cookies[Cookie].Values.Get(Key);

            }

            return null;

        }

        protected override Parameter Clone()

        {

            return new CookieParameter(this);

        }

    }