Wednesday, June 18, 2008

Failed to access IIS metabase problem

I was using VS2008. it had installed .NET framework 3.5 version. This error occurred when i try to view web application that is in IIS. I googled for it and found following soln. It's working perpectly ;)

Possible Cause:-
When you install IIS AFTER .NET 2.0 framework, the rights of the ASPNET user had not been set correctly.

Resolution
Repair (Uninstall if repair does not work for you) .NET Framework 2.0

Simply run the following from command line to reset the IIS registry settings for aspnet user. Usually framework directory for .Net Framework 2.0 resides under C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i

Thursday, May 29, 2008

Tree in a datagrid

I used DevExpress.XtraTreeList.TreeList component to show tree structured DB table on a form. It was so easy.

create table treesamp (
nodeid number(5),
parentnodeid number(5),
datatext varchar2(50)
);

Sample table data:
1 0 "Root"
2 1 "Leaf 1"
3 1 "Leaf 2"
4 2 "Leaf 1 of Leaf 1"
5 2 "Leaf 2 of Leaf 1"
6 3 "Leaf 1 of Leaf 2"
7 3 "Leaf 2 of Leaf 2"
8 4 "Leaf 1 of Leaf 1 of Leaf 1"
...

Place your TreeList on a form and just set following values:

treelist1.DataSource= treeDataTable;
treelist1.KeyFieldName= "nodeid";
treelist1.ParentFieldName= "parentid";
treelist1.PreviewFieldName= "datatext";
RootValue= 0; // if id of parent is zero, it must be root node

To print the tree treeList1.ShowPrintPreview();
It prints it in XtraReport Preview, the same as in form grid.

Monday, March 24, 2008

oracle cast function in stored procedure

There is a thing can be called stupid in oracle. I can't understand the reason!
This is a piece of code in a Oracle Trigger. Decimal numbers are compared roundly by double precision.

if (cast(to_char(:new.unit_price) as number(14,2)) = cast(to_char(v_ppl_unit_price) as number(14,2))) then
:new.unit_price := v_temp_new_unit_price;
end if;

But after executed this code, an error is occurred:

Error: PLS-00103: Encountered the symbol "(" when expecting one of the following:
. ) @ %
The symbol ")" was substituted for "(" to continue.
Line: 122
Text: if ( cast(:new.unit_price as number(14, 5)) =

And i found out how to fix it.
that is declare a subtype, and use it on cast:

declare
subtype number_14_2 is number(14, 2);
...
begin
...
if (cast(to_char(:new.unit_price) as number_14_2) = cast(to_char(v_ppl_unit_price) as number_14_2)) then
:new.unit_price := v_temp_new_unit_price;
end if;
...
end

It's done! How strange!

Thursday, March 20, 2008

What's the difference between varchar, varchar2?

Монгол хэл дээр бичигдсэнийг харахыг хүсвэл:
http://mongolcoder.blogspot.com/2008/03/varchar-varchar2.html

Varchar and varchar2 are data types of Oracle DB and what's the difference between them?
First: Size, varchar can store 2000 bytes information, but varchar2 can store 4000 bytes information.
Second: Encoding or language chooser(:D), varchar supports ascii, but varchar2 supports unicode.
In addition to this, mongolian character's take 2 bytes space, but english characters take 1 bytes space.

Tuesday, March 11, 2008

ORA-06502 error occurred while calling stored procedure from C#

Here is a Oracle stored procedure.

create or replace procedure GETVPNOFROMPNO(p_pno in varchar2, ret out varchar2) is
r SYS_REFCURSOR;
npno1 varchar2(255);
retval varchar2(255);
begin
retval:= p_pno;
open r for
select npno from parts_for_update_npno where parts_for_update_npno.pno= p_pno and npno is not null;
fetch r into npno1;
if r%NOTFOUND then
ret:= retval;
return;
end if;
retval:= p_pno||' /'||npno1;
ret:= retval;
end GETVPNOFROMPNO;

As you see, there is no need of REF CURSOR. I declared it just for further use.
And here is a function that calls the stored procedure above.

public static string getVPNOfromPNO(OracleConnection c, string pno)
{
OracleCommand cmd = null;
try
{
c.Open();
cmd = c.CreateCommand();
cmd.CommandText = "GETVPNOFROMPNO";
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("p_pno", OracleType.VarChar);
cmd.Parameters["p_pno"].Value = pno;
cmd.Parameters["p_pno"].Direction = ParameterDirection.Input;

cmd.Parameters.Add("ret", OracleType.VarChar);
cmd.Parameters["ret"].Value = "";
cmd.Parameters["ret"].Direction = ParameterDirection.Output;

cmd.ExecuteNonQuery();
c.Close();
//MessageBox.Show("Suces");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Program.setStatus("Error while getting ...!", ex);
}
return cmd.Parameters["ret"].Value.ToString();
}

Do you think there is any error? I thought not, but there was.
While executing, this code throws "ORA-06502: numeric or value error: character string buffer too small" exception.

The error is corrected by changing cmd.Parameters["ret"].Value = ""; to cmd.Parameters["ret"].Value = "__________________";

Why? I used parameter for getting output value. The too small string buffer in the error description is cmd.Parameters["ret"].Value that is length of zero. After setting enough space for this variable, it was OK. If i used oracle function, it would be OK without any problem.

Friday, February 29, 2008

Database programming method

I'm talking about a method of writing a database application. You can use this method surely, because this is experience based way of programming.

The steps are:
1. Collect your User Requirement Definitions.
2. Now draw your Database Diagram. In database diagram must have all required outputs (reports).
3. You may need to draw Data Flow Diagram to make the system clear.
4. Create all forms that register data. On all command actions call an empty stored procedure that will be created later.
5. Create all program reports by using user requirement defns. Any report must be bound to exactly one database table according to Step 2.
After this 5 ways, you have an application that gets inputs, gives reports, but no processing involved. You can give it to customer to try how to work.
6. Now start writing stored procedures for main processing. It's easy. You have already named stored procedures and defined what to do in it.

My teacher said that:
Success of developing application program is based on systematic method. That's why there are many methods such as traditional system analysis, UML, RUP and so on. But if you have your way of developing programs, that is better than these standards.

I agreed it. Because i created it, suits me and my environment perfectly, no more work, can be changed freely ...

Tuesday, February 19, 2008

Oracle sequence autoincrement field and DataGrid

I needed to create a Form that does insert/edit/update/delete an Oracle table data in a DataGrid. The Oracle table has primary key that is autoincremented. I was using a trigger on before insert new row that sets sequence's nextval into primary key field. I bound DataTable to a GridControl and then inserted new row. The key value of new row in table in DBMS was correct, but GridControl's primary key field value was not updated as in DBMS. I wasted so much time to make it formally done.

My first solution was a forced one. I wrote a stored procedure that returns the sequence's next value. Called it from my C# code and set it to DataTable's primary field column. To use that i disabled the trigger.

Next to it found a Oracle 10g new feature. It's not supported in previous versions of Oracle, i heard. It's RETURNING clause. RETURNING clause is used for return values after executing a query.
For instance:

INSERT (pk_field, field1, field2, field3) INTO table1 VALUES (seq1.NEXTVAL, 'field1', 'field2', 'field3') RETURNING pk_field INTO :pk_field;

This is an example of insertQuery property of TableAdapter. Insert query itself tells that "I" insert nextval of sequence into primary key field and return it back to the grid using :pk_field parameter.

Unfortunately i couldnt get it worked. Visual Studio 2005 was not "understand" RETURNING clause, sayd me wrong SQL.

Later, a few days ago used it Visual Studio 2008. It's working. But i'm still disappointed. I need to do it in VS2005. I hope there must have any solutions. Please let me know if you have any other.