Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Thursday, June 17, 2021

[VB.net][Resolved] dataGridView dataAdapter value not show

I have set some dataSet and BindingSource and used this code.

These code should show 5 rows of record but it shows 6 empty rows only (including 1 empty new lines), 

the values form database can't be matched:


Source

Import Oracle.DataAccess.Client

Public Class Form1

  Dim dbCommand As OrcaleCommand

  Dim sAdapter As OracleDataAdapter

  Dim sBuilder As OracleCommandBuilder

  Dim dsData As DataSet

  Dim dtData As DataTable

  

  Private Sub loadBtn_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadBtn.Click

    Dim connStr As String = "DataSource=.;Initial Catalog=pubs;Integrated Security=Time"

    Dim sql As String = "SELECT * FROM (SELECT * FROM LIBBKN_BATCH ORDER BY BATCH_NO) WHERE ROW_NAME <=5"

    Dim conn As OrcaleConnection(connStr)

    conn.Open()

    dbCommand = New OrcaleCommand(sql, conn)

    sAdapter = New OrcaleDataAdapter(dbCommand)

    sBuilder = New OrcaleCommandBuilder(sAdapter)

    dsData = New DataSet()

    sAdapter.Fill(sDs,"BATCH")

    sTable = sDs.Tables("BATCH")

    conn.Close()

    

    Me.dgv.DataSource = dsData.Tables("BATCH")

    Me.dgv.DataSource.ReadOnly = True

  End Sub

End Class


After some search from internet, I found my problem is I set the "name" of column only,

I should also set "DataPropertyName" value, value need to same as database column name.

(This Image from ShunNien's Blog)


Reference

https://stackoverflow.com/questions/24049866/winforms-datagridview-showing-blank-rows

https://shunnien.github.io/2015/12/15/DataGridView-in-winform-1/

Tuesday, June 15, 2021

[VB.net][Resolved] DataAccess.Client.OracleException ORA-00911 invalid character

Error message:

DataAccess.Client.OracleException ORA-00911 invalid character


Source:

Dim oradb As String = "Data Source=127.0.0.1/CLP;User Id=user;password=password"

com = New OracleConnection(oradb)


Dim cmd As New OrcaleCommand

cmd.Connection = conn

cmd.CommandText = "SELECT COUNT(*) AS amount FROM batches WHERE batch_no LIKE '20200713%';"


Dim dr As OracleDataReader = cmd.ExecuteReader()

dr.Read()


Solution:

sql syntax error, for my case is remove the semi-colon from the end of sql:

change 

cmd.CommandText = "SELECT COUNT(*) AS amount FROM batches WHERE batch_no LIKE '20200713%';"

to 

cmd.CommandText = "SELECT COUNT(*) AS amount FROM batches WHERE batch_no LIKE '20200713%'"


Reference: 

https://stackoverflow.com/questions/12262145/ora-00911-invalid-character/18456333


Wednesday, February 3, 2021

Tuesday, February 2, 2021

[java][Resolved] java.SQL.Exception: Invalid column index

 Erorr message:

java.SQL.Exception: Invalid column index

source code:

cstmt = (OracleCallableStatment) con.prepareCall('BEGIN SP_TEST(?,?,?,?,?,?,?)');

cstmt.setInt(1,40030485);

cstmt.setString(2,"filepath");

cstmt.setString(3,"%/temp/%");

cstmt.setString(4,"Y");

cstmt.registerOutParameter(5,OrcaleTypes.NUMBER);

cstmt.registerOutParameter(6,OrcaleTypes.DECIMAL);

cstmt.registerOutParameter(7,OrcaleTypes.VARCHAR);

cstmt.registerOutParameter(8,OrcaleTypes.CURSOR);


For my case, it looks the I called store procedure with incorrect parameters, and the error not related to column :

cstmt = (OracleCallableStatment) con.prepareCall('BEGIN SP_TEST(?,?,?,?,?,?,?,?)');

cstmt.setInt(1,40030485);

cstmt.setString(2,"filepath");

cstmt.setString(3,"%/temp/%");

cstmt.setString(4,"Y");

cstmt.registerOutParameter(5,OrcaleTypes.NUMBER);

cstmt.registerOutParameter(6,OrcaleTypes.DECIMAL);

cstmt.registerOutParameter(7,OrcaleTypes.VARCHAR);

cstmt.registerOutParameter(8,OrcaleTypes.CURSOR);


Thursday, January 7, 2021

[VB.net][Oracle][example] Add items to datagridView from Orcale

This part of script is from one of the vb projects.

beware that there is some syntax such as OracleDataAdapter Class has been deprecated :

Imports System.Data

Imports System.IO

Imports Orcale.DataAccess.Client

Imports Orcale.DataAccess.Types


Public Class Form1

  Private connStr As String = "Data Source=192.168.1.1/TestDb; User Id=user; password=pwd"

  Private conn As New OrcaleConnection

  Private Sub Form1_load(ByVal sender As System.Object, ByVal e As Sysstem.EventArgs) Handles MyBase.Load

  Try

    Dim ds As New DaatSet()

    Using conn As OracleConnection = New OracleConnection(connStr)

      conn.Open()

      Using cmd As OracleCommand = New OracleCommand("SELECT * FROM TEST_TABLE ORDER BY ID",conn)

      Dim dr As OracleDataReader = cmd.ExecuteReader()

        While dr.Read()

          Dim rowId As Integer = DataGridView1.Rows.Add()

          Dim row As DataGridViewRow = DataGridView1.Rows(rowId)

          row.Cells("idColumn").Value = dr.Item(0)

          row.Cells("typeColumn").Value = dr.Item(1)

        End While

      End Using

    End Using

  Catch ex As Exception

    Debug.WriteLine(ex.ToString())

  Finally

    conn.Dispose()

  End Try

End Sub

Reference

  • https://www.codegrepper.com/code-examples/csharp/vb.net+add+row+to+datagridview+programmatically
  • https://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/GettingStartedVBVersion/GettingStartedNET_VBVersion.htm

Thursday, November 19, 2020

[Oracle][Resolved] ORA-06512: PL/SQL: numeric or value error: number precision too large.

Error message:

ORA-06512: PL/SQL: numeric or value error: number precision too large.

ORA-06512: at "SP_UPDATE_TEST", line 36

ORA-06512: at line 24

I defined the valuable length as 1.

int count NUMBER(1);

and I haven't take care that the return result count more than 9, the valuable doesn't long enough to store the result number:

SELECT COUNT(1) INTO int_count FROM ASSETS

WHERE ASSET_ID = TO_NUMBER(in_asset);

Correction:

int count NUMBER(2);

Reference:

https://www.techonthenet.com/oracle/errors/ora06502.php




Monday, October 5, 2020

[VB.net][Orcale][Resolved] A first chance exception of type "System.ArgumentNullException" occurred in Orcale.DataAccess.dll

ERROR MESSAGE

A first chance exception of type "System.ArgumentNullException" occurred in Orcale.DataAccess.dll

System.ArgumentNullException: 值不能為null

參數名稱: command

於 Oracle.DataAccess.Client.OracleDataAdapter.Fill (DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, DbCommand commad, CommandBehavior behavior)

於 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)

於 test.frmUsr.tbSearch_Click(Object sender, EventArgs)於 ....frmUsr.vb: 行138

The thread 0x5b0 has exited with code 0 (0x0)


Source

With dbCommand

  .Connection = gConn

  .CommandType = CommandType.Text

  .CommandText = "SELECT * FROM liblnk_group"

  dr = .ExecuteReader()


  dataAdapter.Fill(dsUsr,"liblnk_group")

  Me.dgvData.DataSource = dsUsr.Tables("liblnk_group")

  While dr.Read()

    Debug.WriteLine(dr.Item(0)+","+dr.Items(1)+","+de.Item(2))

  End While

End With


Amendment

With dbCommand

  .Connection = gConn

  .CommandType = CommandType.Text

  .CommandText = "SELECT * FROM liblnk_group"

  dr = .ExecuteReader()


  dataAdapter.SelectCommand = dbCommand

  dataAdapter.Fill(dsUsr,"liblnk_group")

  Me.dgvData.DataSource = dsUsr.Tables("liblnk_group")

  While dr.Read()

    Debug.WriteLine(dr.Item(0)+","+dr.Items(1)+","+de.Item(2))

  End While

End With

Sunday, September 20, 2020

[VB.net] namesapce or type specified in the imports oracle.dataaccess.types desn't contain any public member or cannot be found.

 Error message:

Type "OracleConnection" is not defined.

Type "OracleDataReader" is not defined.

Type "OracleCommand" is not defined.

Type "OracleDbType" is not defined.


Please confirm you import these file for your project reference:

System.Data.OracleClient.dll

Oracle.DataClient.dll


After Imported but found another problem, you need also import Microsoft.VisualBasic.Compatibility.dll :

namespace or type specified in the imports oracle.dataaccess.types desn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases

Monday, August 31, 2020

[VB.Net][Oracle][Resolved] oracle.DataAccess.Client.OracleException ORA-12154: TNS: COULD NOT RESOLVE THE CORRECT IDENTIFIER SPECIED.

Error message:

oracle.DataAccess.Client.OracleException ORA-12154: TNS: COULD NOT RESOLVE THE CORRECT IDENTIFIER SPECIED.

at Orcale.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, intPtr opsErrCtx, OpcSqlValCtx* pOpoSqlValCtx, Object src, String procedure, Boolean bCheck)

at Orcale.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, IntPtr epsErrCtx, Object src)

at Oracle.DataAccess.Object.OracleConnection.Open()

at WindowsApplication1.Form1.Form1_load(Object sender,EventArgs e) at C:\Users\g44612\Documents\Visual Studio 2008\Projects\test\test\Form1.vb : line 12

Source:

Try

Dim oradb As String = "Data Source=books;User Id=admin;password=password"

    conn=New OracleConnection(oradb)

    conn.Open()


    Dim cmd As New OracleCommand

    cmd.connection = conn

    cmd.commandText = "SELECT COUNT(*) AS amount FROM BATCH"

    Dim dr As OracleDataReader = cmd.ExceuteReader()

    dr.Read()


    Dim amount As String = dr.Item("amount")

    Debug.WriteLine("Amount:"+amount)

catch ex As Exception

    Debug.WriteLine(ex.toString())

Finally

    conn.dispose()

End Try


Correction

The source code show I used TNS alias to create connection. Here is the example TNS Alias from oofical site:

"user id=scott;password=tiger;data source=sales";

TNS alias is in connection string and looks like this:

 Dim oradb As String = "Data Source=(DESCRIPTION=" _

 + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=OTNSRVR)(PORT=1521)))" _

 + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));" _

 + "User Id=scott;Password=tiger;"

 

The reason why I got error is cos I am not entered the full address of ther server which hosted the Oracle database, 

Try

Dim oradb As String = "Data Source=192.168.1.1/books;User Id=admin;password=password"

    conn=New OracleConnection(oradb)

    conn.Open()


    Dim cmd As New OracleCommand

    cmd.connection = conn

    cmd.commandText = "SELECT COUNT(*) AS amount FROM BATCH"

    Dim dr As OracleDataReader = cmd.ExceuteReader()

    dr.Read()


    Dim amount As String = dr.Item("amount")

    Debug.WriteLine("Amount:"+amount)

catch ex As Exception

    Debug.WriteLine(ex.toString())

Finally

    conn.dispose()

End Try


Reference:

https://stackoverflow.com/questions/14486632/asp-net-webapp-ora-12154-tnscould-not-resolve-the-connect-identifier-specifi

https://docs.oracle.com/cd/B28359_01/win.111/b28375/featConnecting.htm

Wednesday, August 26, 2020

[Oracle][PL/SQL][Example] Oracle stored procedure with dynamic-SQL and cursor

Create Statement : 

 CREATE OR REPLACE PROCEDURE "SP_GET_LIB_BOOK"

  in_column_name IN VARCHAR2,

  in_column_value IN VARCHAR2,

  in_sort_column IN VARCHAR2,

  in_sort_order IN VARCHAR2,

  out_int_count OUT DECIMAL,

  out_cursor OUT TYPES.CURSOR_TYPE,

  out_err_msg OUT VARCHAR2)

AS

  dynSQL VARCHAR2(4000)

BEGIN

  out_err_msg := '';

  dynSQL := 'SELECT * FROM LIB_ASSET';


  IF LENGTH(in_sort_column) > 0 THEN

    dySQL := dySQL || 'ORDER BY' || in_sort_column || ' ';

  ELSE

    dySQL := dySQL || 'ORDER BY ASSET_ID ';

  END IF


  IF LENGTH(in_sort_order) > 0 THEN

    dySQL := dySQL || in_sort_order;

  ELSE

    dySQL := dySQL || 'ASC ';

  END IF

 

  EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM (' || dynSQL || ') INTO out_int_count';

  OPEN out_cursor FOR dynSQL;


EXCEPTION

  WHEN OTHERS THEN

    out_cursors := SQLERRM || '(Code:' || SQLCODE || ')';

END

[Oracle][PL/SQL][VB][Example] Call oracle stored procedure by VB with cursor

Oracle Stored Procedure:

CREATE OR REPLACE PROCEDURE "SP_GET_LIB_BOOK"

  out_cursor OUT TYPES.CURSOR_TYPE)

AS

BEGIN

  out_err_msg := '';

  OPEN out_cursor FOR

  SELECT * FROM LIB_ASSET

  WHERE ASSET_ID = 14131

EXCEPTION

  WHEN OTHERS THEN

    out_cursors := SQLERRM || '(Code:' || SQLCODE || ')';

END


VB.NET

Dim dbCommand As New OrcaleCommand

Dim dr As OrcaleDateReader = Nothing

With dbCommand 

  .Connection = gPassConn

  .Parameters.Clear()

  .CommandText = "SP_GET_LIB_BOOK"

  .CommandType = CommandType.StoredProcedure

  .Parameters.Clear()

  .Parameters.Add("@out_cursor",OrcaleDbType.RefCursor).Direction = ParameterDirection.Output

  

  dr = .ExecuteReader

  While dr.Read

    Dim assetId As Long = dr.Item("asset_id")

    Dim storeFileName As String = dr.Item("store_file_name")

    Debug.WriteLine(CStr(assetId))

    Debug.WriteLine(storeFileName)

  End While

End With

Sunday, August 23, 2020

[Oracle][Resolved] Invalid number -1722 insert

Error message:

Error starting at line 2 in command:

INSERT INTO LIB_BUFFER(BATCH_NO,CLINK_ID,PGM_FM,ID_FM,REMARK,CRE_USER,CRE_DATE)

VALUES ('20991131-2','LIBVDO_'+11110001),'LIBVDO',1111001,'title1','Super',sysdate);

Error report:

SQL Error : ORA-01722: invalid number

* Cause:

* Action:

SQL:

INSERT INTO LIB_BUFFER(BATCH_NO,CLINK_ID,PGM_FM,ID_FM,REMARK,CRE_USER,CRE_DATE)

VALUES ('20991131-2','LIBVDO_'+11110001,'LIBVDO',1111001,'title1','Super',sysdate);

We should not use symbol + to connect 2 values, use CONCAT function

INSERT INTO LIB_BUFFER(BATCH_NO,CLINK_ID,PGM_FM,ID_FM,REMARK,CRE_USER,CRE_DATE)

VALUES ('20991131-2',CONCAT('LIBVDO_',11110001),'LIBVDO',1111001,'title1','Super',sysdate);

Reference:

https://www.techonthenet.com/oracle/functions/to_char.php

https://stackoverflow.com/questions/33994013/invalid-number-during-insert-query

[VB][Orcale][Resolved] ORA-06502: PL/SQL: numeric or value error: character string buffer too small

Error message "ORA-06502: PL/SQL: numeric or value error: character string buffer too small" mean your column data type length for database is not enough to store the value.

VB:

Dim dbCommand As New OrcaleCommand

With dbCommand

  .Connect = gClipConn

  .CommentText = "SP_ADD_BOOK"

  .Parameters.Clear()

  .Parameters.Add("@in_batch_no","20200820-01")

  .Parameters.Add("@in_cre_user","Super")

  .Parameters.Add("@out_result",Orcale.DbType.Decimal).Direction = ParameterDirection.Output

  .Parameters.Add("@out_err_msg",Orcale.DbType.Varchar2).Direction = ParameterDirection.Output

  .ExecuteNonQuery()


  Debug.WriteLine(.Parameters.Item("@out_err_msg").Value().ToString())

End With


Procedure:

CREATE OR REPLACE

PROCEDURE SP_ADD_BOOK(

  in_batch_no IN VARCHAR2,

  in_cre_user IN VARCHAR2,

  out_result OUT DECIMAL,

  out_err_msg OUT VARCHAR2)

AS

  intCount NUMBER(18,0);

BEGIN

  out_result :=9;

  out_err)msg := "dfgdfgdfg"

END


Correction

Dim dbCommand As New OrcaleCommand

With dbCommand

  .Connect = gClipConn

  .CommentText = "SP_ADD_BOOK"

  .Parameters.Clear()

  .Parameters.Add("@in_batch_no","20200820-01")

  .Parameters.Add("@in_cre_user","Super")

  .Parameters.Add("@out_result",Orcale.DbType.Decimal).Direction = ParameterDirection.Output

  .Parameters.Add("@out_err_msg",Orcale.DbType.Varchar2,100).Direction = ParameterDirection.Output

  .ExecuteNonQuery()


  Debug.WriteLine(.Parameters.Item("@out_err_msg").Value().ToString())

End With

Monday, August 17, 2020

[Oracle][Resolved] Oracle get current db name

In this example, Suppose the database we are using is named "QASYS" but I don't know the name, we can use this sql to check:

sql:

SELECT * FROM global_name;


Result

QASYS.REGRESS.RDBMS.DEV.US.ORCALE.COM


Reference:

https://stackoverflow.com/questions/6288122/checking-oracle-sid-and-database-name

Monday, August 3, 2020

[Oracle][Resolved] SQL for orcale to limit result amount

10g version : 
SELECT * FROM 
  (SELECT * FROM ARTICLE) 
WHERE ROWNUM <=5;
Syntax supported by 12g version:
SELECT * FROM article 
ORDER BY article_id
OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY;

Reference:

Sunday, August 2, 2020

[Oracle][Resolved] Find out your Orcale version

To know all information from Orcale:
SELECT * FROM v$version;

If you want to know your Oracle version only
SELECT * FROM v$version WHERE banner like 'Oracle%';


Reference:
https://www.techonthenet.com/oracle/questions/version.php

[Orcale][Example] DUAL table

DUAL table is is a special one-row (by default value is X), one column (data type VARCHAR2(1)) table which with used for evaluating expressions or calling functions., it also the most simple table designed for fast access

Example 1: SELECT * mean do nothing 
SELECT * FROM dual;
---------------
|    DUMMY    |
---------------
|      X      |
---------------
From the result we can found the default setting of DUAL table: column name: DUMMY, and field value is X.


Example 2: Show string
SELECT 'Hello' FROM dual;
--------------------
|   |    'HELLO'   |
--------------------
| 1 |    'Hello'   |
--------------------

Example 3: Show number
SELECT 4 FROM dual;
-----------
|   |  4  |
-----------
| 1 |  4  |
-----------

Example 4: Show current user
SELECT USER FROM dual;
------------------
|   |    USER    |
------------------
| 1 |  SYSADMIN  |
------------------

Example 5: Show current Oracle date
SELECT SYSDATE FROM dual;
---------------------
|   |    SYSDATE    |
---------------------
| 1 |   03-8月 -20  |
---------------------

Example 6: Show string with build-in function
SELECT UPPER('Hello') FROM dual;
-----------------------
|   |  UPPER('HELLO') |
-----------------------
| 1 |    'HELLO'      |
-----------------------

Example 7: Show expression calculation result by accesss the DUAL table:
SELECT 1000+999 FROM dual;
------------------
|   |  1000+999  |
------------------
| 1 |    1999    |
------------------

Reference:

Saturday, August 1, 2020

[Oracle][Resolved] Oracle show table schema

Suppose that I have a table named article and contains many columns. 
I would like to have a look of the table structure. Let use describe command.

Syntax 
DESCRIBE { table-Name | view-Name }

Example:
DESCRIBE article;
Result : 

Reference:

Monday, September 30, 2013

[Oracle][Chrome] Change language for Enterprise Manager

Enterprise Manager's language is depend on your browser setting, 
if you want to change the language, 
you can change your browser setting first.

These pic below is to for showing the way to change browser language setting in google chrome:

(Step 1)



Step 2) Choose "Show advanced Setting:"



Step 4) Choose your language