Friday, December 17, 2010

Update set of tuples with new values : Oracle

declare

rCount integer;

j integer;

cursor cur_my_cursor is select * from my_table_name;

tRecordSet cur_my_cursor%Rowtype;

begin

rCount := 0; j:=1;

select count(id) into rCount from my_table_name;

dbms_output.put_line(rCount ' Records to be updated');

open cur_my_cursor; for j in 1..rCount

loop

fetch cur_my_cursor into tRecordSet;

update my_table_name mtn set mtn.unique_key = j

where mtn.field_one = tRecordSet.field_one

and mtn.field_two = tRecordSet.field_two

and mtn.field_three = tRecordSet.field_three

and mtn.field_four = tRecordSet.field_four

and mtn.field_five = tRecordSet.five;

dbms_output.put_line('Record id ' tRecordSet.field_one ' has been associated with an unique key of ' j );

end loop;

close cur_my_cursor; end;

ORA-24338: statement handle not executed

This happens when you try to read a cursor after fetching data from it. Try to read the cursor before you fetch data from it



PROCEDURE GetAffiliateCouriers(paffiliate_id number, io_cursor IN OUT appCursor)

IS

vcursor appCursor; tcursor appCursor; retCursor appCursor;

affiliate_id INTEGER; t_allow_courier_selection INTEGER;

v_row AFFILIATE%ROWTYPE; t_row AFFILIATE_COURIER%ROWTYPE;

BEGIN

vcursor := NULL;

tcursor := NULL;

affiliate_id := paffiliate_id;

LOOP

OPEN vcursor FOR SELECT * FROM affiliate WHERE id = affiliate_id AND allowcourierselection = 1;

FETCH vcursor INTO v_row;

IF (vcursor%FOUND) THEN

OPEN tcursor FOR SELECT * FROM affiliate_courier WHERE affiliate_courier.fk_affiliate_id = affiliate_id;

FETCH tcursor INTO t_row;

IF (tcursor%FOUND) THEN

OPEN retCursor FOR SELECT * FROM affiliate_courier WHERE affiliate_courier.fk_affiliate_id = affiliate_id;

io_cursor := retCursor;

RETURN;

END IF;

END IF;

SELECT fk_parent_id INTO affiliate_id FROM affiliate WHERE affiliate.id = affiliate_id;

EXIT WHEN affiliate_id IS NULL;

END LOOP;

io_cursor := retCursor;

END GetAffiliateCouriers;

Overriding Index of a Generic List

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Application.DomainModel.Collections
{
    public class Couriers<Courier> : List<Courier>
    {
        public Courier this[string courierCode]
        {
            get
            {
                return this.Find(delegate(Courier courier)
                { return (string.Compare(courier.CourierCode, courierCode) == 0); });
            }
        }
    }
    public class Courier
    {
        public string CourierCode { get; set; }
    }
}

List.IndexOf(Object o) .NET 3.0

this.ddlCourier.SelectedIndex = affiliatecouriers.IndexOf(affiliatecouriers
        .Find(delegate(Courier courier)
        {
            return courier.ID ==
                int.Parse(iOrder.Supplementary.DeliveryCode);
        }));



References http://msdn2.microsoft.com/en-us/library/x0b5b5bc.aspx

XML Serialization and Deserialization in .NET

/// <summary>
/// Serialize
/// </summary>
/// <param name="clientList">List of objects</param>
/// <returns>success/fail (bool)</returns>
private static bool SaveClientAttributes(List clientList)
{
    try
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List));
        StreamWriter writer =
        new StreamWriter((Server.MapPath(string.Empty)) 
            + "\\ClientData.xml");
        List ClientList = new List();
        serializer.Serialize(writer, clientList);
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }

}
/// <summary>
/// Deserialize
/// </summary>
/// <returns>List of objects</returns>
private static List LoadClientAttributes()
{
    List clientList = new List();
    try
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List));
        FileStream fileStream =
        new FileStream((Server.MapPath(string.Empty))
            + "\\ClientData.xml", FileAccess.Read);
        clientList = (List)serializer.Deserialize(fileStream);
    }
    catch (Exception ex)
    {
        //Do nothing
    }
    return clientList;
}

Reading and writing to Windows.Registry.

using System.Configuration;
using System.Web.Security;
using System.ComponentModel;
using System.Diagnostics;
using System.Configuration.Install;
using Microsoft.Win32;

namespace Install.NameSpace
{
    [RunInstaller(true)]
    public class InstallHelper : Installer
    {

    #region Public Static Methods

    public static void SaveLocalRoot(String value)
    {
        try
        {
            RegistryKey key = Registry
                .LocalMachine.OpenSubKey(ConfigurationManager
                .AppSettings["LocalRootRegKey"]
                .ToString(), true);
            if (key == null)
            Registry.LocalMachine.CreateSubKey(ConfigurationManager
                .AppSettings["LocalRootRegKey"].ToString());
            key.SetValue("DefaultLocalRoot", value);
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    #endregion

    }
} 

Charactor Validatin : Java Script

//
//A funtion for check a charactor input in a web form
//
function checkIt(evt) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if ((charCode < 45 || charCode > 57) && charCode != 8 && charCode != 37 && charCode != 39) {
        alert("This field accepts numbers only")
        return false
    }
    return true
}
//
// you can access it from any contorl as follows
//
onKeyPress = 'javascript:return checkIt(event)'

Timed Recursive Function : Java Script

//
// function that executes recursively but it is timed.
//
function animate() {
    k++
    if (k == 5) k = 1
    document.images[k - 1].src = imgArray2[k - 1].src
    if (k > 1)
        document.images[k - 2].src = imgArray1[k - 2].src
    else
        document.images[3].src = imgArray1[3].src
    var timeOutID = setTimeout("animate()", 1000)
} 

How to set Home Page : Java Script

Hyperlink tag

function setHomePage()
{
    this.style.behavior='url(#default#homepage)';
    this.setHomePage('http://gunasekara.coconia.net/');
    return false;"

}

Script for Autoincrement : Oracle 10

DROP TABLE PROFILE;

DROP SEQUENCE AutoIncrement;

DROP TRIGGER PROFILE_TRIGGER;



CREATE TABLE PROFILE

(



ID NUMBER PRIMARY KEY,

PROFILENAME VARCHAR2(25),

FONT VARCHAR2(25),

DEFAULT_PADDING NUMBER,

FONT_SIZE NUMBER

);

CREATE SEQUENCE AUTOINCREMENT

start with 1

increment by 1

nomaxvalue;

CREATE TRIGGER PROFILE_TRIGGER

BEFORE INSERT

ON PROFILE

REFERENCING NEW AS NEW

FOR EACH ROW BEGIN

SELECT AUTOINCREMENT.nextval INTO :NEW.ID FROM dual;

END;



INSERT INTO PROFILE(PROFILENAME) VALUES('some value one');

INSERT INTO PROFILE(PROFILENAME) VALUES('some value two');



SELECT * FROM PROFILE;

PHP Script for genarate a calender

$cellWidth = 25;
$cellHeight = 25;
$month = 4;
$year = 2006;
$dateNames[1] = "Sun";
$dateNames[2] = "Mon";
$dateNames[3] = "Tue";
$dateNames[4] = "Wed";
$dateNames[5] = "Thr";
$dateNames[6] = "Fri";
$dateNames[7] = "Sat";
$firstDate = mktime(0,0,0,$month,1,$year); // time Stamp
$lastDate = mktime(0,0,0,$month + 1, 0,$year); // time Stamp
$currentDate = mktime(0,0,0,$month,1-date('w',$firstDate),$year); // time Stamp
    echo ""; 
    echo " "; 
    echo "".date('F',$firstDate)." $year"; 
    echo ""; 
    for($i=0;$i<7;$i++){
        echo " "; 
        for($j=1;$j<=7;$j++) {
            if(!$i) { echo "$dateNames[$j]"; } 
            else { 
                echo"".date('j',$currentDate).""; 
                $currentDate = mktime(0,0,0,date('n',$currentDate),
                    date('j',$currentDate)+1,date("Y",$currentDate)); 
            } 
        }
        echo ""; 
    }
    echo "";

Print : Java Script

If you want to print a web page directly use this script code. Although it is simple, applicable for many cases



javascript: void (window.print())
<a href="javascript: void (window.print())" title="Print">Print</a>

How to output web page as MS word document

public partial class Test : Page
{
    protected override void Render(HtmlTextWriter writer)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/msword";
        StringBuilder sb = new StringBuilder();
        StringWriter stringWriter = new StringWriter(sb);
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
        base.Render(htmlTextWriter);
        Response.Write(sb.ToString());
        Response.End();            
    }
}