Sys_Refcursor is null - oracle

I have this c# code
public static DataSet Get_Workflow_Def()
{
OracleConnection conn = DatabaseHelper.getOracleConnection();
DataSet dataset = new DataSet();
Log.Info("Opening connection");
conn.Open();
OracleCommand cmd = new OracleCommand("CTS.GET_WORKFLOW_DEF", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("p_Dataset", OracleDbType.RefCursor).Direction =
ParameterDirection.Output;
Log.Info("Executing query");
cmd.ExecuteNonQuery();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dataset);
conn.Close();
return dataset;
}
Calling this procedure
create or replace procedure CTS.GET_WORKFLOW_DEF(p_Dataset OUT
Sys_Refcursor)
as
begin
Open p_Dataset For
Select WKF_ID,
WKF_WORKFLOW_ID,
WKF_WORKFLOW_NAME,
WKF_WORKFLOW_VERSION,
WKF_WORKFLOW_SCHEMA
FROM CTS.WKF_WORKFLOW_MASTER
WHERE WKF_WORKFLOW_ACTIVE = '1'
ORDER BY WKF_ID DESC;
end GET_WORKFLOW_DEF;
If I execute the query I get the information I am looking for but the RefCursor is null. I'm an Oracle newb. Can anyone see where I am going wrong?

Found my problem I needed to add this line before the da.fill(dataset); line and after I instantiated the OracleDataAdapter in my c# code
da.TableMappings.Add("Table",
ConfigurationManager.AppSettings["DBPullTable"].ToString());

Related

how to execute multiple oracle query c#

i am trying to execute multiple oracle select query as explain at that post answer here but I am getting exception as show at image
the same way explained at oracle site here
btw is ther anyway to handle the no rows found from one of these queries ?
string cmdstr = #"begin open :1 for
SELECT a.column1,
a.olumn2
b.column3
FROM table1 A,table2 B
WHERE A.column1=B.column1
AND A.column2 = NVL(:P_para, 0)
AND B.column3='1';
open :2 for select column1,
column2,
column3,
From dual; end;";
using (OracleConnection conn = new OracleConnection(connstr))
using (OracleCommand cmd = new OracleCommand(cmdstr, conn))
{
try
{
cmd.Parameters.Add(new OracleParameter(":P_para", OracleDbType.Int64)).Value = Convert.ToInt64(Textbox.Text);
cmd.Parameters.Add("p_cr1", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
cmd.Parameters.Add("p_cr2", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
cmd.CommandText = cmdstr;
conn.Open();
OracleTransaction trans = conn.BeginTransaction();
OracleDataReader oraReder;
oraReder = cmd.ExecuteReader();
while (oraReder.Read())
{
textbox1.Text = oraReder.GetString(0).ToString();
textbox2.Text = oraReder.GetValue(1).ToString();
textbox3.Text = oraReder.GetValue(2).ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Erorr Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Although you're using names for your parameters, your driver is treating them positionally. You can kind of tell because it's (almost) matching :1 with the name p_cr1 - '1' isn't a valid name. It doesn't complain since it matches positionally - but that means it's trying to use the P_para for :1, and as the type of that is wrong, that explains the error you see.
There may well be a way to change the driver's behaviour, but for now you can just swap the order you bind them - so the binds occur in the same order (position) the variables appear in the query. So:
cmd.Parameters.Add("p_cr1", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
cmd.Parameters.Add(new OracleParameter(":P_para", OracleDbType.Int64)).Value = Convert.ToInt64(Textbox.Text);
cmd.Parameters.Add("p_cr2", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);

ORA-01840: input value not long enough for date format when executing a query with a timestamp parameter

I am trying to execute a query on Oracle database table with a TimeStamp(6) column, using odp.net
I am using below code, which throws this exception: "ORA-01840: input value not long enough for date format".
OracleConnection con = new OracleConnection(connStr");
using (con)
{
con.Open();
OracleCommand command = con.CreateCommand();
command.CommandText = "SELECT * FROM Logs WHERE LOGDATE > :logDate";
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new OracleParameter("logDate", OracleDbType.TimeStamp, DateTime.Now.AddMonths(-1), ParameterDirection.Input));
using (command)
{
OracleDataReader rdr = command.ExecuteReader(); //ORA-01840 exception is thrown here
}
}
What is wrong about this query and it's parameters? I also tried OracleDbType.Date, instead of OracleDbType.TimeStamp but i got the same error.
I solved the problem by passing an OracleTimeStamp object instead of DateTime object. Below code works good:
OracleConnection con = new OracleConnection(connStr");
using (con)
{
con.Open();
OracleCommand command = con.CreateCommand();
command.CommandText = "SELECT * FROM Logs WHERE LOGDATE > :logDate";
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new OracleParameter("logDate", OracleDbType.TimeStamp, new OracleTimeStamp(DateTime.Now.AddMonths(-1)), ParameterDirection.Input));
using (command)
{
OracleDataReader rdr = command.ExecuteReader(); //ORA-01840 exception was thrown here
}
}

calling oracle stored procedure in c#

I am trying to call a oracle stored procedure in c# like this
OdbcDataAdapter da = new OdbcDataAdapter();
da.SelectCommand = new OdbcCommand();
da.SelectCommand.Connection = con;
da.SelectCommand.Connection.Open();
da.SelectCommand.CommandText = "KAMRAN.ATTN";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.ExecuteNonQuery();
da.SelectCommand.Connection.Close();
Label3.Text = "Attendance Posted Successfully";
But I am showing this error
ERROR [42000] [Oracle][ODBC][Ora]ORA-00900: invalid SQL statement
Please any One Can tell what exactly i am missing to call this procedure.plz help !
You could try to inverse the statements:
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.CommandText = "KAMRAN.ATTN";
Also, maybe try to add:
da.SelectCommand.Parameters.Clear();
before
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.CommandText = "KAMRAN.ATTN";
I think that you should also use a DataTable
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
Maybe that it should also be an Oracle Data Adapter:
OracleDataAdapter da = new OracleDataAdapter();
da.SelectCommand = new OracleCommand();
So you should have:
OracleDataAdapter da = new OracleDataAdapter();
da.SelectCommand = new OracleCommand();
da.SelectCommand.Connection = con;
da.SelectCommand.Connection.Open();
da.SelectCommand.Parameters.Clear();
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.CommandText = "KAMRAN.ATTN";
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
da.SelectCommand.Connection.Close();
I hope this helps in any way.

How to Pass datatable as input to procedure in C#?

I am wrote stored procedure to Insert data into database table but i am not getting how to pass datatable to stored procedure kindly tell how to use it.
below is my storedprocedure
CREATE OR REPLACE PROCEDURE PR_SREE_TEST(p_recordset In SYS_REFCURSOR) IS
Contrac_rc SREE_TEST%rowtype;
BEGIN
Loop
Fetch p_recordset Into Contrac_rc;
EXIT WHEN p_recordset%NOTFOUND;
Insert into SREE_TEST(CT,DESC,FLAG)
Values(Contrac_rc.CT,Contrac_rc.DESC,Contrac_rc.FLAG);
End Loop;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END PR_SREE_TEST;
/
and cs page
public DataSet sreetest(DataTable dt)
{
DataSet dsRegularIndentdtl = new DataSet();
try
{
OracleConnection OraConn = new OracleConnection(strDBConnection);
OraConn.Open();
OracleCommand OraCmd = new OracleCommand();
OraCmd.Connection = OraConn;
OraCmd.CommandText = "PR_SREE_TEST";
OraCmd.CommandType = CommandType.StoredProcedure;
OracleParameter parameter = new OracleParameter();
var recordSet1 = new DataTable();
recordSet1 = dt;
OraCmd.Parameters.Add("p_recordset", OracleDbType.RefCursor, recordSet1, ParameterDirection.Input);
OraCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
return dsRegularIndentdtl;
}
}
above is my code it is saying that p_recordset not valied. please tell me how to execute it.
Make sure you are using the correct overload of OraCmd.Parameters.Add(),
As per msdn the fourth parameter is not parameter direction, it is as below
public OracleParameter Add(
string parameterName,
OracleType dataType,
int size,
string srcColumn
)

Problem with cmd.ExecuteNonQuery()

I am inserting values in to Database from a Webform using ADO.NET, C#. DB I am using is Oracle Database. Values are not being inserted and the program gets struck at the cmd.ExecuteNonquery()
Here is my Code below, Please let me know If I am doing any mistake.. I am using some Static Methods will that be any problem ?..
public Boolean AddDivCo(Int32 UserNo,String ID, String Role, String DivName )
{
Boolean ret = false;
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection();
int i = 0;
try
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["Conn_RIS"].ConnectionString;
conn.Open();
cmd.Connection = conn;
String mySQL = "INSERT INTO R4CAD_ADMIN (AdminUserNo, AdminID, AdminRole, AdminDivName)VALUES(:AdminUserNo,:AdminID,:AdminRole,:DivName)";
OracleParameter p1 = new OracleParameter("AdminUserNo", OracleType.Number);
p1.Value = UserNo;
cmd.Parameters.Add(p1);
OracleParameter p2 = new OracleParameter("AdminID", OracleType.VarChar);
p2.Value = ID;
cmd.Parameters.Add(p2);
OracleParameter p3 = new OracleParameter("AdminRole", OracleType.VarChar);
p3.Value = Role;
cmd.Parameters.Add(p3);
OracleParameter p4 = new OracleParameter("DivName", OracleType.VarChar);
p4.Value = DivName;
cmd.Parameters.Add(p4);
cmd.CommandText = mySQL;
i = cmd.ExecuteNonQuery();
if (i != 0)
{
ret = true;
}
else
{
ret = false;
}
}
catch (Exception err)
{
Console.WriteLine(err.Message.ToString());
}
finally
{
cmd.Dispose();
//cmd = null;
//conn = null;
conn.Close();
}
return ret;
}
Is there a primary key defined on this table? If so, then my guess is that you have another session that already has inserted a record with this key, but has not yet terminated the transaction with a commit or rollback. I don't see a commit as part of your code - I assume you're doing that somewhere else?
Execute your code above once more, and while it's hung run the following query from another session:
SELECT
(SELECT username FROM v$session WHERE sid=a.sid) blocker,
a.sid,
' is blocking ',
(SELECT username FROM v$session WHERE sid=b.sid) blockee,
b.sid
FROM v$lock a JOIN v$lock b ON (a.id1 = b.id1 AND a.id2 = b.id2)
WHERE a.block = 1
AND b.request > 0;
This should tell you if you're being blocked by another session and what the SID is of that session.

Resources